You are on page 1of 67

Client Server TCP/UDP Practicals

List of Practicals
No.
1 2 3

Particulars
Display Hello World using server-client program. Write a client-server program. That will display the IP address & local hostname. Using TCP based client-server communication. 1. Create a server that will calculate the factorial for the client. 2. Create a server that will calculate greatest of 2 nos. 3. Create a server that will verify the prime no. for the client. 4. Create a server that will calculate string length for the client. Using UDP based client-server communication. Create a server that: 1. Will calculate the factorial for the client. 2. Create a server that will calculate greatest of 2 nos. 3. Create a server that will verify the prime no. for the client. 4. Create a server that will calculate string length For the client. Implement a TCP based echo server. 1. On start of the client program. The server should say Hello to client. 2. On start of the client program. The server should display date & time. Implement a TCP based echo server. 1. On start of the client program. The server should say Hello to client. 2. On start of the client program. The server should display date & time. Create a concurrent connection-oriented server, 1. That will calculate the factorial for the client. 2. That will calculate greatest of 2 nos. 3. That will verify the prime no. for the client. 4. That will calculate string length for the client.

Page No.
2 5 8

21

36

42

50

Page No: 1

Client Server TCP/UDP Practicals

Practical No. 1
Aim:
Display Hello World using server-client program.

Server Code:
import java.io.*; import java.net.*; class helloser { Socket S; public static void main(String arg[]) { int port=9999; try{ ServerSocket SS=new ServerSocket(port); Socket S=SS.accept(); BufferedReader br=new BufferedReader(new InputStreamReader(S.getInputStream())); PrintWriter pw=new PrintWriter(new OutputStreamWriter(S.getOutputStream())); pw.println("Hello World"); pw.flush(); System.exit(0); }catch(Exception e){} } }

Page No: 2

Client Server TCP/UDP Practicals

Client Code:
import java.io.*; import java.net.*; class hellocln { public static void main(String arg[]) { int port=9999; Socket s; String str=""; try{ InetAddress add=InetAddress.getByName(null); s=new Socket(add,port); BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream())); str=br1.readLine(); System.out.println(str); }catch(Exception e){} } }

Page No: 3

Client Server TCP/UDP Practicals

Output: Server Output:


C:\Acnprac> set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac helloser.java C:\Acnprac>java helloser C:\Acnprac>

Client Output:
C:\Acnprac> set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac hellocln.java C:\Acnprac>java hellocln Hello World C:\Acnprac>

Page No: 4

Client Server TCP/UDP Practicals

Practical No .2
Aim:
Write a client-server program. That will display the IP address & local hostname.

Server Code:
import java.io.*; import java.net.*; class Ipser { Socket S; public static void main(String arg[]) { int port=9999; try{ ServerSocket SS=new ServerSocket(port); Socket S=SS.accept(); BufferedReader br=new BufferedReader(new InputStreamReader(S.getInputStream())); PrintWriter pw=new PrintWriter(new OutputStreamWriter(S.getOutputStream())); InetAddress a= InetAddress.getLocalHost(); pw.println(a.toString()); pw.flush(); System.exit(0); }catch(Exception e){} } }

Client Code:

Page No: 5

Client Server TCP/UDP Practicals import java.io.*; import java.net.*; class Ipcln { public static void main(String arg[]) { int port=9999; Socket s; String str=""; try{ InetAddress add=InetAddress.getByName(null); s=new Socket(add,port); BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream())); str=br1.readLine(); System.out.println(str); }catch(Exception e){} } }

Output:
Page No: 6

Client Server TCP/UDP Practicals

Server Output:
C:\Acnprac> set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac Ipser.java C:\Acnprac>java Ipser C:\Acnprac>

Client Output:
C:\Acnprac> set path=C:\Program Files\Java\jdk1.5.0_08\bin
C:\Acnprac>javac Ipcln.java C:\Acnprac>java Ipcln insideserver/172.16.1.180 C:\Acnprac>

Page No: 7

Client Server TCP/UDP Practicals

Practical N.3

Aim:

Using TCP based client-server communication. a) Create a server that will calculate the factorial for the client. b) Create a server that will calculate greatest of 2 nos. c) Create a server that will verify the prime no. for the client. d) Create a server that will calculate string length for the client.

Page No: 8

Client Server TCP/UDP Practicals

Practical no .3A
Aim:
Using TCP based client-server communication Create a server that will calculate the factorial for the client.

Source Code: Server Code:


