You are on page 1of 25

TCP SOCKETS TCP Server:

import java.io.*; import java.net.*; class TCPSimpleserver { public static void main(String args[])throws Exception { ServerSocket serversocket=new ServerSocket(1234); while(true) { Socket s=serversocket.accept(); BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream())); String str1; str1=br1.readLine(); System.out.println(str1); } } }

TCP Client:
import java.io.*; import java.net.*; class TCPSimpleclient { public static void main(String args[])throws Exception { Socket s1=new Socket("",1234); BufferedReader br1=new BufferedReader(new InputStreamReader(System.in)); DataOutputStream ds1=new DataOutputStream(s1.getOutputStream()); String str1; str1=br1.readLine(); ds1.writeBytes(str1+"\n"); s1.close(); } }

OUTPUT: TCP Server:

TCP Client:

UDP SOCKETS UDP Server:


import java.io.*; import java.net.*; class udpsimpleserver { public static void main(String args[])throws Exception { DatagramSocket ds1=new DatagramSocket(1234); byte in[]=new byte[1024]; while(true) { DatagramPacket dp2=new DatagramPacket(in,in.length); ds1.receive(dp2); String str1=new String(dp2.getData()); System.out.println(str1); } } }

UDP Client:
import java.io.*; import java.net.*; class udpsimpleclient { public static void main(String args[])throws Exception { BufferedReader br1=new BufferedReader(new InputStreamReader(System.in)); DatagramSocket ds=new DatagramSocket(); InetAddress ia=InetAddress.getByName(""); byte out[]=new byte[1024]; String str1=br1.readLine(); out=str1.getBytes(); DatagramPacket p1=new DatagramPacket(out,out.length,ia,1234); ds.send(p1); ds.close(); } }

OUTPUT: UDP Server:

Client:

APPLICATIONS USING SOCKETS TCP Chat: Server:


import java.io.*; import java.net.*; class tcpchatserver { public static void main(String args[])throws Exception { System.out.println("server started.......Wait"); try { ServerSocket ss1=new ServerSocket(1234); while(true) { Socket a1=ss1.accept(); BufferedReader b1=new BufferedReader(new InputStreamReader(a1.getInputStream())); BufferedReader b2=new BufferedReader(new InputStreamReader(System.in));

DataOutputStream o1=new DataOutputStream(a1.getOutputStream()); String s1,s2; s2=b2.readLine(); o1.writeBytes(s2+"\n"); s1=b1.readLine(); System.out.println(s1); } } catch(Exception e) { System.out.println("error"+e.getMessage()); } } }

Client:
import java.io.*; import java.net.*; class tcpchatclient { public static void main(String args[])throws Exception { System.out.println("start communication"); try { while(true) { Socket sc1=new Socket("",1234); BufferedReader br1=new BufferedReader(new InputStreamReader(System.in)); BufferedReader br2=new BufferedReader(new InputStreamReader(sc1.getInputStream())); DataOutputStream do1=new DataOutputStream(sc1.getOutputStream()); String str1,str2; str1=br1.readLine(); do1.writeBytes(str1+"\n"); str2=br2.readLine();

System.out.println(str2); sc1.close(); } } catch(Exception e) { System.out.println("error"+e.getMessage()); } } }

OUTPUT: TCP Chat: Server:

Client:

SIMULATION OF SLIDING WINDOW PROTOCOL

Server:
import java.lang.System; import java.net.*; import java.io.*; class Swapserver { public static void main(String args[]) { try { BufferedInputStream in; ServerSocket serversocket=new ServerSocket(500); System.out.println("waiting for connection"); Socket client=serversocket.accept(); System.out.println("received req for sending frames"); in=new BufferedInputStream(client.getInputStream()); DataOutputStream out=new DataOutputStream(client.getOutputStream()); int p=in.read();

System.out.println("sending"); for(int i=1;i<=p;++i) { System.out.println("Sending frame number"+i); out.write(i); out.flush(); System.out.println("waiting for ack"); Thread.sleep(5000); int a=in.read(); System.out.println("Recieved ack for frame number "+i+" as " +a); } out.flush(); in.close(); out.close(); client.close(); serversocket.close(); System.out.println("quiting"); } catch(IOException e) { System.out.println(e); } catch(InterruptedException e) { } } }

Client:
import java.lang.System; import java.net.*; import java.io.*; import java.math.*; class Swapclient { public static void main(String a[]) { try

{ InetAddress addr=InetAddress.getByName(""); System.out.println(addr); Socket connection=new Socket(addr,500); DataOutputStream out=new DataOutputStream(connection.getOutputStream()); BufferedInputStream in=new BufferedInputStream(connection.getInputStream()); BufferedInputStream ins=new BufferedInputStream(connection.getInputStream()); BufferedReader b1=new BufferedReader(new InputStreamReader(System.in)); int flag=0; System.out.println("connect"); System.out.println("enter the number of frames to be required"); int c=Integer.parseInt(b1.readLine()); out.write(c); out.flush(); int i,jj=0; while(jj<c) { i=in.read(); System.out.println("Recievd frame no"+i); out.write(i); out.flush(); jj++; } out.flush(); in.close(); ins.close(); System.out.println("quiting"); } catch(Exception e) { System.out.println(e); } } }

OUTPUT: Server:

Client:

