Carrier Sense Multiple Access With Collision Avoidance Aim
Carrier Sense Multiple Access With Collision Avoidance Aim
AIM:
To write a program to implement the CSMA/CA using Java.
PRINCIPLE:
Carrier sense multiple access with collision avoidance (CSMA/CA) is a method in which carrier sensing is
used, nodes attempt to avoid collisions by transmitting only when the channel is sensed to be "idle"
ALGORITHM:
1. Start the program.
2. Initialize k+0.
3. Check for the channel idleness.
4. If the channel is busy, wait for IFS (InterFrame Space) time.
5. Again check for the channel idleness.
6. If the channel is busy, keep on checking channel idleness.
7. If the channel is idle, choose a random number.
8. Wait for R slots.
9. Send frame now.
10. Set wait time for receiving acknowledgment.
11. If the acknowledgement is received, then the transmission will be completed.
12. Else, go for checking the channel idleness.
13. Stop the program.
PROGRAM:
//CSMA/CA PROGRAM - CLIENT
import java.io.*;
import java.net.*;
public class client
{
public static void main(String[] args)
{
try
{
System.out.println("============ Client 1 ===============");
client cli = new client();
int R = 0;
Boolean bln = false;
for(int k=1; k<=15;k++)
{
System.out.println("attempt : "+k);
// is idle channel?
System.out.println("is Channel idle? ");
int i = 0;
while(true)
{
System.out.print(i=i+1);
if(cli.isidle())
{
System.out.println("\n Channel idle");
System.out.println("Wait IFS time 5000");
// wait for IFS time
Thread.sleep(8000);
// is still idle channel?
System.out.println("is still idle?");
if(cli.isidle())
{
System.out.println("Still idle");
// Choose random number
R = 2^k-1;
System.out.println("Selected Random number :"+R);
System.out.println("waiting for R slot time: "+R*6000);
// wait R slot
Thread.sleep(R*6000);
// send frame
System.out.println("Message sent");
// Timer runs and wait for time out
System.out.println("Wait for time out : "+10000);
Thread.sleep(10000);
// ack check
if(cli.isidle())
{
System.out.println("Ack received");
bln = true;
break;
}
else
{
System.out.println("Ack not received");
break;
}
}
else
{
System.out.println("Busy, goes to channel idle check");
}
}
}
if(bln == true)
{
break;
}
}
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
Boolean isidle()
{
try
{
Socket soc= new Socket("localhost",137);
soc.close();
return true;
}
catch (Exception e)
{
return false;
}
}
}
OUTPUT:
Case 1: don’t run client 2 (Channel Busy always)
D:\jdk1.6.0\bin>javac client.java
D:\jdk1.6.0\bin>java client
============ Client 1 ==============
attempt : 1
is Channel idle?
123456789101112131415161718192021222324
Case 2: stop client2 on “is still idle” (Channel busy after IFS time)
D:\jdk1.6.0\bin>javac client.java
D:\jdk1.6.0\bin>java client
============ Client 1 ==============
attempt : 1
is Channel idle? 1
Channel idle
Wait IFS time 5000
is still idle?
Busy, goes to channel idle check
2345678910
RESULT:
Thus the above program to implement the CSMA/CA using java has been performed, verified and executed
successfully.