Vous êtes sur la page 1sur 19

EX.

NO:1

SOCKET

PROGRAMMING:

AIM: To write a program in java to implement client and server using sockets. ALGORITHM: 1. Start the program 2. 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(new InputStreamReader(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:

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 connection with="+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.java z:\> java serverTime Server is waiting for Connection. Server is establish the connection with=\192.168.10.32

z:\>javac clientTime.java z:\> 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 DATA INFORMATION 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=new DatagramPacket(a,a.length,InetAddress.getLocalHost(),1600);

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.java Z:\ven>java udpserver Z:\ven>javac udpclient.java Z:\ven>java udpclient Thu 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

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 with corresponding IP address and port number Get any string to the client side console Pass this string to the Server which prints in the console of Server. 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(new InputStreamReader(System.in)); String line; df=DateFormat.getDateInstance(); while(true) { line=keyboard.readLine(); s=df.format(d1);

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.java import 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);

out.flush(); System.out.println("Waiting for next line.."); System.out.println(); }} catch(Exception e) { e.printStackTrace(); } } } Output: Server.java: Z:\ven>java Server Waiting for Client.. Got a Client ! Client sends Hai!! Welcome to CSE Aug 29,2011 Client.java Z:\ven>java Client Heard of socket 192.168.10.36 and port 6666 Hai!! Welcome to CSE Sending this to Server The server sends.. Got ur message Result: Thus a program was written and successfully executed to implement the program using TCP sockets.

Ex.No:3.(b) Date: AIM:

TCP Program Program to Implement Concurrent Server

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 runnable interface

Accept the connection requisition from the client side systems inside the thread Initialize necessary input and output streams for the accepted connection inside the thread Run any number of clients in parallel communicating the same server The server will respond all the clients at the same time Stop the program

PROGRAM: CLIENT1/CLIENT2/ANY CLIENT import 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) {

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 Client Enter the string: Hai From Server:hai

Client2: Z:\ven>java Clientcon Enter the string: hello From Server: hello Client3: Z:\ven>java Clientcon Enter the string: hello From Server: hello Server: Z:\ven>java Server From Client1:hai From Client1:hello From Client1:hello RESULT: Thus a program is written it implement the concurrent server.

Ex.No:4 PROTOCOL Date:

PROGRAM FOR SIMPLE MAIL TRANSFER

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:

//SMTP Client import 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 Server import 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 {

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.java Z:\ven>javac smtpClient.java Z:\ven>java smtpServer Ready signal Received from client Z:\ven>java smtpClient Enter the Form address: venkatarun@hotmail.com Enter the To address: venkatarun@gmail.com Enter the subject: Test mail Enter the message: Hello, this mail is generated as part of the smtp program testing! Quit Message is delivered to server and client quits Z:\ven>Testmail.eml

Result: Thus a program to implement simple mail transfer protocol was written and executed successfully.

Ex.No:5 Date:

PROGRAM TO IMPLEMENT FILE TRANSFER

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 with corresponding IP address and port number Create necessary file streams Get the option from the user to UPLOD/DOWNLOAD and the file name to be transferred If UPLOAD, read each byte from the input sram and write it into the 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=new DataInputStream(cs.getInputStream()); DataOutputStream out=new DataOutputStream(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:");

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

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;

} } else { System.out.println("Enter Valid Option"); } }}}

OUTPUT: Client Z:\ven>java Client A.UPLOAD B.DOWNLOAD ENTER THE OPTION:B Enter the file name: z:\aa.txt Enter the file name to be saved: d:\cc.txt DOWNOADED!!!! Z:\ven>java Client A.UPLOAD B.DOWNLOAD ENTER THE OPTION:B Enter the file name: d:\cc.txt Enter the file name to be saved: z:\aa.txt UPLOADED!!!! File Server: Z:\ven>java FileServer Accessed file:z:\aa.txt Succesfully send File z:\aa.txt UPLOADED!!!!

Result: Thus a program to implement file transfer between client and server was written and executed successfully Ex.No:6 ALGORITHMS IMPLEMENTATION OF CONGESTION

Date:

LEAKY BUCKET ALGORITHM

AIM: To write a java program to implement leaky bucket algorithm for congestion 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<n; i++) { host[i]=in.readLine(); } try { for(i=0; i<5; i++) { bucket[i]=host[i]; } } Catch(ArrayIndexOutofBoundsException e) { for(i=0; i<n; i++) { bucket[i]=host[i]; } } if(n<5)

{ 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<5; i++) { if System.out.println(network[i]); } } OUTPUT: Enter the no.of.packets in the host 9 The data in the host 3 4 5 6 7 8 9 1 2 No.of packets exceeds the network capacity Remaining packets are discarded The packets received in the network 3 4 5 6 7 RESULT: Thus the Leak Bucket Algorithm or congestion control was implemented and executed successfully.

Vous aimerez peut-être aussi