import java.io.*; import java.net.*; class factser { Socket S; public static void main(String arg[]) { int port=9999; try{ ServerSocket SS=new ServerSocket(port); Socket S=SS.accept(); BufferedReader br=new BufferedReader(new InputStreamReader(S.getInputStream())); PrintWriter pw=new PrintWriter(new OutputStreamWriter(S.getOutputStream())); String str; int num,fact=1,i; while(true){ str=br.readLine(); num=Integer.parseInt(str); for(i=1;i<=num;i++) { fact=fact*i; } pw.println("Factorial: "+fact); pw.flush(); } }catch(Exception e){} } }

Page No: 9

Client Server TCP/UDP Practicals

Client Code:
import java.io.*; import java.net.*; class factcln { public static void main(String arg[]) { int port=9999; Socket s; String str=""; try{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); InetAddress add=InetAddress.getByName(null); s=new Socket(add,port); PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream())); BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream())); String msg=br.readLine(); pw.println(msg); pw.flush(); str=br1.readLine(); System.out.println(str); }catch(Exception e){} } }

Page No: 10

Client Server TCP/UDP Practicals

Ouptut : Server Ouput:


C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac factser.java C:\Acnprac>java factser C:\Acnprac>

Client Output:
C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac factcln.java C:\Acnprac>java factcln 5 Factorial: 120 C:\Acnprac>

Page No: 11

Client Server TCP/UDP Practicals

Practical No.3B
Aim:
Using TCP based client-server communication Create a server that will calculate greatest of 2 nos.

Source Code: Server Code:


import java.io.*; import java.net.*; class larges { Socket S; public static void main(String arg[]) { int port=9999; try{ ServerSocket SS=new ServerSocket(port); Socket S=SS.accept(); BufferedReader br=new BufferedReader(new InputStreamReader(S.getInputStream())); PrintWriter pw=new PrintWriter(new OutputStreamWriter(S.getOutputStream())); String str; int i=0; int a[]= new int [2]; while (i<2) { str= br.readLine(); a[i]= Integer.parseInt(str); i++; } int max= a[0]; for (i=1;i<2;i++) { if (max<a[i]) max= a[i]; } pw.println("Maximum No = "+max); pw.flush(); }catch(Exception e){} } } Page No: 12

Client Server TCP/UDP Practicals

Client Code:
import java.io.*; import java.net.*; class largec { public static void main(String arg[]) { int port=9999; Socket s; String str=""; try{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); InetAddress add=InetAddress.getByName(null); s=new Socket(add,port); PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream())); BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream())); int num[]=new int[2]; for(int i=0;i<2;i++) { String msg=br.readLine(); num[i]=Integer.parseInt(msg); pw.println(msg); pw.flush(); } str=br1.readLine(); System.out.println(str); }catch(Exception e){} } }

Page No: 13

Client Server TCP/UDP Practicals

Output: Server Output:


C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac larges.java C:\Acnprac>java larges C:\Acnprac>

Client Output:
C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac largec.java C:\Acnprac>java largec 5 8 Maximum No = 8 C:\Acnprac>

Page No: 14

Client Server TCP/UDP Practicals

Practical No.3C Aim:


Using TCP based client-server communication Create a server that will verify the prime no. for the client.

Source Code: Server Code:


import java.io.*; import java.net.*; class primeser { Socket S; public static void main(String arg[]) { int port=9999; try{ ServerSocket SS=new ServerSocket(port); Socket S=SS.accept(); BufferedReader br=new BufferedReader(new InputStreamReader(S.getInputStream())); PrintWriter pw=new PrintWriter(new OutputStreamWriter(S.getOutputStream())); String str; int num,i; while(true){ str=br.readLine(); num=Integer.parseInt(str); for(i=2;i<=num-1;i++) { if(num%i==0) { pw.println("Not Prime"); break; } } if(i==num) pw.println("Prime"); pw.flush(); } }catch(Exception e){}

Page No: 15

Client Server TCP/UDP Practicals }}

Client Code:
import java.io.*; import java.net.*; class primecln { public static void main(String arg[]) { int port=9999; Socket s; String str=""; try{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); InetAddress add=InetAddress.getByName(null); s=new Socket(add,port); PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream())); BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream())); String msg=br.readLine(); pw.println(msg); pw.flush(); str=br1.readLine(); System.out.println(str); }catch(Exception e){} } }

Page No: 16

Client Server TCP/UDP Practicals

Output: Server code:


C:\Acnprac> set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac primeser.java C:\Acnprac>java primeser C:\Acnprac>

Client Code:
C:\Acnprac> set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac primecln.java C:\Acnprac>java primecln 5 Prime C:\Acnprac>

Page No: 17

Client Server TCP/UDP Practicals

Practical No .3D
Aim:
Using TCP based client-server communication Create a server that will calculate string length for the client.

