Computer Networks Codes
Computer Networks Codes
SERVER
import java.io.";
import java.net.";
public class mts
catch(Exception e)
System.out.printin("error:"+e);
OUTPUT
CLIENT:
Connected with client - IP : 127.0.0.1
Message Received from Client: Networks Lab
RESULT:
Thus the MESSAGE TRANSFER concept is programmed using java is
implemented successfully.
try
{
System.out.println(=== ===Client
=");
1.creating a socket to connect to the senver
Socket con = new Socket("localhost",137);:
System.out.println("Connected with server IP: "+
con.getlnetAddress().getHostAddress();
/2. set Output streams
ObjectOutputStream out = new
ObjectOutputStream(con.getOutputStream);
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 ilp 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 o/p stream
3. Send the message, 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
SERVER:
import java.io. *;
import java.net.*;
public class Server
tIy
Socket con =
ss.accept ()
System. out.println ("Connected") ;
ObjectInputStream i n = new
in.close ()
ss.close ();
catch (Exception e)
try
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;
try
ObjectoutputStream (soc.getoutputstream
S t r i n g msg = "CNLAB";
());
out.write0bject (msg)
System.out.println("Message sent: "+msg);
s t r ="sent" ;
catch (Exception e)
return str;
ENCRYPTION PROGRAM
import java.io.";
import java.net.";
y
System.out.println("====== ENCRYPTION
====s=====);
Socket con = new Socket("localhost",137);
ObjectoutputStream out = new
ObjectOutputStream(con.getOutputStream();
String msg = "CNLAB":
System.out.println("Message: "+ msg);
StringBuilder hex = new StringBuilder();
forintI=0;iemsg.length(0:i++)
hex.appendlnteger.toHexString(int)msg.charAt(0)):
out.writeObject(hex);
System.out.printin("Generated Hex code sent to server: "+
hex);
out.close()
con.close(0
catch(Exception e)
System.out.println('error"+e);
DECRYTION PROGRAM
import java.io.";
import java.net."
public class Decryption
try
System.out.printin("-== == DECRYPTION
ObjectinputStream(con.getlnputStream()):
String hex = in.readObject().toString():
System.out.println("Received hex code is: "+ hex);
System.out.println("Decrypted message:"+hex):
in.close():
ss.close()
catch(Exception e)
{
System.out.println('error:"+e);