CSMA Implementation in Java
This is the CSMA Implementation in java
Carrier sense multiple access (CSMA) is a probabilistic media access control (MAC) protocol in which a node verifies the absence of other traffic before transmitting on a shared transmission medium, such as an electrical bus, or a band of the electromagnetic spectrum.
Carrier sense means that a transmitter uses feedback from a receiver to determine whether another transmission is in progress before initiating a transmission. That is, it tries to detect the presence of a carrier wave from another station before attempting to transmit. If a carrier is sensed, the station waits for the transmission in progress to finish before initiating its own transmission. In other words, CSMA is based on the principle “sense before transmit” or “listen before talk”.
Multiple access means that multiple stations send and receive on the medium. Transmissions by one node are generally received by all other stations connected to the medium.
import java.io.*;
import java.net.*;
class csma extends Thread
{
Thread t3,t2,t1;
String th;
static int x=0;
csma(String name)
{
super(name);
start();
}
int get(){return x;}
void put(){x++;}
public void run()
{
try
{
int a,b,i=0;
while(true){
a=get();
b=get();
if(a==b) {put();a=get();
System.out.println("a="+a);
th=Thread.currentThread().getName();
System.out.println("Station Transmitting from"+th+" from"+i++);
Thread.sleep(1000);
}
b=get();
System.out.println("b= "+b);
System.out.println("a after sleeps "+a);
if(a!=b){
System.out.println("Station "+th+"Collision OCcured form"+(i-1));
Thread.sleep(1000);
}
else
{
System.out.println("Station"+th+"Success frame "+i);
}
}
}
catch(Exception e){}
}
public static void main(String[] args)
{
csma c1=new csma("A");
csma c2=new csma("B");
csma c3=new csma("C");
while(true);
}
}
can i get explanation of the program?