Source Code: Server code:


import java.io.*; import java.net.*; class strings { Socket S; public static void main(String arg[]) { int port=9999; try{ ServerSocket SS=new ServerSocket(port); Socket S=SS.accept(); BufferedReader br=new BufferedReader(new InputStreamReader(S.getInputStream())); PrintWriter pw=new PrintWriter(new OutputStreamWriter(S.getOutputStream())); String str; int num,len,i; while(true){ str=br.readLine(); len=str.length(); pw.println("Length: "+len); pw.flush(); } }catch(Exception e){} } } Page No: 18

Client Server TCP/UDP Practicals

Client Code:
import java.io.*; import java.net.*; class stringc { public static void main(String arg[]) { int port=9999; Socket s; String str=""; try{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); InetAddress add=InetAddress.getByName(null); s=new Socket(add,port); PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream())); BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream())); String msg=br.readLine(); pw.println(msg); pw.flush(); str=br1.readLine(); System.out.println(str); }catch(Exception e){} } }

Page No: 19

Client Server TCP/UDP Practicals

Output: Server Output:


C:\Acnprac> set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac> javac strings.java C:\Acnprac>java strings C:\Acnprac>

Client Code:
C:\Acnprac> set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac stringc.java C:\Acnprac>java stringc keep smiling!!!! Length: 16 C:\Acnprac>

Page No: 20

Client Server TCP/UDP Practicals

Practical No. 4
Aim:
Using UDP based client-server communication. a) Create a server that will calculate the factorial for the client. b) Create a server that will calculate greatest of 2 nos. c) Create a server that will verify the prime no. for the client. d) Create a server that will calculate string length For the client.

Page No: 21

Client Server TCP/UDP Practicals

Practical No.4A
Aim:
Using UDP based client-server communication. Create a server that will calculate the factorial for the client

Source Code: Server Code:


import java.io.*; import java.net.*; class udpfacts { public static DatagramSocket ds; public static byte bufferRec[]=new byte[1024]; public static byte bufferSend[]=new byte[1024]; public static void main(String arg[]) throws Exception { int fact,i,num; ds= new DatagramSocket(800); while(true) { DatagramPacket rp = new DatagramPacket (bufferRec, bufferRec.length); ds.receive(rp); String s= new String(rp.getData(),0,rp.getLength()); num=Integer.parseInt(s); fact=1; for(i=1;i<=num;i++) fact = fact * i; s=Long.toString(fact); bufferSend=s.getBytes(); InetAddress ip= InetAddress.getByName(null); DatagramPacket sp= new DatagramPacket(bufferSend, s.length(), ip,801); ds.send(sp); Page No: 22

Client Server TCP/UDP Practicals } } }

Client Code:
import java.io.*; import java.net.*; class udpfactc { public static public static public static public static {

DatagramSocket ds; DatagramPacket rp; DatagramPacket sp; void main(String arg[]) throws Exception

byte bufferRec[]=new byte[1024]; byte bufferSend[]=new byte[1024]; ds= new DatagramSocket(801); InetAddress ip= InetAddress.getByName(null); BufferedReader b= new BufferedReader(new InputStreamReader(System.in)); String num =b.readLine(); bufferSend=num.getBytes(); sp= new DatagramPacket (bufferSend, num.length(), ip,800); ds.send(sp); rp=new DatagramPacket(bufferRec,bufferRec.length); ds.receive(rp); String fact=new String(rp.getData(),0,rp.getLength()); System.out.println("Factorial="+fact); } }

Page No: 23

Client Server TCP/UDP Practicals

Output: Server Code:


C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac udpfacts.java C:\Acnprac>java udpfacts

Client Code:
C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac udpfactc.java C:\Acnprac>java udpfactc 9 Factorial=362880 C:\Acnprac>

Page No: 24

Client Server TCP/UDP Practicals

Practical No .4B Aim:


Using UDP based client-server communication and creates a server that will calculate greatest of 2 nos.

Source Code: Server Code:


import java.io.*; import java.net.*; class udplarges { public static DatagramSocket ds; public static byte bufferRec[]=new byte[1024]; public static byte bufferSend[]=new byte[1024]; public static void main(String arg[]) throws Exception { int num1,num2; String s=""; ds= new DatagramSocket(800); while(true) { DatagramPacket rp1 = new DatagramPacket (bufferRec, bufferRec.length); ds.receive(rp1); String s1= new String(rp1.getData(),0,rp1.getLength()); num1=Integer.parseInt(s1); DatagramPacket rp2 = new DatagramPacket (bufferRec, bufferRec.length); ds.receive(rp2); String s2= new String(rp2.getData(),0,rp2.getLength()); num2=Integer.parseInt(s2);

Page No: 25

Client Server TCP/UDP Practicals if(num1>num2) s=Long.toString(num1); else s=Long.toString(num2); bufferSend=s.getBytes(); InetAddress ip= InetAddress.getByName(null); DatagramPacket sp= new DatagramPacket(bufferSend, s.length(), ip,801); ds.send(sp); } } }

