You are on page 1of 37

MCA Semester 5 -1

-
Nikhil N Kulkarni Roll No : 06 MCG 35
Definition :

Implement client server program when client send integer array to
server, server find max and min and return back to client.

Source code :

maxminserver.java

import java.io.*;
import java.net.*;

class ServerCC
{
ServerSocket ss;
Socket s;
DataInputStream dis;
DataOutputStream dos;
public ServerCC()
{
ss=null;
s=null;
dis=null;
dos=null;
}
public void communicate()
{
int request=0;
String response="Array list";
int number=0;
try
{
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
int arr[];
int len=dis.readInt();
System.out.println("Lenght: "+len);
arr = new int[len];
for(int i = 0;i<len;i++)
{
arr[i] = dis.readInt();
System.out.println("Request From Client Array : "+arr[i]);
}
int min = arr[0], max = arr[0];
for(int i = 0;i<len;i++)
{
if(min>arr[i])
min = arr[i];
if(max<arr[i])
max = arr[i];
}
response = "Minimum of array ="+min+" and Maximum of array= "+max;
dos.writeUTF(response);
}
catch(Exception e)
{
System.out.println("Exception : Socekt Not Created !!! Program
Terminated");
Sardar Vallabhbhai Patel Institute of Technology - Vasad

MCA Semester 5 -2-
Nikhil N Kulkarni Roll No : 06 MCG 35
e.getMessage();
e.printStackTrace();
}
}

public void connection()
{
try
{
ss = new ServerSocket(3000);
System.out.println("Server Started...");
while(true)
{
s = ss.accept();
communicate();
}
}
catch(Exception e)
{
System.out.println("Exception : Socekt Not Created !!! Program
Terminated");
e.getMessage();
e.printStackTrace();
}
}
}

public class maxminserver
{
public static void main(String [] args)
{
try
{
ServerCC s1 = new ServerCC();
s1.connection();
}
catch(Exception e)
{
e.getMessage();
e.printStackTrace();
}
}
}

maxminclient.java :

import java.io.*;
import java.net.*;

class ClientCC
{
Socket s;
DataInputStream dis;
DataOutputStream dos;

public ClientCC()
{
s=null;
Sardar Vallabhbhai Patel Institute of Technology - Vasad

MCA Semester 5 -3-
Nikhil N Kulkarni Roll No : 06 MCG 35
dis=null;
dos=null;
}

public String communicate(int arr[])
{
String reply=null;
try
{
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
dos.writeInt(arr.length);
for(int i=0;i<arr.length;i++)
{
dos.writeInt(arr[i]);
}
reply=dis.readUTF();
s.close();
}
catch(Exception e)
{
System.out.println("Exception : Socekt Not Created !!!
Program Terminated");
e.getMessage();
e.printStackTrace();
}
return reply;
}
public void connection()
{
try
{
s = new Socket("localhost",3000);
System.out.println("Client Connected...");

}
catch(Exception e)
{
System.out.println("Exception : Socekt Not Created !!!
Program Terminated");
e.getMessage();
e.printStackTrace();
}

}
}

public class maxminclient
{
public static void main(String [] args)
{
try
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("Enter A Number of Array length ");
int len = Integer.parseInt(br.readLine());
Sardar Vallabhbhai Patel Institute of Technology - Vasad

connection().printStackTrace().parseInt(br.MCA Semester 5 -4- Nikhil N Kulkarni Roll No : 06 MCG 35 int arr[].out.getMessage(). String response = c1. } catch(Exception e) { e. e. arr=new int[len].Vasad . System.. } } } OUTPUT : D:\SemesterV\NT2\maxmin>javac maxminserver.println("From Server : " + response).java D:\SemesterV\NT2\maxmin>java maxminserver Server Started. From Server : Minimum of array =3 and Maximum of array = 90 Sardar Vallabhbhai Patel Institute of Technology .println("Enter Array Elements ").i++) { arr[i]=Integer.out.communicate(arr)... } ClientCC c1 = new ClientCC().. System.readLine()). Lenght: 7 Request From Client Array : 22 Request From Client Array : 12 Request From Client Array : 3 Request From Client Array : 11 Request From Client Array : 43 Request From Client Array : 90 Request From Client Array : 23 D:\SemesterV\NT2\maxmin>javac maxminclient.i<len. c1. for(int i=0.java D:\SemesterV\NT2\maxmin>java maxminclient Enter A Number of Array length 7 Enter Array Elements 22 12 3 11 43 90 23 Client Connected.

k<arr. DataInputStream dis. try { dis = new DataInputStream(s.getInputStream()).println("Lenght: "+len). for(int k=0.MCA Semester 5 -5- Nikhil N Kulkarni Roll No : 06 MCG 35 Definition : Implement client server program when client send integer array to server. System.readInt().*.Vasad . int len=dis.out. dos=null. Socket s.l<arr.length-k.length. Source code : Server.*. Sardar Vallabhbhai Patel Institute of Technology .println("Request From Client Array : "+arr[i]). Server sort the array based on received option value and send a sorted array back to client. System.i++) { arr[i] = dis.readInt(). arr = new int[len]. DataOutputStream dos. } public void communicate() { int request=0. s=null.out. class SortServer { ServerSocket ss. dos = new DataOutputStream(s.i<len. arr[l]= arr[l-1].k++) { for(int l=1.getOutputStream()). and option of ascending or descending order. String response="". int number=0.readInt(). for(int i = 0. int arr[].l++) { if(arr[l]<arr[l-1]) { temp = arr[l]. import java. dis=null.java : import java. } int stype = dis. public SortServer() { ss=null.io.net. if(stype == 1) { int temp=0.

} } } public class Server { Sardar Vallabhbhai Patel Institute of Technology .printStackTrace().i<len.i++) response += arr[i] + ". arr[l-1] = temp.. } } public void connection() { try { ss = new ServerSocket(3000). e.k++) { for(int l=1.k<arr.length-k.length()-1).length.l++) { if(arr[l]>arr[l-1]) { temp = arr[l].l<arr.out. } catch(Exception e) { System.getMessage(). arr[l]= arr[l-1].".MCA Semester 5 -6- Nikhil N Kulkarni Roll No : 06 MCG 35 arr[l-1] = temp.println("Server Started.substring(0.out. response = response. for(int k=0. while(true) { s = ss.getMessage(). e.out.writeUTF(response).println("Exception : Socekt Not Created !!! Program Terminated").accept(). e. communicate(). dos. e. } } } } for(int i = 0. System."). } } catch(Exception e) { System.response..println("Exception : Socekt Not Created !!! Program Terminated").printStackTrace(). } } } } else{ int temp=0.Vasad .

} dos. } public String communicate(int arr[]. dis=null.printStackTrace(). reply=dis. } } } Client.getInputStream()). s1.getOutputStream()).java : import java.connection().*.getMessage(). class SortClient { Socket s.length.printStackTrace().getMessage().i++) { dos. public SortClient() { s=null.writeInt(stype). dos = new DataOutputStream(s.Vasad . e. DataOutputStream dos. Sardar Vallabhbhai Patel Institute of Technology . e. s. System. try { dis = new DataInputStream(s.net.io.writeInt(arr[i]).println("Exception : Socekt Not Created !!! Program Terminated"). e.int stype) { String reply=null. dos=null. } return reply.writeInt(arr.length).readUTF().*. dos. DataInputStream dis.MCA Semester 5 -7- Nikhil N Kulkarni Roll No : 06 MCG 35 public static void main(String [] args) { try { SortServer s1 = new SortServer().close(). } catch(Exception e) { e.out. } catch(Exception e) { System.println("Enter Array elements ").i<arr. for(int i=0. import java.out.

System. System.println("From Server : Sorted Array : " + response).parseInt(br.println("Client Sending array of lenght: "+ len).out.i++) { arr[i]=Integer.println("PRESS 1 FOR ASCENDING ").out.getMessage().parseInt(br..communicate(arr.readLine()).out.out.parseInt(br. arr=new int[len].in). } catch(Exception e) { System. c1.out. BufferedReader br = new BufferedReader(isr). } Sardar Vallabhbhai Patel Institute of Technology . } catch(Exception e) { e. } } } public class Client { public static void main(String [] args) { try { InputStreamReader isr = new InputStreamReader(System. e. SortClient c1 = new SortClient().println("Enter A Number of Array lenght ").MCA Semester 5 -8- Nikhil N Kulkarni Roll No : 06 MCG 35 } public void connection() { try { s = new Socket("localhost". int arr[].. int len = Integer.readLine()).out.connection().println("PRESS 2 FOR DESCENDING ").println("Exception : Socekt Not Created !!! Program Terminated"). int stype = Integer.println("Client Connected. System.Vasad . System. e.i<len. String response = c1.printStackTrace().readLine()). for(int i=0.3000). } System.printStackTrace().println("Enter Array Elements "). System.").out.out. System.stype). e.getMessage().

2..5.5.3..3..MCA Semester 5 -9- Nikhil N Kulkarni Roll No : 06 MCG 35 } } OUTPUT: D:\Nt\ex_3>java Server Server Started.6 D:\Nt\ex_3>java Client Enter A Number of Array lenght 5 Enter Array Elements 3 2 1 6 5 PRESS 1 FOR ASCENDING PRESS 2 FOR DESCENDING 2 not valid choice Client Sending array of lenght: 5 Client Connected. Enter Array elements From Server : Sorted Array : 6. Enter Array elements From Server : Sorted Array : 1...Vasad ..1 Sardar Vallabhbhai Patel Institute of Technology .2. Lenght: 5 Request From Client Array : 3 Request From Client Array : 2 Request From Client Array : 1 Request From Client Array : 6 Request From Client Array : 5 Lenght: 5 Request From Client Array : 3 Request From Client Array : 2 Request From Client Array : 1 Request From Client Array : 6 Request From Client Array : 5 D:\Nt\ex_3>java Client Enter A Number of Array lenght 5 Enter Array Elements 3 2 1 6 5 PRESS 1 FOR ASCENDING PRESS 2 FOR DESCENDING 1 not valid choice Client Sending array of lenght: 5 Client Connected.

Sardar Vallabhbhai Patel Institute of Technology . int count=0. DataOutputStream dos=new DataOutputStream(os).MCA Semester 5 .io. String str= "". DataInputStream dis=new DataInputStream(is). Source code : FileServer. File f= new File(filenm). InputStream is=s.").writeInt(count). count++. import java.*.*..net.. import java.println("Error..getOutputStream().nextToken()).. import java.out.. s. if(!f.out.println("File does not exists:"). } } } FileClient.net.java : import java. } catch(Exception e) { System.getInputStream().exists()) System.close(). //FileInputStream fis=new FileInputStream(filenm).hasMoreElements()) { System..println(st.out.readLine())!= null) { StringTokenizer st = new StringTokenizer(str).accept(). while(st. public class FileServer { public static void main(String args[]) { try{ ServerSocket ss=new ServerSocket(21).server returns the number of words in that file back to client.Vasad .io. } } OutputStream os=s. BufferedReader br=new BufferedReader(new FileReader(filenm)).10 - Nikhil N Kulkarni Roll No : 06 MCG 35 Definition : Implement client server program where a client sends the text file to server.java : import java.*. Socket s=ss..readUTF(). dos. while((str = br.util.*. String filenm=dis.*.

dos. System. System.in)).util.println("error. } } } OUTPUT : D:\Nt\ex_7>java FileClient Enter file name:name. s.out.11 - Nikhil N Kulkarni Roll No : 06 MCG 35 import java.MCA Semester 5 . DataInputStream dis=new DataInputStream(is). BufferedReader br=new BufferedReader(new InputStreamReader(System.readLine().").close().print("Enter file name:").getInputStream(). public class FileClient { public static void main(String args[]) { try{ Socket s=new Socket("localhost".*. DataOutputStream dos=new DataOutputStream(os). int word=dis.Vasad .out. OutputStream os=s.. String fnm=br.readInt().writeUTF(fnm).out.println("Number of charecter found in file : "+ word ).txt Number of charecter found in file : 7 D:\Nt\ex_7>java FileServer BillGets NarendraModi RatanTata they r from svit Sardar Vallabhbhai Patel Institute of Technology ..21).getOutputStream(). InputStream is=s. } catch(Exception e) { System.

getOutputStream()).*. dos = new DataOutputStream(s. } catch(Exception e) { System.println("Exception : Socekt Not Created !!! Program Terminated"). DataOutputStream dos. } } dos. while((fileData=br. e. } public void communicate() { String request="".MCA Semester 5 .txt BillGates NarendraModi RatanTata Source code : import java.getInputStream()). class ServerCC { ServerSocket ss. String response="Name not found in File". dis=null. try { dis = new DataInputStream(s. BufferedReader br=new BufferedReader(new FileReader("name. s=null.getMessage(). Server matches the string with a list of predefined string stored in a text file accordingly server returns a success or failure message. Socket s.writeUTF(response). dos=null.Vasad .println("Request From Client : "+request).12 - Nikhil N Kulkarni Roll No : 06 MCG 35 Definition : Implement client server program where client sends a string to server. Sardar Vallabhbhai Patel Institute of Technology .out.io.equalsIgnoreCase(request)) { response = "Name found in File". public ServerCC() { ss=null.readLine())!=null) { if(fileData.net. System.txt")). String fileData = "". DataInputStream dis.*.readUTF().out. Name. request=dis. import java.

e.connection().. System.*.out. DataOutputStream dos.13 - Nikhil N Kulkarni Roll No : 06 MCG 35 e. dis=null.getMessage(). } } public void connection() { try { ss = new ServerSocket(3000). communicate().printStackTrace().net. } } } Client.getMessage().println("Exception : Socekt Not Created !!! Program Terminated"). class ClientCC { Socket s. e.accept().MCA Semester 5 . s1. Sardar Vallabhbhai Patel Institute of Technology .java : import java..").printStackTrace(). public ClientCC() { s=null. e. while(true) { s = ss.*.printStackTrace().io. } } catch(Exception e) { System.println("Prime Server Started. DataInputStream dis.Vasad . } catch(Exception e) { e. import java. } } } public class Server { public static void main(String [] args) { try { ServerCC s1 = new ServerCC().out.

} public String communicate(String str) { String reply=null.. e. reply=dis. } catch(Exception e) { System.println("Exception : Socekt Not Created !!! Program Terminated")..printStackTrace().println("Client Connected.in).getMessage().14 - Nikhil N Kulkarni Roll No : 06 MCG 35 dos=null.println("Exception : Socekt Not Created !!! Program Terminated").getInputStream()).MCA Semester 5 .out.printStackTrace().readLine(). String str = br.3000)."). dos = new DataOutputStream(s. BufferedReader br = new BufferedReader(isr). } public void connection() { try { s = new Socket("localhost". try { dis = new DataInputStream(s. e. } catch(Exception e) { System.println("Enter A String ").out. e.out. dos. System.close(). } return reply. e. Sardar Vallabhbhai Patel Institute of Technology .readUTF().out. System. s.writeUTF(str).getOutputStream()).getMessage().Vasad . } } } public class Client { public static void main(String [] args) { try { InputStreamReader isr = new InputStreamReader(System.

printStackTrace(). e..out.communicate(str). String response = c1.getMessage().MCA Semester 5 .15 - Nikhil N Kulkarni Roll No : 06 MCG 35 System. System.connection().... c1. } catch(Exception e) { e.println("From Server : " + response).println("Client Sending : "+ str). ClientCC c1 = new ClientCC(). From Server : Name found in File D:\Nt\ex_8>java Client Enter A String anilambani Client Sending : anilambani Client Connected..out. } } } OUTPUT : D:\Nt\ex_8>java Server Prime Server Started.. From Server : Name not found in File Sardar Vallabhbhai Patel Institute of Technology . Request From Client : ratantata Request From Client : anilambani D:\Nt\ex_8>java Client Enter A String ratantata Client Sending : ratantata Client Connected.Vasad .

println()."). System..out.MCA Semester 5 .println("Waiting for a client.. DataInputStream in = new DataInputStream(sin). InputStream sin = socket. break. ValidIPServer.java : import java.").".*.Vasad . StringTokenizer stk = new StringTokenizer(request.hasMoreTokens()) { int octet = Integer.out. request=(String)in.println("From Client : "+request). System. DataOutputStream out = new DataOutputStream(sout).nextToken()).net. import java. Socket socket = ss. public class ValidIPServer { public static void main(String[] ar) { String request=null.parseInt(stk.flush().16 - Nikhil N Kulkarni Roll No : 06 MCG 35 Definition : Implement a program in which the client sends a particular IP address in dotted decimal notation as a string. import java. } } out. The server should verify whether the IP address is a valid IP address and accordingly send a success or a failure message as string.readUTF(). try { ServerSocket ss = new ServerSocket(port).accept().getOutputStream().*.io.util.*. OutputStream sout = socket.out. Sardar Vallabhbhai Patel Institute of Technology . while(stk. else { response="IP is InValid". if(octet>=0 && octet<=255) response="IP is Valid". int port = 6666. System. String line = null.writeUTF(response).getInputStream(). String response=null. out.

out.net. DataOutputStream out.. in = new DataInputStream(sin). String address = "127. out = new DataOutputStream(sout). System.util.1".flush(). class IPClient { Socket socket.17 - Nikhil N Kulkarni Roll No : 06 MCG 35 while(true) { line = in. System.printStackTrace(). } catch(Exception e) { Sardar Vallabhbhai Patel Institute of Technology . out.println("The received line from client: " + line).flush()..").getInputStream(). System. } } catch(Exception x) { x.java : import java.io. OutputStream sout = socket. } } } ValidIPClient.*.Vasad .println(). out. out.0.println("Waiting for the next line.out.readUTF().getOutputStream(). reply=in.MCA Semester 5 . int serverPort = 6666. import java.out. System.writeUTF(line).0.*.readUTF(). import java.. public String communicate(String data) { String reply=null.out.writeUTF(data).*. DataInputStream in. try { InputStream sin = socket.").println("I'm sending it back. out..

. String ipAddr = br.in).20) : ").MCA Semester 5 . } public void connection() { try { InetAddress ipAddress = InetAddress..readLine(). IPClient c1 = new IPClient().out. e.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.10. System. System.println("Exception : Socekt Not Created !!! Program Terminated").out.println("Client Connected. e.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b")) { System.out.println("Enter IP Address in Decimal Dotted Notation (eg : 192.printStackTrace().println("Exception : Socekt Not Created !!! Program Terminated"). System.getMessage().connection().matches("\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9] [0. } } } public class ValidIPClient { public static void main(String [] args) { try { InputStreamReader isr = new InputStreamReader(System. } return reply. e. if(ipAddr.println("From Server : " + isValidIP).printStackTrace(). socket = new Socket(ipAddress.getByName(address).18 - Nikhil N Kulkarni Roll No : 06 MCG 35 System. serverPort).println("Invalid IP Address").Vasad .getMessage()."). BufferedReader br = new BufferedReader(isr). c1. String isValidIP = c1.out.out.(25[0-5]|2[0-4] [0-9]|[01]?[0-9][0-9]?)\\.communicate(ipAddr).out. } catch(Exception e) { System. e.println("Client Sending : "+ipAddr). Sardar Vallabhbhai Patel Institute of Technology .out.168. 9]?)\\. } else { System.

.12.10.SocketException: Connection reset at java.readUTF(DataInputStream..32.168. From Client : 112.19 - Nikhil N Kulkarni Roll No : 06 MCG 35 } } catch(Exception e) { e.12..java D:\SemesterV\NT2\validip2>java ValidIPClient Enter IP Address in Decimal Dotted Notation (eg : 192.net.12.java:149) D:\SemesterV\NT2\validip2>javac ValidIPClient.32.MCA Semester 5 .32.net.readUTF(DataInputStream.java:168) at java.SocketInputStream.210 Client Sending : 112.Vasad . } } } OUTPUT :==> D:\SemesterV\NT2\validip2>javac ValidIPServer.DataInputStream.net.20) : 112.main(ValidIPServer.java:182) at java.io.readUnsignedShort(DataInputStream.read(SocketInputStream.210 Client Connected.read(SocketInputStream.getMessage().DataInputStream.java:307) at java.io.io.210 java. e.DataInputStream.printStackTrace(). From Server : IP is Valid Sardar Vallabhbhai Patel Institute of Technology .java D:\SemesterV\NT2\validip2>java ValidIPServer Waiting for a client.java:545) at java.java:522) at ValidIPServer..SocketInputStream.

ois = new ObjectInputStream(s. public void runServer() { try { ss = new ServerSocket(port.*.1). else if(cls>=192 && cls <=223) ipClass = new String("Class C IP Address"). int cls = Integer. else if(cls >=224 && cls <=239) ipClass = new String("Class D IP Address").getInetAddress(). Source code : Server. import java. if(cls>=0 && cls<=127) ipClass = new String("Class A IP Address").*. ObjectInputStream ois.out.getHostAddress() + " Connected").println(s.split("\\. The server should return the class of that IP address back to the client as string.writeObject(ipClass). oos.flush().20 - Nikhil N Kulkarni Roll No : 06 MCG 35 Definition : Implement a program in which the client sends a valid IP address in dotted decimal notation as a string.getOutputStream()). Sardar Vallabhbhai Patel Institute of Technology . int port = 6666. String str=new String("").net. s =ss. Socket s.parseInt(ip[0]). class Server1 { ServerSocket ss. oos = new ObjectOutputStream(s.getInputStream()). do { str = (String) ois.").Vasad . else if(cls>=240 && cls<=255) ipClass = new String("Class E IP Address").readObject().io.java : import java. oos. ObjectOutputStream oos.MCA Semester 5 . else if(cls>=128 && cls<=191) ipClass = new String("Class B IP Address"). String ipClass. String ip[] = str. while(true) { System.accept().

close().getOutputStream()). class Client1 { Socket s.getMessage()).io. ois = new ObjectInputStream(s. } } } public class Server { public static void main(String args[]) { try { Server1 ser = new Server1(). Sardar Vallabhbhai Patel Institute of Technology .close().21 - Nikhil N Kulkarni Roll No : 06 MCG 35 }while(! str.out. ois. int port = 6666. } catch(Exception e) { System.Vasad .out.MCA Semester 5 . } } catch(Exception e) { System.equals("TERMINATE")).out.println("Server Closed"). System.getInputStream()).close(). oos. public void runClient(String host) { try { s = new Socket(host.net.java : import java. ObjectInputStream ois.getMessage()). ser. s.*.println(e.port). //String str = new String(""). String ipClass. } } } Client.runServer(). oos = new ObjectOutputStream(s. ObjectOutputStream oos.*. String str = new String("").println(e. import java.

println("Client Terminating").Vasad . //args[0] = ser.flush(). BufferedReader br = new BufferedReader(new InputStreamReader(System. } catch(Exception e) { System.writeObject(str).close(). String cls = (String)ois.close().getMessage()). ois. oos.MCA Semester 5 . oos. s.println(e.out.close(). }while(! str.equals("TERMINATE")).out.out. } } } Sardar Vallabhbhai Patel Institute of Technology .readLine().getMessage()). } } } public class Client { public static void main(String args[]) { try { Client1 ser = new Client1().in)).println("Enter An IP Address : ").str. //ser.22 - Nikhil N Kulkarni Roll No : 06 MCG 35 do { System.runClient("localhost").println(e. str = br. ser.out.runClient(args[0]).println("Server>>"+cls). System. } catch(Exception e) { System. System.out. oos.readObject().

23.44 Server>>Class A IP Address Enter An IP Address : 140.34 Server>>Class B IP Address Enter An IP Address : 244.22.1 Connected D:\SemesterV\NT2\ipclass>java Client Enter An IP Address : 123.33.0.23.122.222 Server>>Class E IP Address Enter An IP Address : Connection reset Sardar Vallabhbhai Patel Institute of Technology .155.0.Vasad .23 - Nikhil N Kulkarni Roll No : 06 MCG 35 OUTPUT : D:\SemesterV\NT2\ipclass>java Server 127.MCA Semester 5 .

byte buffer[] = netAdd.getByName("localhost").getBytes().close(). DatagramPacket dp = new DatagramPacket(buffer. public void send(String netAdd) { try { DatagramSocket ds = new DatagramSocket().println("Received From Client : "+frmClient).length. byte buffer[] = new byte[BUFFSIZE].getData().InetAddress.dp. } catch(Exception e) { System.*. } } public void receive() { try{ DatagramSocket ds = new DatagramSocket(3000).getMessage()).Vasad .out. public class Netidserver { int BUFFSIZE = 1024. String frmClient = new String(dp.println(e. Design client interface using Applet.0.length). The server should return the network address back to the client as string. 5000).java : import java. import java.24 - Nikhil N Kulkarni Roll No : 06 MCG 35 Definition : Implement a program using UDP in which the client sends a valid IP address in dotted decimal notation as a string.send(dp).out. DatagramPacket dp = new DatagramPacket(buffer. ds.buffer.buffer.receive(dp). Source code : Netidserver.net. ds.MCA Semester 5 . Sardar Vallabhbhai Patel Institute of Technology .io. String ipClass. System.getLength()).*. ds.

println(e. if(cls>=0 && cls<=127) netAdd =ip[0]+".out. } catch(Exception e) { System. try { String ip[] = ipAddress. } public static void main(String args[]) { try { while(true) new Netidserver().0" . else if(cls>=192 && cls <=223) netAdd =ip[0]+".out. int cls = Integer.25 - Nikhil N Kulkarni Roll No : 06 MCG 35 send(netAddress(frmClient)).getMessage()).close(). else if(cls>=128 && cls<=191) netAdd =ip[0]+".java : /* <applet code="Netidclient" width=300 height=300> </applet> */ Sardar Vallabhbhai Patel Institute of Technology .println(e.MCA Semester 5 .0."+ip[1]+".0" . } } public String netAddress(String ipAddress) { String netAdd="".Vasad . } catch(Exception e) { System.parseInt(ip[0]). else netAdd="Can't Find Out".split("\\.0" .receive(). ds."+ip[1]+".0.getMessage()+"\n"). } catch(Exception e) { System. } } } Netidclient.0.getMessage()).println(e.")."+ip[2]+". } return netAdd.out.

*.awt. Sardar Vallabhbhai Patel Institute of Technology .26 - Nikhil N Kulkarni Roll No : 06 MCG 35 import java. import java. add(txt). add(lbl). add(btn). import java. } public void start() { while(true) receive(). btn. btn = new Button("SEND").getBytes(). import java.awt. TextArea txa . } public void paint(Graphics g) { } public void send() { try{ DatagramSocket ds = new DatagramSocket().*.getText(). add(txa).event. } public void actionPerformed(ActionEvent ae) { send(). txt = new TextField(10). byte buffer[] = txt. lbl = new Label("IP Address :").*.MCA Semester 5 .*. Button btn . setVisible(true). TextField txt .addActionListener(this).50).applet. txa = new TextArea(10. public class Netidclient extends Applet implements ActionListener { Label lbl . public Netidclient() { } public void init() { setLayout(new FlowLayout()).net.Vasad . int BUFFSIZE = 1024.

27 - Nikhil N Kulkarni Roll No : 06 MCG 35 DatagramPacket dp = new DatagramPacket(buffer.getByName("localhost").append(e.close().getData(). ds. ds. DatagramPacket dp = new DatagramPacket(buffer.MCA Semester 5 . txa. byte buffer[] = new byte[BUFFSIZE].receive(dp).send(dp).getText()+"\n").append("Client : "+ txt.length.getLength()).0. 3000).length).buffer.close(). ds.Vasad .InetAddress. String frmServer = new String(dp. } catch(Exception e) { txa.append("Net Address : "+frmServer+"\n"). } catch(Exception e) { txt.setText(e.buffer.getMessage()).getMessage()).dp. } } public void receive() { try { DatagramSocket ds = new DatagramSocket(5000). txa. } } } Sardar Vallabhbhai Patel Institute of Technology . ds.

Vasad .28 - Nikhil N Kulkarni Roll No : 06 MCG 35 OUTPUT : Sardar Vallabhbhai Patel Institute of Technology .MCA Semester 5 .

String str2.lang.area.str3. OutputStream os = socket.29 - Nikhil N Kulkarni Roll No : 06 MCG 35 Definition : Implement a client server program where client sends radius as integer to server.14*radius*radius.getInputStream(). DataInputStream dis = new DataInputStream(is). str2 = dis. DataOutputStream dos = new DataOutputStream(os). int cnt=0. Socket socket = ss. } } } Sardar Vallabhbhai Patel Institute of Technology .*. class circleserver { public static void main(String args[]) { int port = 6666.in)). int radius = Integer.14.printStackTrace().parseInt(str2). Server computes the area and circumference of the circle and returns the result back to client as integer.println(" The radius is = " +radius).io. try{ ServerSocket ss = new ServerSocket(port).accept(). InputStream is = socket. import java.Vasad .*. circum = 2*radius*3.*. } catch(Exception e) { e.java : import java.getOutputStream().MCA Semester 5 . String res = "The area is "+area+"and circumference is "+circum. Source code : circleserver. double circum.net. import java.out.readUTF(). System. area = 3. dos. BufferedReader br = new BufferedReader(new InputStreamReader(System.writeUTF(res).

System.readLine(). DataOutputStream dos = new DataOutputStream(os). String finalres = dis.getInputStream(). DataInputStream dis = new DataInputStream(is).getOutputStream(). //Double db[] = new Double[3]. String str1.15999999999997and circumference is 75.MCA Semester 5 .flush().port).net. } catch(Exception e) { e. } } } OUTPUT : D:\SemesterV\NT2\circle>java circleserver The radius is = 12 D:\SemesterV\NT2\circle>javac circleclient. System.*. Socket socket = new Socket(address.io.36 Sardar Vallabhbhai Patel Institute of Technology .readUTF(). OutputStream os = socket. InputStream is = socket.getByName(address).*.java D:\SemesterV\NT2\circle>java circleclient Enter the radius : 12 The area is 452. try { InetAddress ipadr = InetAddress.writeUTF(str1).println("Enter the radius : ").java : import java.printStackTrace(). import java. dos. dos. BufferedReader br = new BufferedReader(new InputStreamReader(System. str1 = br. public class circleclient { public static void main(String args[]) { int port = 6666.out.out.30 - Nikhil N Kulkarni Roll No : 06 MCG 35 circleclient.println(finalres). String address = "localhost".in)).Vasad .

DataOutputStream dos = new DataOutputStream(os). res2 = " The string is not palindrome and reverse string " +str4. String str4 = sb.Vasad . InputStream is = socket. res1 = " The string is palindrome and reverse string is " +str4. Server checks the string if it is a palindrome. StringBuffer sb = new StringBuffer(str2). } } catch(Exception e) { e. char ch.MCA Semester 5 .*.31 - Nikhil N Kulkarni Roll No : 06 MCG 35 Definition : Implement a client server program where client sends a string to server. String str2 = dis. BufferedReader br = new BufferedReader(new InputStreamReader(System. int len.io.readUTF().net. Socket socket = ss.accept().reverse(). OutputStream os = socket.in)). System. try{ ServerSocket ss = new ServerSocket(port).toString().println("The original string : " +str2). reversed string or palindrome) Source code : revserver.out. } else { dos.*. (i.getOutputStream(). Accordingly it sends result back to client.java : import java.equals(str2)) { dos. import java. class revserver { public static void main(String args[]) { int port = 6666.getInputStream(). DataInputStream dis = new DataInputStream(is).printStackTrace().writeUTF(res2). } } } Sardar Vallabhbhai Patel Institute of Technology . String res1.e.res2. If string is not palindrome it reverses it.writeUTF(res1). if(str4.

DataInputStream dis = new DataInputStream(is).java D:\SemesterV\NT2\reverse>java revserver The original string : abcdcba D:\SemesterV\NT2\reverse>java revclient Enter any string : Paresh The string is not palindrome and reverse string hseraP D:\SemesterV\NT2\reverse>java revclient Enter any string : abcdcba The string is palindrome and reverse string is abcdcba Sardar Vallabhbhai Patel Institute of Technology .32 - Nikhil N Kulkarni Roll No : 06 MCG 35 revclient. dos. String str3 = dis.io. InputStream is = socket.out.MCA Semester 5 .port).getInputStream(). OutputStream os = socket. System.readUTF().getOutputStream(). System.writeUTF(str).out. dos. } catch(Exception e) { e.printStackTrace(). String str = null.println(str3).net.*.println(" Enter any string : "). import java. public class revclient { public static void main(String args[]) { int port = 6666.*.java : import java. BufferedReader br = new BufferedReader(new InputStreamReader(System. } } } OUTPUT : D:\SemesterV\NT2\reverse>java revserver The original string : Paresh D:\SemesterV\NT2\reverse>javac revserver. DataOutputStream dos = new DataOutputStream(os).Vasad .in)).readLine(). String address = "localhost". str = br.flush(). Socket socket = new Socket(address.getByName(address). try{ InetAddress ipadr = InetAddress.

Server echoes the same text message back to client.Vasad .getInputStream()..io.*. System.net.").33 - Nikhil N Kulkarni Roll No : 06 MCG 35 Definition : Implement a client server program where client sends text message to server. while(true) { line = in. import java.out."). public class echoClient { public static void main(String[] ar) Sardar Vallabhbhai Patel Institute of Technology . public class echoServer { public static void main(String[] ar) { int port = 6666.java : import java. out. String line = null.out. out. OutputStream sout = socket.MCA Semester 5 . import java.println("The received line from client : " + line).println(). Socket socket = ss. System.flush().out. try { ServerSocket ss = new ServerSocket(port)..printStackTrace().getOutputStream().println().readUTF().. } } } echoClient. DataInputStream in = new DataInputStream(sin).net.writeUTF(line).. InputStream sin = socket.out. System.accept().out. Source code : echoServer. } } catch(Exception x) { x.println("Waiting for the next line.*. System.java : import java.println("Waiting for a client.*.*. System. DataOutputStream out = new DataOutputStream(sout).io.

println("The server sent me this : " + line)..out.printStackTrace().out. BufferedReader keyboard = new BufferedReader(new InputStreamReader(System. System.out.out."). DataInputStream in = new DataInputStream(sin).println("Sending this line to the server. line = in. Sardar Vallabhbhai Patel Institute of Technology .println("Type something and press enter. out.in)). while(true) { line = keyboard. String line = null. try { InetAddress ipAddress = InetAddress.out. String address = "127.0.readUTF(). System. } } } OUTPUT : D:\SemesterV\NT2\ECHO>java echoServer Waiting for a client.. InputStream sin = socket.Vasad . Socket socket = new Socket(ipAddress.getInputStream().34 - Nikhil N Kulkarni Roll No : 06 MCG 35 { int serverPort = 6666.println(). System.out.MCA Semester 5 .getByName(address)..flush(). DataOutputStream out = new DataOutputStream(sout). out..getOutputStream(). System. System. System.1".readLine().println().").0.writeUTF(line).println(" Go ahead and enter more lines. serverPort). } } catch(Exception x) { x. OutputStream sout = socket.").

. Hello....Vasad .... Waiting for the next line... how r u? Sending this line to the server. D:\SemesterV\NT2\ECHO>java echoClient Type something and press enter.. Go ahead and enter more lines....... The received line from client : I m fine.35 - Nikhil N Kulkarni Roll No : 06 MCG 35 The received line from client : Hello. Sardar Vallabhbhai Patel Institute of Technology . The server sent me this : Hello. how r u? Go ahead and enter more lines. how r u? Waiting for the next line... Sending this line to the server.. I m fine.MCA Semester 5 . The server sent me this : I m fine..

DataOutputStream out = new DataOutputStream(sout).*.io.*.java : import java. Source code : daytimeServer.getOutputStream(). System. } } } daytimeClient. String daytime = dd. out. public class daytimeServer { public static void main(String[] ar) { int port = 6666.accept().printStackTrace().").*.Vasad ...out.net. import java.net. import java.MCA Semester 5 .io. public class daytimeClient { public static void main(String[] ar) { int serverPort = 6666. Sardar Vallabhbhai Patel Institute of Technology . Socket socket = ss. System.println("Waiting for a client.println(). try { ServerSocket ss = new ServerSocket(port).java import java. OutputStream sout = socket.*.toString(). Server returns the daytime back to client. } catch(Exception e) { e.writeUTF(daytime + "\r\n"). import java.util.36 - Nikhil N Kulkarni Roll No : 06 MCG 35 Definition : Implement a client server program where client connects to server.*. Date dd = new Date().out.

0. String dtime = null.1". System. DataInputStream in = new DataInputStream(sin).Vasad .getByName(address). D:\SemesterV\NT2\daytime>java daytimeClient Day and time at server := Thu Dec 11 20:43:09 IST 2008 Sardar Vallabhbhai Patel Institute of Technology . try { InetAddress ipAddress = InetAddress.getInputStream(). dtime = in..out.printStackTrace().MCA Semester 5 .readUTF().. Socket socket = new Socket(ipAddress.0. } } } OUTPUT : D:\SemesterV\NT2\daytime>java daytimeServer Waiting for a client.37 - Nikhil N Kulkarni Roll No : 06 MCG 35 String address = "127. InputStream sin = socket. serverPort).println(" Day and time at server := " +dtime). } catch(Exception e) { e.