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

CSMA CD Program1

Uploaded by

kurmaputejasai5
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

CSMA CD Program1

Uploaded by

kurmaputejasai5
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

IMPLEMENTATION OF CSMA/CD

AIM: To study and implement the Carrier Sense Multiple Access with Collision Detection

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. Initialize server socket


2. Display waiting for connection
3. Initialize the socket and accept the client message
4. Display connected with client
5. Initialize input stream
6. Read message
7. Display message from client
8. Close all objects
9. Stop

CLIENT:
1. Open socket with input address, port
2. Initialize output stream
3. Send the message, if message sent collision not occurred.
4. If message not sent, collision occurred (To occur collision don’t run not server)
5. Calculate back of time using random number selection and wait for that time
6. Again send the message, if message sent collision not occurred.
7. If message not sent, collision occurred, Again calculate back off time by selecting random
number, this trail can be done for 15 times.
8. If not succeeded with 15 trails transmission will be stopped.
//CSMA/CD PROGRAM

//CLEINT:(Sender side)

import java.io.*;

import java.net.*;

public class client1

public static void main(String[] args)

try

System.out.println("============ Client 1 ===============");

client1 cli = new client1();

int Tp = 2000;

int R = 0;

int Tb = 0;

for(int i=1; i<=15;i++)

System.out.println("attempt : "+i);

if(cli.send() == "sent")

break;

else

R = 2^i-1;
System.out.println("Selected Random number :"+R);

Tb = R*Tp;

System.out.println("waiting for next attempt with back time (in seconds): "+Tb);

Thread.sleep(Tb);

catch (InterruptedException e)

System.out.println(e);

String send()

String str=null;

String msg = "CNLAB";

try

Socket soc = new Socket("localhost",1372);

ObjectOutputStream out = new ObjectOutputStream(soc.getOutputStream());

out.writeObject(msg);

System.out.println("Message sent : "+msg);

str = "sent";

}
catch(Exception e)

str = "collision occured";

System.out.println("Message sent : "+msg);

return str;

//CSMA/CD PROGRAM

//SERVER:

import java.io.*;

import java.net.*;

public class Server

public static void main(String[] args)

try

System.out.println("============ Client 2 ===============");

ServerSocket ss = new ServerSocket(1372);

System.out.println("Waiting for connection");

Socket con = ss.accept();

System.out.println("Connected");

ObjectInputStream in = new ObjectInputStream(con.getInputStream());


System.out.println((String)in.readObject());

in.close();

ss.close();

catch(Exception e)

System.out.println(e);

Output:

1. Run the Client program, don’t run server program


2. Client will try to send the message to server, but server is not running, so the attempt is
failed, so consider as collision occurred.
3. Client will select random number and calculate Back off time and wait for the back off time
and again will try to send the message. This loop continues till the 15 trails.
4. Run the server in between consider as no collision, so message will be sent from client to
server.

You might also like