Page No: 26

Client Server TCP/UDP Practicals

Client Code:
import java.io.*; import java.net.*; class udplargec { public static public static public static public static public static {

DatagramSocket ds; DatagramPacket rp; DatagramPacket sp1; DatagramPacket sp2; void main(String arg[]) throws Exception

byte bufferRec[]=new byte[1024]; byte bufferSend[]=new byte[1024]; ds= new DatagramSocket(801); InetAddress ip= InetAddress.getByName(null); BufferedReader b= new BufferedReader(new InputStreamReader(System.in)); String num1 =b.readLine(); bufferSend=num1.getBytes(); sp1= new DatagramPacket (bufferSend, num1.length(), ip,800); ds.send(sp1); String num2 =b.readLine(); bufferSend=num2.getBytes(); sp2= new DatagramPacket (bufferSend, num2.length(), ip,800); ds.send(sp2); rp=new DatagramPacket(bufferRec,bufferRec.length); ds.receive(rp); String s=new String(rp.getData(),0,rp.getLength()); System.out.println("Largest no.="+s); } }

Page No: 27

Client Server TCP/UDP Practicals

Outputs: Server Code:


C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac udplargec.java C:\Acnprac>java udplargec 5 8 Largest no.=8 C:\Acnprac>

Client Code:
C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac udplargec.java C:\Acnprac>java udplargec 5 8 Largest no.=8 C:\Acnprac>

Page No: 28

Client Server TCP/UDP Practicals

Practical No. 4C
Aim:
Using UDP based client-server communication and creates a server that will verify the prime no. for the client.

Source Code: Server Code:


import java.io.*; import java.net.*; class udpprimes { public static DatagramSocket ds; public static byte bufferRec[]=new byte[1024]; public static byte bufferSend[]=new byte[1024]; public static void main(String arg[]) throws Exception { int i,num; String res=""; ds= new DatagramSocket(800); while(true) { DatagramPacket rp = new DatagramPacket (bufferRec, bufferRec.length); ds.receive(rp); String s= new String(rp.getData(),0,rp.getLength()); num=Integer.parseInt(s); for(i=2;i<=num-1;i++) { if(num%i==0) { res=" not a Prime No"; break; } } if(i==num) res="Prime No"; bufferSend=res.getBytes();

Page No: 29

Client Server TCP/UDP Practicals

InetAddress ip= InetAddress.getByName(null); DatagramPacket sp= new DatagramPacket(bufferSend, res.length(), ip,801); ds.send(sp); } } }

Page No: 30

Client Server TCP/UDP Practicals

Client code :
import java.io.*; import java.net.*; class udpprimec { public static DatagramSocket ds; public static DatagramPacket rp; public static DatagramPacket sp; public static void main(String arg[]) throws Exception { int prime; byte bufferRec[]=new byte[1024]; byte bufferSend[]=new byte[1024]; ds= new DatagramSocket(801); InetAddress ip= InetAddress.getByName(null); BufferedReader b= new BufferedReader(new InputStreamReader(System.in)); String num =b.readLine(); bufferSend=num.getBytes(); sp= new DatagramPacket (bufferSend, num.length(), ip,800); ds.send(sp); rp=new DatagramPacket(bufferRec,bufferRec.length); ds.receive(rp); String s=new String(rp.getData(),0,rp.getLength()); System.out.println(s); } }

Page No: 31

Client Server TCP/UDP Practicals

Output: Server Code :


C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac> javac udpprimes.java C:\Acnprac>java udpprimes

Client Code :
C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac udpprimec.java C:\Acnprac>java udpprimec 9 not a Prime No C:\Acnprac>

Page No: 32

Client Server TCP/UDP Practicals

Practical No. 4D
Aim :
Using UDP based client-server communication and creates a server that will calculate string length for the client.

Source Code: Server Code:


import java.io.*; import java.net.*; class udpstrlens { public static DatagramSocket ds; public static byte bufferRec[]=new byte[1024]; public static byte bufferSend[]=new byte[1024]; public static void main(String arg[]) throws Exception { int len; ds= new DatagramSocket(800); while(true) { DatagramPacket rp = new DatagramPacket (bufferRec, bufferRec.length); ds.receive(rp); String s= new String(rp.getData(),0,rp.getLength()); len=s.length(); s=Long.toString(len); bufferSend=s.getBytes(); InetAddress ip= InetAddress.getByName(null); DatagramPacket sp= new DatagramPacket(bufferSend, s.length(), ip,801); ds.send(sp); } } }