ROUTING PROTOCOL
import java.io.*; class rout { static int visit[]=new int[10]; static int dis[]=new int[10]; static int par[]=new int [10]; } class path { int n,sour,des; int a[][]=new int[20][20]; path() { try { InputStreamReader r=new InputStreamReader(System.in); BufferedReader in=new BufferedReader(r); System.out.print("enter the number of nodes:"); String s=in.readLine(); n=Integer.parseInt(s); System.out.println("enter the source code");

String s1=in.readLine(); sour=Integer.parseInt(s1); System.out.print("enter the destination node"); String s2=in.readLine(); des=Integer.parseInt(s2); for(int i=1;i<=n;i++) { rout.visit[i]=0; rout.dis[i]=0; for(int j=1;j<=n;j++) { a[i][j]=0; } } } catch(Exception e) { } } void read() { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(i!=j) { try { InputStreamReader r=new InputStreamReader(System.in); BufferedReader in=new BufferedReader(r); System.out.println("enter the values of "+i+","+j+":"); String s3=in.readLine(); a[i][j]=Integer.parseInt(s3); } catch(Exception e) { System.out.println("exception"+e); } }

} } System.out.println("WEIGHT MATRIX"); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) System.out.print(" "+a[i][j]); System.out.println(" "); } } void choice() { int v=0,loc=0; rout.visit[sour]=1; rout.dis[sour]=0; rout.par[sour]=0; try { while(v<=n) { for(int i=1;i<=n;i++) { if(rout.visit[i]!=0) { for(int j=1;j<=n;j++) { if((a[i][j]!=0) & (rout.visit[j]==0) &( rout.dis[j]>rout.dis[i]+a[i][j]) | (rout.dis[j]==0)) { rout.dis[j]=rout.dis[i]+a[i][j]; rout.par[j]=i; } } } } int min=0,flag=0; for(int i=1;i<=n;i++) { if((rout.visit[i]==0) && (flag==0) && (rout.dis[i]!=0)) { min=rout.dis[i];

loc=i; flag++; } if((rout.dis[i]!=0) && ( min>rout.dis[i]) && (rout.visit[i]==0)) { min=rout.dis[i]; loc=i; } } rout.visit[loc]=1; v++; } } catch(Exception e) { System.out.println("Exception:"+e); } } void display() { int i=1,flag=0; while(i<=n) { if((rout.visit[i]!=0) && (i!=sour) && (i==des)) { System.out.println("Shortest path form "+sour+"to"+i+"with distance"+route.dis[i]); print(i); System.out.println(i); flag++; } i++; } if(flag!=0) { System.out.println("the node "+des+" is reachable from the source node"); } } void print(int i) { if(i!=sour) { i=rout.par[i];

print(i); System.out.println(i+"----->"); } else return; } } class spath { public static void main(String args[]) { path p1=new path(); p1.read(); p1.choice(); p1.display(); } }

OUTPUT:

MULTI-USER CHAT

Server:
import java.io.*; import java.net.*; public class chatserver { static ServerSocket serversocket=null; static Socket clientsocket=null; static clientThread Th[]=new clientThread[10]; public static void main(String args[]) { int portnumber=1234; System.out.println("Server started..."); System.out.println("[press ctrl c to terminate]"); try { serversocket=new ServerSocket(portnumber); } catch(IOException e) { System.out.println("Exception for input/output"); } while(true) { try { clientsocket=serversocket.accept(); for(int i=0;i<=9;i++) { if(Th[i]==null) { (Th[i]=new clientThread(clientsocket,Th)).start(); break; } } } catch(IOException e) {

System.out.println("Exception for input/output"); } } } } class clientThread extends Thread { BufferedReader input=null; PrintStream output=null; Socket clientsocket=null; clientThread Th[]; public clientThread(Socket clientsocket,clientThread[] Th) { this.clientsocket=clientsocket; this.Th=Th; } public void run() { String msg; String username; try { input=new BufferedReader(new InputStreamReader(clientsocket.getInputStream())); output=new PrintStream(clientsocket.getOutputStream()); output.println("What is ur name?enter it-"); username=input.readLine(); output.println(username+" Welcome to chat room"); output.println("to leave chat room type $$"); for(int i=0;i<=9;i++) if(Th[i]!=null&&Th[i]!=this) Th[i].output.println("......a new user arrived in chat room:"+username); while(true) { msg=input.readLine(); if(msg.startsWith("$$")) break; for(int i=0;i<=9;i++) if(Th[i]!=null) Th[i].output.println("<"+username+">"+msg); }

for(int i=0;i<=9;i++) if(Th[i]!=null&&Th[i]!=this) Th[i].output.println("....a user leaving chat room:"+username+"....."); output.println("........."+username+"let the chat room...."); output.println("press control c to return to prompt..."); for(int i=0;i<=9;i++) if(Th[i]==this) Th[i]=null; input.close(); output.close(); clientsocket.close(); } catch(IOException e) { System.out.println("Exception for input/output"); } }} Client: import java.io.*; import java.net.*; public class chatclient implements Runnable { static Socket clientsocket=null; static PrintStream output=null; static BufferedReader input=null; static BufferedReader userinput=null; static boolean Flag=false; public static void main(String[] args) { int portnumber=1234; String host=""; try { clientsocket=new Socket(host,portnumber); userinput=new BufferedReader(new InputStreamReader(System.in)); output=new PrintStream(clientsocket.getOutputStream()); input=new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));

} catch(IOException e) { System.err.println("Exception for input/output"); } if(clientsocket!=null) { try { new Thread(new chatclient()).start(); while(!Flag) { output.println(userinput.readLine()); } output.close(); input.close(); clientsocket.close(); } catch(IOException e) { System.err.println("IOException:"+e); } }} public void run() { String msg; try { while((msg=input.readLine())!=null) System.out.println(msg); Flag=true; } catch(IOException e) { System.err.println("IOException:"+e); } }}

Server:

Client 1:

Client 2:

Client 3:

You might also like