Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Computer Networks Codes

Uploaded by

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

Computer Networks Codes

Uploaded by

Aby Antony
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

MESSAGE TRANSFER PROGRAM

SERVER
import java.io.";
import java.net.";
public class mts

public static void main(String args[])


{

System.out.printin("-== ===== Server


== ==");

I1.creating a server socket


ServerSocket ss = new ServerSocket(137):

/2. Wait for connection


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

Socket con = ss.accept();


System.out.println("Connected with client-IP: "+
con.getlnetAddress(). getHostAddress();
13. set Input streams
ObjectlnputStream in = new
ObjectinputStream(con.getinputStream();
14. Read message from input stream and print

String message = (String)in. readObject();


System.out.printin("Message Received from Client: "+
message);
15. Close all objects
in.close()
ss.close):

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.

MESSAGE TRANSFER PROGRAM


CLIENT
import java.io.;
import java.net.";

public class mtc

public static void main(String args|)

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

/3: Communicating with the server


String message = "Networks Lab";
Out.writeObject(message):
System.out.println("Message Sent to Server: "+ message);

14. Close all objects


out.close();
con.close(0:
catch(Exception e)
System.out.printin("error:"+e);
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 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

public static void main (Stringt1 args)

tIy

System. out.println ("== client 2

ServerSocket ss = new Serversocket (137);


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

Socket con =
ss.accept ()
System. out.println ("Connected") ;
ObjectInputStream i n = new

ObjectInputStream (con.getInputStream ())


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

in.close ()
ss.close ();

catch (Exception e)

System. out.println (e);


cSMA/CD PROGRAM
CLEINT:
import java.io.*;
import java.net.*;
public class client1

public static void main (String[] args)

try

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


========
==");
clienti cli = new c l i e n t l ( ) ;
int Tp = 2000;
int R 0;
i n t Tb = 0;

for (int i=1; ik=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;
try

Socket soc new Socket ("localhost ",137)


ObjectoutputStream o u t = new

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)

str = "collision occured"


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

return str;
ENCRYPTION PROGRAM
import java.io.";
import java.net.";

public class Encryption

public static void main(String argsl)

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

public static void main(String args])

try

System.out.printin("-== == DECRYPTION

ServerSocket ss = new ServerSocket(137);


System.outprintin("Waiting for connection");
Socket con = ss.accept();
ObjectinputStream in = new

ObjectinputStream(con.getlnputStream()):
String hex = in.readObject().toString():
System.out.println("Received hex code is: "+ hex);

StringBuilder sb = new StringBuilder();


forfinti = 0 ichex.length(0-1:i+=2)

String output = hex.substring(i,i+2);


int decimal = Integer.parselnt(output,16):
sb.append(char)decimal):

System.out.println("Decrypted message:"+hex):
in.close():
ss.close()
catch(Exception e)
{
System.out.println('error:"+e);

You might also like