Page No: 33

Client Server TCP/UDP Practicals

Client Code:
import java.io.*; import java.net.*; class udpstrlenc { public static public static public static public static {

DatagramSocket ds; DatagramPacket rp; DatagramPacket sp; void main(String arg[]) throws Exception

byte bufferRec[]=new byte[1024]; byte bufferSend[]=new byte[1024]; ds= new DatagramSocket(801); InetAddress ip= InetAddress.getByName(null); BufferedReader b= new BufferedReader(new InputStreamReader(System.in)); String str =b.readLine(); bufferSend=str.getBytes(); sp= new DatagramPacket (bufferSend, str.length(), ip,800); ds.send(sp); rp=new DatagramPacket(bufferRec,bufferRec.length); ds.receive(rp); String len=new String(rp.getData(),0,rp.getLength()); System.out.println("Length="+len); } }

Outputs:
Page No: 34

Client Server TCP/UDP Practicals

Server Code:
C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac udpstrlens.java C:\Acnprac>java udpstrlens

Client Code:
C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac udpstrlenc.java C:\Acnprac>java udpstrlenc be happy!!!!!! Length=14 C:\Acnprac>

Page No: 35

Client Server TCP/UDP Practicals

Practical No.5
Aim:
Implement a TCP based echo server. a) On start of the client program. The server should say Hello to client. b) On start of the client program. The server should display date & time.

Page No: 36

Client Server TCP/UDP Practicals

Practical No.5A
Aim:
Implement a TCP based echo server. c) On start of the client program. The server should say Hello to client.

Source Code: Server Code:


import java.io.*; import java.net.*; class echoser { Socket S; public static void main(String arg[]) { int port=9999; try{ ServerSocket SS=new ServerSocket(port); Socket S=SS.accept(); String str2=""; BufferedReader br=new BufferedReader(new InputStreamReader(S.getInputStream())); PrintWriter pw=new PrintWriter(new OutputStreamWriter(S.getOutputStream())); pw.println("Hello"); pw.flush(); while(true) { str2=br.readLine(); System.out.println(str2); } }catch(Exception e){} } }

Page No: 37

Client Server TCP/UDP Practicals

Client Code:
import java.io.*; import java.net.*; class echocln { public static void main(String arg[]) { int port=9999; Socket s; String str=""; String str1=""; try{ InetAddress add=InetAddress.getByName(null); s=new Socket(add,port); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream())); str=br1.readLine(); System.out.println(str); while(true) { str1=br.readLine(); pw.println(str1); pw.flush(); if(str1.equals("exit")) System.exit(0); } }catch(Exception e){} } }

Page No: 38

Client Server TCP/UDP Practicals

Outputs: Server Output:


C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac echoser.java C:\Acnprac>java echoser hi how ru exit C:\Acnprac> C:\Acnprac>how are u

Client Output:
C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac echocln.java C:\Acnprac>java echocln Hello hi how ru exit C:\Acnprac>

Page No: 39

Client Server TCP/UDP Practicals

Practical No.5B
Aim:
Implement a TCP based echo server ,On start of the client program. The server should display date & time.

Source Code: Server Code:


import java.io.*; import java.net.*; import java.util.Calendar; class echotimes { Socket S; static Calendar c= Calendar.getInstance(); public static void main(String arg[]) { int port=9999; try{ ServerSocket SS=new ServerSocket(port); Socket S=SS.accept(); String str2=""; BufferedReader br=new BufferedReader(new InputStreamReader(S.getInputStream())); PrintWriter pw=new PrintWriter(new OutputStreamWriter(S.getOutputStream())); pw.println("Time: "+c.getTime()); pw.flush(); while(true) { str2=br.readLine(); System.out.println(str2); if(str2.equals("exit")) System.exit(0); } }catch(Exception e){} }

Page No: 40

Client Server TCP/UDP Practicals }

Client Code:
import java.io.*; import java.net.*; class echotimec { public static void main(String arg[]) { int port=9999; Socket s; String str=""; String str1=""; try{ InetAddress add=InetAddress.getByName(null); s=new Socket(add,port); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream())); str=br1.readLine(); System.out.println(str); while(true) { str1=br.readLine(); pw.println(str1); pw.flush(); if(str1.equals("exit")) System.exit(0); } }catch(Exception e){} } }

Page No: 41

Client Server TCP/UDP Practicals

