lab manual-cn full (1)
TRANSCRIPT
-
7/31/2019 Lab Manual-CN Full (1)
1/19
EX.NO:1 SOCKET PROGRAMMING:
AIM:To write a program in java to implement client and server using
sockets.
ALGORITHM:
1. Start the program2. Create the necessary input and output streams.3. Create a socket connection from client to the server with
corresponding port number.4. Get the information from the client side console.5. Pass this information to the server which replies back to the client.6. Display the output in the client.7. Stop the program.
PROGRAM:clientTime:
import java.io.*;import java.net.*;class clientTime{public void process(){try{Socket client= new Socket(InetAddress.getLocalHost(),2000);BufferedReader br=new BufferedReader(newInputStreamReader(client.getInputStream()));String time=br.readLine();System.out.println("ServerTime="+time);}catch(Exception e){}}public static void main(String arg[]){clientTime c=new clientTime();c.process();}}
serverTime:
-
7/31/2019 Lab Manual-CN Full (1)
2/19
import java.io.*;import java.util.*;import java.net.*;class serverTime{public void process()
{try
{ServerSocket server=new ServerSocket(2000);System.out.println("Server is waiting for COnnection.....");Socket client=server.accept();System.out.println("Server is establish the connectionwith="+client.getInetAddress());PrintWriter pr= new PrintWriter(client.getOutputStream(),true);while(true){Date d= new Date();String time=d.toString();
pr.println(time);}}catch(Exception e){}}public static void main(String arg[]){serverTime s= new serverTime();s.process();}}
Output:z:\> javac serverTime.javaz:\> java serverTime
Server is waiting for Connection.Server is establish the connection with=\192.168.10.32
-
7/31/2019 Lab Manual-CN Full (1)
3/19
z:\>javac clientTime.javaz:\> java clientTime
ServerTime= wed May 11 17:12:15 GMT 2011
Result:Thus the program to implement the socket was written and executed
successfully.
EX.NO:2 IMPLEMENTATION OF GETTING TIME AND DATAINFORMATION FROM THE SERVER USING UDP
AIM:To write a program in jav a to implement datagram program using UDP
ALGORITHM:
Start the program.
Create necessary input and output ports.
Instantiate the datagram class .
Establish connection between the server and the user.
Receive the data from the server and display .
Stop the program.
PROGRAM:UDPClient:
import java.io.*;import java.net.*;
import java.util.Date;class udpclient{public static void main(String args[])throws Exception{DatagramSocket ds=new DatagramSocket(800);DataInputStream in=new DataInputStream(System.in);byte a[]=new byte[225];byte tt[]=new byte[20];byte tem[]=new byte[20];Date date=new Date();
String str;
str=date.toString();System.out.println(str);a=str.getBytes();
DatagramPacket dp=newDatagramPacket(a,a.length,InetAddress.getLocalHost(),1600);
-
7/31/2019 Lab Manual-CN Full (1)
4/19
ds.send(dp);}}
UDPServer:
import java.io.*;import java.net.*;
class udpserver{public static void main(String args[])throws Exception{DatagramSocket ds=new DatagramSocket(600);byte data[]=new byte[20];byte t[]=new byte[255];byte t1[]=new byte[100];String str;
while(true)
{DatagramPacket dp=new DatagramPacket(t,255);ds.receive(dp);data=dp.getData();str=new String(data);System.out.println(str.substring(8,11)+"_"+ str.substring(0,3)+"_"+str.substring(26,30));}}}
Output:Z:\ven>javac udpserver.javaZ:\ven>java udpserver
Z:\ven>javac udpclient.javaZ:\ven>java udpclientThu sep 08 14:51:39 IST 2011
Result:Thus the program to implement the datagram was written and
executed successfully.
Ex.No:3.(a) TO GET INFORMATION USING TCP
-
7/31/2019 Lab Manual-CN Full (1)
5/19
AIM:To Write java program to implement chatting application.
ALGORITHM:
Start the program
Create necessary input and output streams
Create a socket connection from user 1to user 2 withcorresponding IP address and port number
Get any string to the client side console
Pass this string to the Server which prints in the console ofServer.
Repeat the same until exit command
Stop the program.
PROGRAM:Client.java
import java.net.*;
import java.io.*;import java.lang.*;import java.util.*;import java.text.*;public class Client{public static void main(String args[])throws IOException{int serverport=6666;Date d1=new Date();DateFormat df;
String s;String address="192.168.10.36";try{InetAddress ipAdd=InetAddress.getByName(address);System.out.println("heard of socket"+address+"and port"+serverport);Socket sock=new Socket(ipAdd,serverport);InputStream in=sock.getInputStream();OutputStream out=sock.getOutputStream();DataInputStream din=new DataInputStream(in);DataOutputStream dout=new DataOutputStream(out);
BufferedReader keyboard=new BufferedReader(newInputStreamReader(System.in));String line;df=DateFormat.getDateInstance();while(true){line=keyboard.readLine();s=df.format(d1);
-
7/31/2019 Lab Manual-CN Full (1)
6/19
System.out.println("Sending this to Server");dout.writeUTF(line);dout.writeUTF(s);dout.flush();line=din.readUTF();System.out.println("the Server sends..."+line);}}
catch(Exception e){e.printStackTrace();}}}
Server.javaimport java.net.*;import java.io.*;import java.lang.*;
import java.util.*;import java.text.*;public class Server{public static void main(String args[])throws IOException{int port=6666;try{ServerSocket ss=new ServerSocket(port);System.out.println("waiting for Client..");Socket sock=ss.accept();System.out.println("Got a Client...!");System.out.println();InputStream in=sock.getInputStream();OutputStream out=sock.getOutputStream();DataInputStream din=new DataInputStream(in);DataOutputStream dout=new DataOutputStream(out);String line;String s;String line1="Got ur Message";while(true){line=din.readUTF();s=din.readUTF();System.out.println("Client Sends.."+line);System.out.println(s);System.out.println("My reply is...");dout.writeUTF(line1);
-
7/31/2019 Lab Manual-CN Full (1)
7/19
out.flush();System.out.println("Waiting for next line..");System.out.println();}}catch(Exception e){e.printStackTrace();}
}}
Output:Server.java:Z:\ven>java ServerWaiting for Client..Got a Client !
Client sends Hai!! Welcome to CSEAug 29,2011
Client.javaZ:\ven>java ClientHeard of socket 192.168.10.36 and port 6666Hai!! Welcome to CSESending this to ServerThe server sends.. Got ur message
Result: Thus a program was written and successfully executed toimplement the program using TCP sockets.
Ex.No:3.(b) TCP ProgramDate: Program to Implement Concurrent Server
AIM:To write a program to implement the concept of concurrent
server.
ALGORITHM:
Start the program
Create necessary sockets in the client and server side systems
Initialize a thread in the server side system using runnableinterface
-
7/31/2019 Lab Manual-CN Full (1)
8/19
Accept the connection requisition from the client side systemsinside the thread
Initialize necessary input and output streams for the acceptedconnection inside the thread
Run any number of clients in parallel communicating the sameserver
The server will respond all the clients at the same time
Stop the program
PROGRAM:
CLIENT1/CLIENT2/ANY CLIENTimport java.io.*;import java.net.*;class Client1{public static void main(String arg[]) throws Exception{
String sentence, newsentence, newsentence1;DataInputStream in=new DataInputStream(System.in);Socket cs=new Socket("192.168.10.36",6789);DataInputStream inp=new DataInputStream(cs.getInputStream());DataOutputStream out=new DataOutputStream(cs.getOutputStream());sentence=inp.readLine();System.out.println(sentence);newsentence=in.readLine();out.writeBytes(newsentence+'\n');newsentence1=inp.readLine();System.out.println("From server:"+newsentence1);
cs.close();}}
Server:import java.io.*;import java.net.*;class Server
{public static void main(String arg[])throws Exception{int count=0;Thread t=Thread.currentThread();ServerSocket ss=new ServerSocket(6789);while(true){
-
7/31/2019 Lab Manual-CN Full (1)
9/19
try{Socket s=ss.accept();count++;conserver c=new conserver(s,count);}catch(Exception e){ }
}}}class conserver implements Runnable{Thread t;Socket s;int c;conserver(Socket s1,int count){t=new Thread(this,"Demo");
t.start();s=s1;c=count;}public void run(){try{DataInputStream inp=new DataInputStream(s.getInputStream());DataOutputStream out=new DataOutputStream(s.getOutputStream());String sentence="Enter the String:";String newsentence;out.writeBytes(sentence+'\n');newsentence=inp.readLine();//Thread.sleep(1000);System.out.println("From Client"+c+":"+newsentence);out.writeBytes(newsentence+'\n');}catch(Exception e) { }
}}
OUTPUT:
Client1:Z:\ven>java ClientEnter the string:HaiFrom Server:hai
-
7/31/2019 Lab Manual-CN Full (1)
10/19
Client2:Z:\ven>java ClientconEnter the string:helloFrom Server: helloClient3:Z:\ven>java ClientconEnter the string:
helloFrom Server: helloServer:Z:\ven>java ServerFrom Client1:haiFrom Client1:helloFrom Client1:hello
RESULT:
Thus a program is written it implement the concurrent server.
Ex.No:4 PROGRAM FOR SIMPLE MAIL TRANSFERPROTOCOLDate:
AIM: To write a java program to implement simple mail transfer protocol
ALGORITHM:
Start the program
Establish socket connection between server and client Get the from address, to address, subject of the message and the
message
Transfer message between client and server upto QUIT message
Stop the program
PROGRAM:SmtpClient:
-
7/31/2019 Lab Manual-CN Full (1)
11/19
//SMTP Clientimport java.io.*;import java.net.*;class smtpClient{
public static void main(String args[])throws Exception{Socket s=new Socket("localhost",8080);
DataInputStream dis=new DataInputStream(s.getInputStream());DataInputStream in=new DataInputStream(System.in);PrintStream ps=new PrintStream(s.getOutputStream());ps.println("Ready");System.out.println(dis.readLine());String strFrom=in.readLine();ps.println(strFrom);
System.out.println(dis.readLine());String strTo=in.readLine();ps.println(strTo);System.out.println(dis.readLine());
String strSub=in.readLine();ps.println(strSub);System.out.println(dis.readLine());while(true){String msg=in.readLine();ps.println(msg);
if(msg.equals("Quit")){System.out.println("Message is Delivered to Server &client quits");break;
}}}}StmpServer://SMTP Serverimport java.io.*;import java.net.*;class smtpServer{
public static void main(String args[])throws Exception{ServerSocket ss=new ServerSocket(8080);
Socket s=ss.accept();ServiceClient(s);}public static void ServiceClient(Socket s)throws Exception{
-
7/31/2019 Lab Manual-CN Full (1)
12/19
DataInputStream dis=null;PrintStream ps=null;
dis=new DataInputStream(s.getInputStream());ps=new PrintStream(s.getOutputStream());FileWriter f=new FileWriter("TestMail.eml");String tel=dis.readLine();if(tel.equals("Ready"))System.out.println("Ready Signal Received from Client");
ps.println("Enter the From address:");String from=dis.readLine();f.write("From:"+from+"\n");ps.println("Enter the To address:");String to=dis.readLine();f.write("To:"+to+"\n");ps.println("Enter the Subject:");String sub=dis.readLine();f.write("Subject:"+sub+"\n");ps.println("Enter the Message:");String msg=dis.readLine();
f.write("\n Message:"+msg+"\n");f.close();}}
Output:Z:\ven>javac smtp Server.javaZ:\ven>javac smtpClient.javaZ:\ven>java smtpServerReady signal Received from clientZ:\ven>java smtpClientEnter the Form address:[email protected] the To address:[email protected] the subject:Test mailEnter the message:Hello, this mail is generated as part of the smtp program testing!QuitMessage is delivered to server and client quitsZ:\ven>Testmail.eml
mailto:[email protected]:[email protected]:[email protected]:[email protected] -
7/31/2019 Lab Manual-CN Full (1)
13/19
Result:Thus a program to implement simple mail transfer protocol was
written and executed successfully.
-
7/31/2019 Lab Manual-CN Full (1)
14/19
Ex.No:5 PROGRAM TO IMPLEMENT FILE TRANSFERDate:
AIM: To write a program in java to implement file transfer between client
and server.
ALGORITHM:
Start the program
Create necessary input and output streams
Create a socket connection from user 1to user 2 withcorresponding IP address and port number
Create necessary file streams
Get the option from the user to UPLOD/DOWNLOAD and the filename to be transferred
If UPLOAD, read each byte from the input sram and write it intothe file
Stop the execution
PROGRAM:CLIENT:import java.io.*;import java.net.*;class Client{public static void main(String args[])throws Exception
{long end;String fname,f1name;int size=0;String option;DataInputStream in=new DataInputStream(System.in);Socket cs=new Socket("192.168.10.36",6789);DataInputStream inp=newDataInputStream(cs.getInputStream());DataOutputStream out=newDataOutputStream(cs.getOutputStream());
System.out.println("A.UPLOAD");System.out.println("B.DOWNLOAD");System.out.println("ENTER THE OPTION:");option=in.readLine();out.writeBytes(option+"\n");if(option.compareTo("A")==0){System.out.println("Enter the file name:");
-
7/31/2019 Lab Manual-CN Full (1)
15/19
fname=in.readLine();System.out.println("Enter the file name to be saved in Server:");f1name=in.readLine();out.writeBytes(f1name+'\n');File f=new File(fname);FileInputStream fi=new FileInputStream(fname);size=fi.available();byte b[]=new byte[size];
fi.read(b);fi.close();out.write(b);System.out.println("UPLOADED!!!");}else if(option.compareTo("B")==0){System.out.println("Enter the file name:");fname=in.readLine();System.out.println("Enter the file name to be saved:");f1name=in.readLine();
out.writeBytes(fname+'\n');byte b[]=new byte[400];inp.read(b);File f=new File(f1name);FileOutputStream fo=new FileOutputStream(f1name);fo.write(b);fo.close();System.out.println("DOWNLOADED!!!");}else{System.out.println("Enter Valid Option");}cs.close();end=System.currentTimeMillis();}}
FileServer:import java.io.*;import java.net.*;class Server{
public static void main(String arg[]) throws Exception{String fname,f1name,option;DataInputStream in=new DataInputStream(System.in);ServerSocket ss=new ServerSocket(6789);
-
7/31/2019 Lab Manual-CN Full (1)
16/19
while(true){Socket s=ss.accept();DataInputStream inp=new
DataInputStream(s.getInputStream());DataOutputStream out=new
DataOutputStream(s.getOutputStream());option=inp.readLine();
if(option.compareTo("A")==0){f1name=inp.readLine();try{File f=new File(f1name);FileOutputStream fo=new FileOutputStream(f);byte b[]=new byte[400];inp.read(b);fo.write(b);fo.close();
System.out.println("File"+f1name+"UPLOADED!!!!!");}
catch(FileNotFoundException e){String excep="Sorry... File Not Present";System.out.println(excep);break;}}else if(option.compareTo("B")==0){fname=inp.readLine();try{File f=new File(fname);InputStream fi=new FileInputStream(f);System.out.println("Accessed file:"+fname);int size=fi.available();byte b[]=new byte[size];fi.read(b);fi.close();out.write(b);System.out.println("Successfully send");}catch(FileNotFoundException e){String excep="Sorry... File Not Present";System.out.println(excep);break;
-
7/31/2019 Lab Manual-CN Full (1)
17/19
}}else{System.out.println("Enter Valid Option");}} } }
OUTPUT:ClientZ:\ven>java ClientA.UPLOADB.DOWNLOADENTER THE OPTION:BEnter the file name:z:\aa.txtEnter the file name to be saved:
d:\cc.txtDOWNOADED!!!!
Z:\ven>java ClientA.UPLOADB.DOWNLOADENTER THE OPTION:BEnter the file name:d:\cc.txtEnter the file name to be saved:z:\aa.txtUPLOADED!!!!
File Server:Z:\ven>java FileServerAccessed file:z:\aa.txtSuccesfully sendFile z:\aa.txt UPLOADED!!!!
Result:Thus a program to implement file transfer between client and server
was written and executed successfully
Ex.No:6 IMPLEMENTATION OF CONGESTIONALGORITHMS
-
7/31/2019 Lab Manual-CN Full (1)
18/19
Date: LEAKY BUCKET ALGORITHM
AIM: To write a java program to implement leaky bucket algorithm forcongestion control in network
ALGORITHM:
Start the program.
Establish socket connection between the client and server Get the number of packets going to be sent over the network
Get the capacity of the network
Send the data over the network
Discard the remaining datas
Stop the program
PROGRAM:import java.io.*;Class class1
{public static void main(String args[])throws Exception{DataInputStream in=new DataInputStream(System.in);String bucket[]=new String[5];String network[]=new String[5];System.out.println("enter the no of packets");int n=Integer.parseInt(in.readLine());String host[]=new String[n]);int i;system.out.println("the data in the host");
for(i=0; i
-
7/31/2019 Lab Manual-CN Full (1)
19/19
{System.out.println("no. of packets exceeds the network capacity");System.out.println("Remaining packets are discarded");}else(n==0)system.out.println("There is no congestion");System.out.println("the packets received in the network");for(i=0; i