0% found this document useful (0 votes)
97 views

Carrier Sense Multiple Access With Collision Avoidance Aim

The document describes a Java program that implements Carrier Sense Multiple Access with Collision Avoidance (CSMA/CA). CSMA/CA is a method where nodes attempt to avoid collisions by transmitting only when the channel is sensed to be idle. The program checks if the channel is idle, waits for the Inter-Frame Space if busy, transmits if idle by waiting a random number of slots, and waits for acknowledgment. The program was tested in different scenarios and successfully executed the CSMA/CA algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Carrier Sense Multiple Access With Collision Avoidance Aim

The document describes a Java program that implements Carrier Sense Multiple Access with Collision Avoidance (CSMA/CA). CSMA/CA is a method where nodes attempt to avoid collisions by transmitting only when the channel is sensed to be idle. The program checks if the channel is idle, waits for the Inter-Frame Space if busy, transmits if idle by waiting a random number of slots, and waits for acknowledgment. The program was tested in different scenarios and successfully executed the CSMA/CA algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

CARRIER SENSE MULTIPLE ACCESS WITH COLLISION AVOIDANCE

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;
}
}
}

//CSMA/CA PROGRAM - SERVER


import java.io.*;
import java.net.*;

public class server


{
public static void main(String[] args)
{
try
{
System.out.println("============ Client 2 ===============");
while(true)
{
ServerSocket ss = new ServerSocket(137);
System.out.println("Waiting for connection");
ss.accept();
ss.close();
System.out.println("Connected");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}

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

Case 3: stop client2 on Ack received check (Collision occured)


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?
Still idle
Selected Random number :2
waiting for R slot time: 12000
Message sent
Wait for time out : 10000
Ack not received
attempt : 2
is Channel idle?

Case 4: CSMA/CA ack received


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?
Still idle
Selected Random number :2
waiting for R slot time: 12000
Message sent
Wait for time out : 10000
Ack received

RESULT:
Thus the above program to implement the CSMA/CA using java has been performed, verified and executed
successfully.

You might also like