Output: Server Output:


C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac echotimes.java C:\Acnprac>java echotimes

Client Output:
C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac echotimec.java C:\Acnprac>java echotimec Time: Fri Feb 15 16:06:49 GMT+05:30 2008

Page No: 42

Client Server TCP/UDP Practicals

Practical No.6
Aim:
Implement a TCP based echo server. a) On start of the client program. The server should say Hello to client. b) On start of the client program. The server should display date & time.

Page No: 43

Client Server TCP/UDP Practicals

Practical No.6A
Aim:
Implement a TCP based echo server. a) On start of the client program. The server should say Hello to client.

Source Code: Server Code:


import java.io.*; import java.net.*; class UDPHelloServer { public static void main(String argv[]) throws Exception { DatagramSocket ds= new DatagramSocket(6000); byte []bufferSend = new byte[102]; byte []bufferRec = new byte[102]; DatagramPacket rp= new DatagramPacket(bufferRec,bufferRec.length); ds.receive(rp); String s= new String(rp.getData()); String m= "Hello"; bufferSend = m.getBytes(); InetAddress IP =InetAddress.getByName(null); DatagramPacket sp= new DatagramPacket(bufferSend,m.length(),IP,6001); ds.send(sp); while(true) { DatagramPacket rp1= new DatagramPacket(bufferRec,bufferRec.length); ds.receive(rp1); String c = new String(rp1.getData(),0,rp1.getLength()); System.out.println(c); if(c.equals("exit")) System.exit(0); } Page No: 44

Client Server TCP/UDP Practicals } }

Client Code:
import java.io.*; import java.net.*; class UDPHelloClient { public static void main(String argv[]) throws Exception { DatagramSocket ds= new DatagramSocket(6001); byte []bufferSend = new byte[102]; byte []bufferRec = new byte[102]; String s = ""; InetAddress ip = InetAddress.getByName(null); bufferSend = s.getBytes(); DatagramPacket sp= new DatagramPacket(bufferSend,s.length(),ip,6000); ds.send(sp); DatagramPacket rp= new DatagramPacket(bufferRec,bufferRec.length); ds.receive(rp); String msg = new String(rp.getData()); System.out.println(msg); while(true) { String str; BufferedReader br=new BufferedReader( new InputStreamReader(System.in) ); str = br.readLine(); bufferSend = str.getBytes(); DatagramPacket sp1=new DatagramPacket(bufferSend,str.length(),ip,6000); ds.send(sp1); if(str.equals("exit")) System.exit(0); } } }

Page No: 45

Client Server TCP/UDP Practicals

Output: Server Code:


C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac UDPHelloServer.java C:\Acnprac>java UDPHelloServer hi exit C:\Acnprac>

Client Code:
C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac UDPHelloClient.java C:\Acnprac>java UDPHelloClient Hello hi exit C:\Acnprac>

Page No: 46

Client Server TCP/UDP Practicals

Practical No.6B
Aim:
Implement a TCP based echo server. On start of the client program. The server should Display date & time.

Source Code: Server Code:


import java.io.*; import java.net.*; import java.util.Calendar; class UDPTimeSer { public static void main(String argv[]) throws Exception { DatagramSocket ds= new DatagramSocket(6000); byte []bufferSend = new byte[102]; byte []bufferRec = new byte[102]; Calendar calendar = Calendar.getInstance(); DatagramPacket rp= new DatagramPacket(bufferRec,bufferRec.length); ds.receive(rp); String s= new String(rp.getData()); String m= calendar.get(Calendar.HOUR) +":"+calendar.get(Calendar.MINUTE) +":"+calendar.get(Calendar.SECOND); bufferSend = m.getBytes(); InetAddress IP =InetAddress.getByName(null); DatagramPacket sp= new DatagramPacket(bufferSend,m.length(),IP,6001); ds.send(sp); while(true)

Page No: 47

Client Server TCP/UDP Practicals { DatagramPacket rp1= new DatagramPacket(bufferRec,bufferRec.length); ds.receive(rp1); String c = new String(rp1.getData(),0,rp1.getLength()); System.out.println(c); if(c.equals("exit")) System.exit(0); } } }

Page No: 48

Client Server TCP/UDP Practicals

