0% found this document useful (0 votes)
124 views3 pages

Implementing CSMA/CD in Java

The document describes a Java program that implements Carrier Sense Multiple Access with Collision Detection (CSMA/CD). It explains the principle of CSMA/CD, which uses carrier sensing to detect collisions on wired networks. The program includes a server and client: the client sends messages and retries transmission if a collision is detected, while the server receives and displays messages. The program was run successfully, with the client detecting and handling collisions over multiple transmission attempts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views3 pages

Implementing CSMA/CD in Java

The document describes a Java program that implements Carrier Sense Multiple Access with Collision Detection (CSMA/CD). It explains the principle of CSMA/CD, which uses carrier sensing to detect collisions on wired networks. The program includes a server and client: the client sends messages and retries transmission if a collision is detected, while the server receives and displays messages. The program was run successfully, with the client detecting and handling collisions over multiple transmission attempts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CARRIER SENSE MULTIPLE ACCESS WITH COLLISION DETECTION

AIM: To write a program to implement the CSMA/CD using java.

PRINCIPLE:
The Carrier Sense Multiple Access is based on the principle of “SENSE BEFORE TRANSMIT”. This
CSMA/CD is generally used in wired networks. It is mainly focused to detect the collision if it has occurred.

ALGORITHM:SERVER:
1. Start the program.
2. Initialize server socket.
3. Display waiting for connection.
4. Initialize the socket and accept the client message.
5. Display connected with client.
6. Initialize i/p stream.
7. Read message.
8. Display message from client.
9. Close all objects.
10. Stop the program.
CLIENT:
1. Start the program.
2. Open socket with input address ,port
3. Initialize o/p stream
4. Send the message, if message sent collision not occurred.
5. If message not sent, collision occurred (To occur collision don’t run not server)
6. Calculate back of time using random number selection and wait for that time
7. Again send the message, if message sent collision not occurred.
8. If message not sent, collision occurred, Again calculate back off time by selecting random number,
this trail can be done for 15 times.
9. If not succeeded with 15 trails transmission will be stopped.
10. Stop the program.
PROGRAM:
//CSMA/CD PROGRAM - SERVER:
import [Link].*;
import [Link].*;
public class server
{
public static void main(String[] args)
{
try
{
[Link]("============ Client 2 ==============");
ServerSocket ss = new ServerSocket(137);
[Link]("Waiting for connection");
Socket con = [Link]();
[Link]("Connected");
ObjectInputStream in = new ObjectInputStream([Link]());
[Link]((String)[Link]());
[Link]();
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}

//CSMA/CD PROGRAM - CLEINT:


import [Link].*;
import [Link].*;
public class client1
{
public static void main(String[] args)
{
try
{
[Link]("============ Client 1 ===============");
client1 cli = new client1();
int Tp = 2000;
int R = 0;
int Tb = 0;
for(int i=1; i<=15;i++)
{
[Link]("attempt : "+i);
if([Link]() == "sent")
{
break;
}
else
{
R = 2^i-1;
[Link]("Selected Random number :"+R);
Tb = R*Tp;
[Link]("waiting for next attempt with back time
(in seconds): "+Tb);
[Link](Tb);
}
}
}
catch (InterruptedException e)
{
[Link](e);
}
}
String send()
{
String str=null;
try
{
Socket soc = new Socket("localhost",137);
ObjectOutputStream out = new
ObjectOutputStream([Link]());
String msg = "CNLAB";
[Link](msg);
[Link]("Message sent : "+msg);
str = "sent";
}
catch(Exception e)
{
String msg=null;
str = "collision occured";
[Link]("Message sent : "+msg);
}
return str;
}
}

OUTPUT:CLIENT
D:\jdk1.6.0\bin>javac [Link]
D:\jdk1.6.0\bin>java client1
============ Client 1 ===============
attempt : 1
Message sent : null
Selected Random number :2
waiting for next attempt with back time (in seconds): 4000
attempt : 2
Message sent : null
Selected Random number :3
waiting for next attempt with back time (in seconds): 6000
attempt : 3
Message sent : null
Selected Random number :0
waiting for next attempt with back time (in seconds): 0
attempt : 4
Message sent : null
Selected Random number :1
waiting for next attempt with back time (in seconds): 2000
attempt : 5
Message sent : null
Selected Random number :6
waiting for next attempt with back time (in seconds): 12000
attempt : 6
Message sent : CNLAB

D:\jdk1.6.0\bin>

SERVER:
D:\jdk1.6.0\bin>javac [Link]
D:\jdk1.6.0\bin>java server
============ Client 2 ===============
Waiting for connection
Connected
CNLAB

D:\jdk1.6.0\bin>

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

Common questions

Powered by AI

The server implementation begins with starting the program and initializing a server socket to wait for client connections . Once a connection is accepted, the server reads the message from the client through an input stream, displays it, and finally closes the connection and associated resources . This process involves setting up listeners for incoming data, making it integral to enabling communication in a CSMA/CD setup .

The client's transmission process involves opening a socket connection to the server, sending a message, and checking for successful transmission. If the message is not sent, a collision is assumed to have occurred . The program then calculates a back-off time using a random number, waits for this period, and attempts to send the message again. This process can be repeated up to 15 times to ensure successful message transmission .

The 'back-off time' in CSMA/CD is the period a device waits before attempting to retransmit after detecting a collision. By using a random exponential back-off time, CSMA/CD reduces the probability of subsequent collisions during simultaneous transmission attempts from multiple devices, thereby enhancing network efficiency . The variability in back-off time ensures that devices do not repeatedly collide, thus optimizing the recovery process after a collision and decreasing overall network congestion .

The CSMA/CD algorithm employs a strategy of exponential back-off, which involves increasing the waiting time exponentially with each subsequent collision attempt. This strategy is designed to reduce the likelihood of repeated collisions by gradually lengthening the delay before retransmission . The algorithm allows for a maximum of 15 attempts to resolve collisions, ensuring that network resources are not indefinitely consumed by failed transmission attempts .

CSMA/CD is based on the principle of 'SENSE BEFORE TRANSMIT', which means that a network device first checks if the communication channel is free before sending data . This principle is important in wired networks to avoid data collisions, ensuring efficient and successful data transmission. If a collision is detected, the transmission is stopped, and the device attempts to resend the data following a calculated back-off period .

The collision detection mechanism in a CSMA/CD network parallels real-world traffic management strategies, such as traffic signals at intersections, which prevent vehicles from colliding. Just as cars wait their turn to avoid traffic congestion, network devices sense when the channel is busy and wait for their opportunity to transmit. Similarly, in case of a collision, both systems employ a form of back-off – in traffic, this could be represented by a traffic light turning red, allowing time for congestion to clear .

Implementing a CSMA/CD program in Java provides a practical illustration of collision management by simulating the behavior of network devices during data transmission. The Java implementation demonstrates the principle of 'SENSE BEFORE TRANSMIT', collides detection, random exponential back-off calculation, and collision handling across multiple retransmission attempts. Such a program highlights how theoretical concepts of collision management operate within real-world network communication systems .

In the CSMA/CD client program, the 'selected random number' determines the back-off period between retransmission attempts. It is computed using the formula 2^i-1, where i represents the number of the current attempt . This calculation introduces a random delay, thus reducing the probability of repeated collisions by preventing simultaneous retransmission attempts from multiple clients .

Closing server resources like sockets after communication is crucial to freeing up system resources and preventing leaks, maintaining network stability, and ensuring efficient resource management . Failure to close these resources can lead to resource exhaustion, resulting in server unavailability, increased latency, and degraded network performance, potentially causing system crashes or data loss in the CSMA/CD network application .

Upon detecting a collision, the client in the CSMA/CD algorithm waits for a random back-off time before attempting to retransmit the message. The process involves calculating the back-off time using a random number (R), which is determined by 2^i-1, where i is the attempt number . The algorithm allows up to 15 attempts to send the message successfully. If a message is not sent after these attempts, the transmission is stopped .

You might also like