Client Code:
import java.io.*; import java.net.*; class UDPTimeCln { public static void main(String argv[]) throws Exception { DatagramSocket ds= new DatagramSocket(6001); byte []bufferSend = new byte[102]; byte []bufferRec = new byte[102]; String s = ""; InetAddress ip = InetAddress.getByName(null); bufferSend = s.getBytes(); DatagramPacket sp= new DatagramPacket(bufferSend,s.length(),ip,6000); ds.send(sp); DatagramPacket rp= new DatagramPacket(bufferRec,bufferRec.length); ds.receive(rp); String msg = new String(rp.getData()); System.out.println(msg); while(true) { String str; BufferedReader br=new BufferedReader( new InputStreamReader(System.in) ); str = br.readLine(); bufferSend = str.getBytes(); DatagramPacket sp1= new DatagramPacket(bufferSend,str.length(),ip,6000); ds.send(sp1); if(str.equals("exit")) System.exit(0); } }

Page No: 49

Client Server TCP/UDP Practicals

Output: Server Code:


C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac UDPTimeSer.java C:\Acnprac>java UDPTimeSer hello hi exit C:\Acnprac>

Client Code:
C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac UDPTimeCln.java C:\Acnprac>java UDPTimeCln 1:38:32 hello hi exit C:\Acnprac>

Page No: 50

Client Server TCP/UDP Practicals

Practical No.7
Aim:
Create e) f) g) h) a concurrent connection-oriented server, That will calculate the factorial for the client. That will calculate greatest of 2 nos. That will verify the prime no. for the client. That will calculate string length for the client.

Page No: 51

Client Server TCP/UDP Practicals

Practical No .7A
Aim:
Create a concurrent connection-oriented server that will calculate the factorial for the client.

Source Code: Server Code:


import java.io.*; import java.net.*; class confactserver implements Runnable { Socket S; public static void main(String arg[]) { int port=9999; try{ ServerSocket SS=new ServerSocket(port); while(true) { Socket S=SS.accept(); confactserver ser=new confactserver(S); Thread t=new Thread(ser); t.start(); } }catch(Exception e){} } confactserver(Socket S) { this.S=S; } public void run() { try{ BufferedReader br=new BufferedReader(new InputStreamReader(S.getInputStream())); PrintWriter pw=new PrintWriter(new OutputStreamWriter(S.getOutputStream())); String str; int num,fact=1,i; while(true){ str=br.readLine(); num=Integer.parseInt(str); for(i=1;i<=num;i++) Page No: 52

Client Server TCP/UDP Practicals { fact=fact*i; } pw.println("Factorial: "+fact); pw.flush(); } }catch(Exception e){} } }

Page No: 53

Client Server TCP/UDP Practicals

Client Code:
import java.io.*; import java.net.*; class confactclnt { public static void main(String arg[]) { int port=9999; Socket s; String str=""; try{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); InetAddress add=InetAddress.getByName(null); s=new Socket(add,port); PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream())); BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream())); String msg=br.readLine(); pw.println(msg); pw.flush(); str=br1.readLine(); System.out.println(str); }catch(Exception e){} }}

Page No: 54

Client Server TCP/UDP Practicals

Output: Server Output:


C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac confactserver.java C:\Acnprac>java confactserver

Client1 Output:
C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac confactclnt.java C:\Acnprac>java confactclnt 3 Factorial: 6 C:\Acnprac>

Client2 Output:
C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac confactclnt.java C:\Acnprac>java confactclnt 5 Factorial: 120 C:\Acnprac>

Page No: 55

Client Server TCP/UDP Practicals

Practical no.7B
Aim:
Create a concurrent connection-oriented server that will calculate greatest of 2 nos.

Source Code: Server Code:


import java.io.*; import java.net.*; class conlarges implements Runnable { Socket S; public static void main(String arg[]) { int port=9999; try{ ServerSocket SS=new ServerSocket(port); while(true) { Socket S=SS.accept(); conlarges ser=new conlarges(S); Thread t=new Thread(ser); t.start(); } }catch(Exception e){} } conlarges(Socket S) { this.S=S; } public void run() { try{ BufferedReader br=new BufferedReader(new InputStreamReader(S.getInputStream())); PrintWriter pw=new PrintWriter(new OutputStreamWriter(S.getOutputStream())); String str; int i=0; int a[]= new int [2]; while (i<2) Page No: 56

Client Server TCP/UDP Practicals { str= br.readLine(); a[i]= Integer.parseInt(str); i++; } int max= a[0]; for (i=1;i<2;i++) { if (max<a[i]) max= a[i]; } pw.println("Maximum No = "+max); pw.flush(); }catch(Exception e){} } }

Page No: 57

Client Server TCP/UDP Practicals

Client Code:
import java.io.*; import java.net.*; class conlargec { public static void main(String arg[]) { int port=9999; Socket s; String str=""; try{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); InetAddress add=InetAddress.getByName(null); s=new Socket(add,port); PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream())); BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream())); int num[]=new int[2]; for(int i=0;i<2;i++) { String msg=br.readLine(); num[i]=Integer.parseInt(msg); pw.println(msg); pw.flush(); } str=br1.readLine(); System.out.println(str); }catch(Exception e){} }}

Page No: 58

Client Server TCP/UDP Practicals

Output: Server Code:


C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac conlarges.java C:\Acnprac>java conlarges

Client1 Output:
C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac conlargec.java C:\Acnprac>java conlargec 10 9 Maximum No = 10 C:\Acnprac>

Client2 Output:
C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac conlargec.java C:\Acnprac>java conlargec 5 8 Maximum No = 8 C:\Acnprac>

Page No: 59

Client Server TCP/UDP Practicals

Practical No.7C
Aim:
Create a concurrent connection-oriented server that will verify the prime no. for the client.

Source Code: Server Code:


import java.io.*; import java.net.*; class conprimeserver implements Runnable { Socket S; public static void main(String arg[]) { int port=9999; try{ ServerSocket SS=new ServerSocket(port); while(true) { Socket S=SS.accept(); conprimeserver ser=new conprimeserver(S); Thread t=new Thread(ser); t.start(); } }catch(Exception e){} } conprimeserver(Socket S) { this.S=S; } public void run() { try{ BufferedReader br=new BufferedReader(new InputStreamReader(S.getInputStream())); PrintWriter pw=new PrintWriter(new OutputStreamWriter(S.getOutputStream())); String str; int num,i; while(true){ str=br.readLine(); Page No: 60

Client Server TCP/UDP Practicals num=Integer.parseInt(str); for(i=2;i<=num-1;i++) { if(num%i==0) { pw.println("Not Prime"); break; } } if(i==num) pw.println("Prime"); pw.flush(); } }catch(Exception e){} } }

Page No: 61

Client Server TCP/UDP Practicals

Client Code:
import java.io.*; import java.net.*; class conprimeclnt { public static void main(String arg[]) { int port=9999; Socket s; String str=""; try{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); InetAddress add=InetAddress.getByName(null); s=new Socket(add,port); PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream())); BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream())); String msg=br.readLine(); pw.println(msg); pw.flush(); str=br1.readLine(); System.out.println(str); }catch(Exception e){} } }

Page No: 62

Client Server TCP/UDP Practicals

Output: Server output:


C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac conprimeserver.java C:\Acnprac>java conprimeserver

Client1 output:
C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac conprimeclnt.java C:\Acnprac>java conprimeclnt 5 Prime C:\Acnprac>

Client2 Output:
C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac conprimeclnt.java C:\Acnprac>java conprimeclnt 8 Not Prime C:\Acnprac>

Page No: 63

Client Server TCP/UDP Practicals

Practical No.7D
Aim:
Create a concurrent connection-oriented server that will calculate string length for the client.

Source Code: Server Code:


import java.io.*; import java.net.*; class constringserver implements Runnable { Socket S; public static void main(String arg[]) { int port=9999; try{ ServerSocket SS=new ServerSocket(port); while(true) { Socket S=SS.accept(); constringserver ser=new constringserver(S); Thread t=new Thread(ser); t.start(); } }catch(Exception e){} } constringserver(Socket S) { this.S=S; } public void run() { try{ BufferedReader br=new BufferedReader(new InputStreamReader(S.getInputStream())); PrintWriter pw=new PrintWriter(new OutputStreamWriter(S.getOutputStream())); String str;

Page No: 64

Client Server TCP/UDP Practicals int num,len,i; while(true){ str=br.readLine(); len=str.length(); pw.println("Length: "+len); pw.flush(); } }catch(Exception e){} } }

Page No: 65

Client Server TCP/UDP Practicals

Client Code:
import java.io.*; import java.net.*; class constringclnt { public static void main(String arg[]) { int port=9999; Socket s; String str=""; try{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); InetAddress add=InetAddress.getByName(null); s=new Socket(add,port); PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream())); BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream())); String msg=br.readLine(); pw.println(msg); pw.flush(); str=br1.readLine(); System.out.println(str); }catch(Exception e){} } }

Page No: 66

Client Server TCP/UDP Practicals

Output: Server Output:


C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac constringserver.java C:\Acnprac>java constringserver

Client1 Output :
C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac constringclnt.java C:\Acnprac>java constringclnt Be happy!!!!!!!!!!!! Length: 21 C:\Acnprac>

Client2 Output:
C:\Acnprac>set path=C:\Program Files\Java\jdk1.5.0_08\bin C:\Acnprac>javac constringclnt.java C:\Acnprac>java constringclnt Keep smiling!!!!!!!!!! Length: 22 C:\Acnprac>

Page No: 67