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
MCA Semester 5 -4-
Nikhil N Kulkarni Roll No : 06 MCG 35
int arr[];
arr=new int[len];
System.out.println("Enter Array Elements ");
for(int i=0;i<len;i++)
{
arr[i]=Integer.parseInt(br.readLine());
}
ClientCC c1 = new ClientCC();
c1.connection();
String response = c1.communicate(arr);
System.out.println("From Server : " + response);
}
catch(Exception e)
{
e.getMessage();
e.printStackTrace();
}
}
}

OUTPUT :

D:\SemesterV\NT2\maxmin>javac maxminserver.java

D:\SemesterV\NT2\maxmin>java maxminserver
Server Started...
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.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...
From Server : Minimum of array =3 and Maximum of array = 90

Sardar Vallabhbhai Patel Institute of Technology - Vasad


MCA Semester 5 -5-
Nikhil N Kulkarni Roll No : 06 MCG 35
Definition :

Implement client server program when client send integer array to


server, and option of ascending or descending order. Server sort the
array based on received option value and send a sorted array back to
client.

Source code :

Server.java :

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

class SortServer
{
ServerSocket ss;
Socket s;
DataInputStream dis;
DataOutputStream dos;

public SortServer()
{
ss=null;
s=null;
dis=null;
dos=null;
}
public void communicate()
{
int request=0;
String response="";
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 stype = dis.readInt();
if(stype == 1)
{
int temp=0;
for(int k=0;k<arr.length;k++)
{
for(int l=1;l<arr.length-k;l++)
{
if(arr[l]<arr[l-1])
{
temp = arr[l];
arr[l]= arr[l-1];
Sardar Vallabhbhai Patel Institute of Technology - Vasad
MCA Semester 5 -6-
Nikhil N Kulkarni Roll No : 06 MCG 35
arr[l-1] = temp;
}
}
}
}
else{
int temp=0;
for(int k=0;k<arr.length;k++)
{
for(int l=1;l<arr.length-k;l++)
{
if(arr[l]>arr[l-1])
{
temp = arr[l];
arr[l]= arr[l-1];
arr[l-1] = temp;
}
}
}
}
for(int i = 0;i<len;i++)
response += arr[i] + ",";
response = response.substring(0,response.length()-1);
dos.writeUTF(response);
}
catch(Exception e)
{
System.out.println("Exception : Socekt Not Created !!!
Program Terminated");
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 Server


{
Sardar Vallabhbhai Patel Institute of Technology - Vasad
MCA Semester 5 -7-
Nikhil N Kulkarni Roll No : 06 MCG 35
public static void main(String [] args)
{
try
{
SortServer s1 = new SortServer();
s1.connection();
}
catch(Exception e)
{
e.getMessage();
e.printStackTrace();
}
}
}

Client.java :

import java.io.*;
import java.net.*;
class SortClient
{
Socket s;
DataInputStream dis;
DataOutputStream dos;

public SortClient()
{
s=null;
dis=null;
dos=null;
}

public String communicate(int arr[],int stype)


{
String reply=null;
try
{
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());

dos.writeInt(arr.length);
System.out.println("Enter Array elements ");
for(int i=0;i<arr.length;i++)
{
dos.writeInt(arr[i]);
}
dos.writeInt(stype);
reply=dis.readUTF();
s.close();
}
catch(Exception e)
{
System.out.println("Exception : Socekt Not Created !!!
Program Terminated");
e.getMessage();
e.printStackTrace();
}
return reply;
Sardar Vallabhbhai Patel Institute of Technology - Vasad
MCA Semester 5 -8-
Nikhil N Kulkarni Roll No : 06 MCG 35
}
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 Client


{
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 lenght ");


int len = Integer.parseInt(br.readLine());
int arr[];
arr=new int[len];
System.out.println("Enter Array Elements ");
for(int i=0;i<len;i++)
{
arr[i]=Integer.parseInt(br.readLine());
}
System.out.println("PRESS 1 FOR ASCENDING ");
System.out.println("PRESS 2 FOR DESCENDING ");
int stype = Integer.parseInt(br.readLine());

System.out.println("Client Sending array of lenght: "+


len);
SortClient c1 = new SortClient();
c1.connection();
String response = c1.communicate(arr,stype);

System.out.println("From Server : Sorted Array : " +


response);

}
catch(Exception e)
{
e.getMessage();
e.printStackTrace();
}
Sardar Vallabhbhai Patel Institute of Technology - Vasad
MCA Semester 5 -9-
Nikhil N Kulkarni Roll No : 06 MCG 35
}
}

OUTPUT:

D:\Nt\ex_3>java Server
Server Started...
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...
Enter Array elements
From Server : Sorted Array : 1,2,3,5,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,5,3,2,1

Sardar Vallabhbhai Patel Institute of Technology - Vasad


MCA Semester 5 - 10 -
Nikhil N Kulkarni Roll No : 06 MCG 35
Definition :

Implement client server program where a client sends the text file to
server.server returns the number of words in that file back to client.

Source code :

FileServer.java :

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

public class FileServer


{
public static void main(String args[])
{
try{
ServerSocket ss=new ServerSocket(21);
Socket s=ss.accept();

InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);

String filenm=dis.readUTF();
File f= new File(filenm);

if(!f.exists())
System.out.println("File does not exists:");
//FileInputStream fis=new FileInputStream(filenm);
BufferedReader br=new BufferedReader(new
FileReader(filenm));
String str= ""; int count=0;
while((str = br.readLine())!= null)
{
StringTokenizer st = new StringTokenizer(str);
while(st.hasMoreElements())
{
System.out.println(st.nextToken());
count++;
}
}
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
dos.writeInt(count);
s.close();
}
catch(Exception e)
{
System.out.println("Error........");
}
}
}

FileClient.java :

import java.io.*;
import java.net.*;
Sardar Vallabhbhai Patel Institute of Technology - Vasad
MCA Semester 5 - 11 -
Nikhil N Kulkarni Roll No : 06 MCG 35
import java.util.*;

public class FileClient


{
public static void main(String args[])
{
try{
Socket s=new Socket("localhost",21);
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));
System.out.print("Enter file name:");
String fnm=br.readLine();

dos.writeUTF(fnm);

InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);
int word=dis.readInt();
System.out.println("Number of charecter found in file :
"+ word );

s.close();
}
catch(Exception e)
{
System.out.println("error...");
}
}
}

OUTPUT :

D:\Nt\ex_7>java FileClient
Enter file name:name.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 - Vasad


MCA Semester 5 - 12 -
Nikhil N Kulkarni Roll No : 06 MCG 35
Definition :

Implement client server program where client sends a string to server.


Server matches the string with a list of predefined string stored in a
text file accordingly server returns a success or failure message.

Name.txt
BillGates
NarendraModi
RatanTata

Source code :

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()
{
String request="";
String response="Name not found in File";

try
{
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
String fileData = "";
request=dis.readUTF();
System.out.println("Request From Client : "+request);
BufferedReader br=new BufferedReader(new
FileReader("name.txt"));
while((fileData=br.readLine())!=null)
{
if(fileData.equalsIgnoreCase(request))
{
response = "Name found in File";
}
}
dos.writeUTF(response);
}
catch(Exception e)
{
System.out.println("Exception : Socekt Not Created !!!
Program Terminated");
e.getMessage();
Sardar Vallabhbhai Patel Institute of Technology - Vasad
MCA Semester 5 - 13 -
Nikhil N Kulkarni Roll No : 06 MCG 35
e.printStackTrace();
}
}

public void connection()


{
try
{
ss = new ServerSocket(3000);
System.out.println("Prime 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 Server


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

Client.java :

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

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

public ClientCC()
{
s=null;
dis=null;
Sardar Vallabhbhai Patel Institute of Technology - Vasad
MCA Semester 5 - 14 -
Nikhil N Kulkarni Roll No : 06 MCG 35
dos=null;
}

public String communicate(String str)


{
String reply=null;

try
{
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());

dos.writeUTF(str);

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 Client


{
public static void main(String [] args)
{
try
{
InputStreamReader isr = new
InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

System.out.println("Enter A String ");


String str = br.readLine();

Sardar Vallabhbhai Patel Institute of Technology - Vasad


MCA Semester 5 - 15 -
Nikhil N Kulkarni Roll No : 06 MCG 35
System.out.println("Client Sending : "+ str);
ClientCC c1 = new ClientCC();
c1.connection();
String response = c1.communicate(str);
System.out.println("From Server : " + response);

}
catch(Exception e)
{
e.getMessage();
e.printStackTrace();
}
}
}

OUTPUT :

D:\Nt\ex_8>java Server
Prime Server Started...
Request From Client : ratantata
Request From Client : anilambani

D:\Nt\ex_8>java Client
Enter A String
ratantata
Client Sending : ratantata
Client Connected...
From Server : Name found in File

D:\Nt\ex_8>java Client
Enter A String
anilambani
Client Sending : anilambani
Client Connected...
From Server : Name not found in File

Sardar Vallabhbhai Patel Institute of Technology - Vasad


MCA Semester 5 - 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. The server should verify
whether the IP address is a valid IP address and accordingly send a
success or a failure message as string.

ValidIPServer.java :

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

public class ValidIPServer


{
public static void main(String[] ar)
{
String request=null;
String response=null;
int port = 6666;

try
{
ServerSocket ss = new ServerSocket(port);
System.out.println("Waiting for a client...");

Socket socket = ss.accept();

System.out.println();

InputStream sin = socket.getInputStream();


OutputStream sout = socket.getOutputStream();

DataInputStream in = new DataInputStream(sin);


DataOutputStream out = new
DataOutputStream(sout);

request=(String)in.readUTF();
System.out.println("From Client : "+request);
StringTokenizer stk = new StringTokenizer(request,".");

while(stk.hasMoreTokens())
{
int octet = Integer.parseInt(stk.nextToken());
if(octet>=0 && octet<=255)
response="IP is Valid";
else
{
response="IP is InValid";
break;
}
}

out.writeUTF(response);
out.flush();

String line = null;


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

while(true)
{
line = in.readUTF();
System.out.println("The received line from client: " +
line);
System.out.println("I'm sending it back...");
out.writeUTF(line);
out.flush();
System.out.println("Waiting for the next line...");
System.out.println();
}
}
catch(Exception x)
{
x.printStackTrace();
}

ValidIPClient.java :

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

class IPClient
{
Socket socket;
DataInputStream in;
DataOutputStream out;
int serverPort = 6666;
String address = "127.0.0.1";

public String communicate(String data)


{
String reply=null;

try
{
InputStream sin = socket.getInputStream();
OutputStream sout =
socket.getOutputStream();

in = new DataInputStream(sin);
out = new DataOutputStream(sout);

out.writeUTF(data);
out.flush();
reply=in.readUTF();

}
catch(Exception e)
{

Sardar Vallabhbhai Patel Institute of Technology - Vasad


MCA Semester 5 - 18 -
Nikhil N Kulkarni Roll No : 06 MCG 35
System.out.println("Exception : Socekt Not Created !!!
Program Terminated");
e.getMessage();
e.printStackTrace();
}
return reply;

}
public void connection()
{
try
{
InetAddress ipAddress = InetAddress.getByName(address);

socket = new Socket(ipAddress, serverPort);


System.out.println("Client Connected...");

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

public class ValidIPClient


{
public static void main(String [] args)
{
try
{

InputStreamReader isr = new


InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

System.out.println("Enter IP Address in Decimal Dotted


Notation (eg : 192.168.10.20) :
");
String ipAddr = br.readLine();
if(ipAddr.matches("\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9]
[0- 9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4]
[0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b"))
{
System.out.println("Client Sending : "+ipAddr);
IPClient c1 = new IPClient();
c1.connection();
String isValidIP = c1.communicate(ipAddr);
System.out.println("From Server : " + isValidIP);
}
else
{
System.out.println("Invalid IP Address");
Sardar Vallabhbhai Patel Institute of Technology - Vasad
MCA Semester 5 - 19 -
Nikhil N Kulkarni Roll No : 06 MCG 35
}
}
catch(Exception e)
{
e.getMessage();
e.printStackTrace();
}
}
}

OUTPUT :==>

D:\SemesterV\NT2\validip2>javac ValidIPServer.java

D:\SemesterV\NT2\validip2>java ValidIPServer
Waiting for a client...

From Client : 112.32.12.210


java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at java.net.SocketInputStream.read(SocketInputStream.java:182)
at
java.io.DataInputStream.readUnsignedShort(DataInputStream.java:307)
at java.io.DataInputStream.readUTF(DataInputStream.java:545)
at java.io.DataInputStream.readUTF(DataInputStream.java:522)
at ValidIPServer.main(ValidIPServer.java:149)

D:\SemesterV\NT2\validip2>javac ValidIPClient.java

D:\SemesterV\NT2\validip2>java ValidIPClient
Enter IP Address in Decimal Dotted Notation (eg : 192.168.10.20) :
112.32.12.210
Client Sending : 112.32.12.210
Client Connected...
From Server : IP is Valid

Sardar Vallabhbhai Patel Institute of Technology - Vasad


MCA Semester 5 - 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. The server should return the class
of that IP address back to the client as string.

Source code :

Server.java :

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

class Server1
{
ServerSocket ss;
Socket s;
ObjectOutputStream oos;
ObjectInputStream ois;
int port = 6666;
String ipClass;

public void runServer()


{
try
{
ss = new ServerSocket(port,1);
s =ss.accept();

oos = new ObjectOutputStream(s.getOutputStream());


ois = new ObjectInputStream(s.getInputStream());

String str=new String("");

while(true)
{

System.out.println(s.getInetAddress().getHostAddress() + "
Connected");

do
{
str = (String) ois.readObject();
String ip[] = str.split("\\.");
int cls = Integer.parseInt(ip[0]);
if(cls>=0 && cls<=127)
ipClass = new String("Class A IP Address");
else if(cls>=128 && cls<=191)
ipClass = new String("Class B IP Address");
else if(cls>=192 && cls <=223)
ipClass = new String("Class C IP Address");
else if(cls >=224 && cls <=239)
ipClass = new String("Class D IP Address");
else if(cls>=240 && cls<=255)
ipClass = new String("Class E IP Address");
oos.writeObject(ipClass);
oos.flush();

Sardar Vallabhbhai Patel Institute of Technology - Vasad


MCA Semester 5 - 21 -
Nikhil N Kulkarni Roll No : 06 MCG 35
}while(! str.equals("TERMINATE"));

oos.close();
ois.close();
s.close();

System.out.println("Server Closed");
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}

public class Server


{

public static void main(String args[])


{
try
{
Server1 ser = new Server1();
ser.runServer();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
Client.java :

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

class Client1
{
Socket s;

ObjectOutputStream oos;
ObjectInputStream ois;

String ipClass;
int port = 6666;
String str = new String("");

public void runClient(String host)


{
try
{
s = new Socket(host,port);

oos = new ObjectOutputStream(s.getOutputStream());


ois = new ObjectInputStream(s.getInputStream());

//String str = new String("");


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

do
{
System.out.println("Enter An IP Address : ");
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));

str = br.readLine();

oos.writeObject(str);
oos.flush();

String cls = (String)ois.readObject();


System.out.println("Server>>"+cls);

}while(! str.equals("TERMINATE"));

oos.close();
ois.close();
s.close();

System.out.println("Client Terminating");

}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}

public class Client


{

public static void main(String args[])


{
try
{
Client1 ser = new Client1();
//args[0] = ser.str;

//ser.runClient(args[0]);
ser.runClient("localhost");

}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}

Sardar Vallabhbhai Patel Institute of Technology - Vasad


MCA Semester 5 - 23 -
Nikhil N Kulkarni Roll No : 06 MCG 35
OUTPUT :

D:\SemesterV\NT2\ipclass>java Server
127.0.0.1 Connected

D:\SemesterV\NT2\ipclass>java Client
Enter An IP Address :
123.22.33.44
Server>>Class A IP Address
Enter An IP Address :
140.122.23.34
Server>>Class B IP Address
Enter An IP Address :
244.155.23.222
Server>>Class E IP Address
Enter An IP Address :
Connection reset

Sardar Vallabhbhai Patel Institute of Technology - Vasad


MCA Semester 5 - 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. The server should
return the network address back to the client as string. Design client
interface using Applet.

Source code :

Netidserver.java :

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

public class Netidserver


{
int BUFFSIZE = 1024;
String ipClass;

public void send(String netAdd)


{
try
{
DatagramSocket ds = new DatagramSocket();
byte buffer[] = netAdd.getBytes();

DatagramPacket dp = new
DatagramPacket(buffer,buffer.length,InetAddress.getByName("localhost"),
5000);

ds.send(dp);
ds.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}

public void receive()


{
try{
DatagramSocket ds = new DatagramSocket(3000);

byte buffer[] = new byte[BUFFSIZE];

DatagramPacket dp = new
DatagramPacket(buffer,buffer.length);

ds.receive(dp);

String frmClient = new


String(dp.getData(),0,dp.getLength());

System.out.println("Received From Client :


"+frmClient);

Sardar Vallabhbhai Patel Institute of Technology - Vasad


MCA Semester 5 - 25 -
Nikhil N Kulkarni Roll No : 06 MCG 35
send(netAddress(frmClient));

ds.close();
}
catch(Exception e)
{
System.out.println(e.getMessage()+"\n");
}
}

public String netAddress(String ipAddress)


{
String netAdd="";
try
{

String ip[] = ipAddress.split("\\.");

int cls = Integer.parseInt(ip[0]);

if(cls>=0 && cls<=127)


netAdd =ip[0]+".0.0.0" ;
else if(cls>=128 && cls<=191)
netAdd =ip[0]+"."+ip[1]+".0.0" ;
else if(cls>=192 && cls <=223)
netAdd =ip[0]+"."+ip[1]+"."+ip[2]+".0"
;
else
netAdd="Can't Find Out";
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
return netAdd;
}

public static void main(String args[])


{
try
{
while(true)
new Netidserver().receive();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}

Netidclient.java :

/*
<applet code="Netidclient" width=300 height=300>
</applet>
*/
Sardar Vallabhbhai Patel Institute of Technology - Vasad
MCA Semester 5 - 26 -
Nikhil N Kulkarni Roll No : 06 MCG 35

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;

public class Netidclient extends Applet implements ActionListener


{
Label lbl ;
TextField txt ;
TextArea txa ;
Button btn ;
int BUFFSIZE = 1024;

public Netidclient()
{
}

public void init()


{
setLayout(new FlowLayout());

lbl = new Label("IP Address :");


txt = new TextField(10);
txa = new TextArea(10,50);
btn = new Button("SEND");
add(txa);
add(lbl);
add(txt);
add(btn);
btn.addActionListener(this);
setVisible(true);
}

public void start()


{
while(true)
receive();
}

public void actionPerformed(ActionEvent ae)


{
send();
}

public void paint(Graphics g)


{
}

public void send()


{

try{
DatagramSocket ds = new DatagramSocket();

byte buffer[] = txt.getText().getBytes();

Sardar Vallabhbhai Patel Institute of Technology - Vasad


MCA Semester 5 - 27 -
Nikhil N Kulkarni Roll No : 06 MCG 35
DatagramPacket dp = new
DatagramPacket(buffer,buffer.length,InetAddress.getByName("localhost"),
3000);

txa.append("Client : "+ txt.getText()+"\n");

ds.send(dp);

ds.close();
}
catch(Exception e)
{
txt.setText(e.getMessage());
}

public void receive()


{
try
{
DatagramSocket ds = new DatagramSocket(5000);
byte buffer[] = new byte[BUFFSIZE];

DatagramPacket dp = new
DatagramPacket(buffer,buffer.length);

ds.receive(dp);

String frmServer = new


String(dp.getData(),0,dp.getLength());

txa.append("Net Address : "+frmServer+"\n");

ds.close();
}
catch(Exception e)
{
txa.append(e.getMessage());
}
}
}

Sardar Vallabhbhai Patel Institute of Technology - Vasad


MCA Semester 5 - 28 -
Nikhil N Kulkarni Roll No : 06 MCG 35

OUTPUT :

Sardar Vallabhbhai Patel Institute of Technology - Vasad


MCA Semester 5 - 29 -
Nikhil N Kulkarni Roll No : 06 MCG 35
Definition :

Implement a client server program where client sends radius as integer


to server.
Server computes the area and circumference of the circle and returns
the result back to client as integer.

Source code :

circleserver.java :

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

class circleserver
{
public static void main(String args[])
{
int port = 6666;
try{
ServerSocket ss = new ServerSocket(port);
Socket socket = ss.accept();

InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();

DataInputStream dis = new DataInputStream(is);


DataOutputStream dos = new DataOutputStream(os);

BufferedReader br = new BufferedReader(new


InputStreamReader(System.in));

String str2,str3;
double circum,area;

str2 = dis.readUTF();

int radius = Integer.parseInt(str2);


int cnt=0;

System.out.println(" The radius is = " +radius);

circum = 2*radius*3.14;
area = 3.14*radius*radius;

String res = "The area is "+area+"and circumference is


"+circum;

dos.writeUTF(res);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Sardar Vallabhbhai Patel Institute of Technology - Vasad


MCA Semester 5 - 30 -
Nikhil N Kulkarni Roll No : 06 MCG 35
circleclient.java :

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

public class circleclient


{
public static void main(String args[])
{
int port = 6666;
String address = "localhost";
try
{
InetAddress ipadr = InetAddress.getByName(address);

Socket socket = new Socket(address,port);

InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();

DataInputStream dis = new DataInputStream(is);


DataOutputStream dos = new DataOutputStream(os);

BufferedReader br = new BufferedReader(new


InputStreamReader(System.in));

String str1;
//Double db[] = new Double[3];

System.out.println("Enter the radius : ");


str1 = br.readLine();

dos.writeUTF(str1);
dos.flush();

String finalres = dis.readUTF();


System.out.println(finalres);

}
catch(Exception e)
{
e.printStackTrace();
}
}
}

OUTPUT :

D:\SemesterV\NT2\circle>java circleserver
The radius is = 12

D:\SemesterV\NT2\circle>javac circleclient.java

D:\SemesterV\NT2\circle>java circleclient
Enter the radius :
12
The area is 452.15999999999997and circumference is 75.36
Sardar Vallabhbhai Patel Institute of Technology - Vasad
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. Server checks the string if it is a palindrome. If string is
not palindrome it reverses it. Accordingly it sends result back to
client. (i.e. reversed string or palindrome)

Source code :

revserver.java :

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

class revserver
{
public static void main(String args[])
{
int port = 6666;
try{
ServerSocket ss = new ServerSocket(port);
Socket socket = ss.accept();
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
DataInputStream dis = new DataInputStream(is);
DataOutputStream dos = new DataOutputStream(os);
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int len;
char ch;
String res1,res2;
String str2 = dis.readUTF();
System.out.println("The original string : " +str2);
StringBuffer sb = new StringBuffer(str2);
String str4 = sb.reverse().toString();
res1 = " The string is palindrome and reverse string is "
+str4;
res2 = " The string is not palindrome and reverse string "
+str4;
if(str4.equals(str2))
{
dos.writeUTF(res1);
}
else
{
dos.writeUTF(res2);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Sardar Vallabhbhai Patel Institute of Technology - Vasad


MCA Semester 5 - 32 -
Nikhil N Kulkarni Roll No : 06 MCG 35
revclient.java :

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

public class revclient


{
public static void main(String args[])
{
int port = 6666;
String address = "localhost";
try{
InetAddress ipadr = InetAddress.getByName(address);
Socket socket = new Socket(address,port);
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
DataInputStream dis = new DataInputStream(is);
DataOutputStream dos = new DataOutputStream(os);
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str = null;
System.out.println(" Enter any string : ");
str = br.readLine();
dos.writeUTF(str);
dos.flush();
String str3 = dis.readUTF();
System.out.println(str3);

}
catch(Exception e)
{
e.printStackTrace();
}
}
}

OUTPUT :

D:\SemesterV\NT2\reverse>java revserver
The original string : Paresh

D:\SemesterV\NT2\reverse>javac revserver.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 - Vasad


MCA Semester 5 - 33 -
Nikhil N Kulkarni Roll No : 06 MCG 35
Definition :

Implement a client server program where client sends text message to


server. Server echoes the same text message back to client.

Source code :

echoServer.java :

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

public class echoServer {


public static void main(String[] ar)
{

int port = 6666;

try
{
ServerSocket ss = new ServerSocket(port);
System.out.println("Waiting for a client...");
Socket socket = ss.accept();
System.out.println();
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
String line = null;
while(true)
{
line = in.readUTF();
System.out.println("The received line from client : " +
line);
out.writeUTF(line);
out.flush();
System.out.println("Waiting for the next line...");
System.out.println();
}

}
catch(Exception x)
{
x.printStackTrace();
}

echoClient.java :

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

public class echoClient


{
public static void main(String[] ar)
Sardar Vallabhbhai Patel Institute of Technology - Vasad
MCA Semester 5 - 34 -
Nikhil N Kulkarni Roll No : 06 MCG 35
{
int serverPort = 6666;

String address = "127.0.0.1";

try
{
InetAddress ipAddress = InetAddress.getByName(address);

Socket socket = new Socket(ipAddress, serverPort);

InputStream sin = socket.getInputStream();


OutputStream sout = socket.getOutputStream();

DataInputStream in = new DataInputStream(sin);


DataOutputStream out = new DataOutputStream(sout);

BufferedReader keyboard = new BufferedReader(new


InputStreamReader(System.in));
String line = null;

System.out.println("Type something and press enter.");


System.out.println();

while(true)
{
line = keyboard.readLine();
System.out.println("Sending this line to the server...");

out.writeUTF(line);

out.flush();

line = in.readUTF();

System.out.println("The server sent me this : " + line);


System.out.println(" Go ahead and enter more lines.");
System.out.println();
}

}
catch(Exception x)
{

x.printStackTrace();
}

OUTPUT :

D:\SemesterV\NT2\ECHO>java echoServer
Waiting for a client...

Sardar Vallabhbhai Patel Institute of Technology - Vasad


MCA Semester 5 - 35 -
Nikhil N Kulkarni Roll No : 06 MCG 35
The received line from client : Hello, how r u?
Waiting for the next line...

The received line from client : I m fine.....


Waiting for the next line...

D:\SemesterV\NT2\ECHO>java echoClient
Type something and press enter.

Hello, how r u?
Sending this line to the server...
The server sent me this : Hello, how r u?
Go ahead and enter more lines.

I m fine.....
Sending this line to the server...
The server sent me this : I m fine.....
Go ahead and enter more lines.

Sardar Vallabhbhai Patel Institute of Technology - Vasad


MCA Semester 5 - 36 -
Nikhil N Kulkarni Roll No : 06 MCG 35
Definition :

Implement a client server program where client connects to server.


Server returns the daytime back to client.

Source code :

daytimeServer.java

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

public class daytimeServer {


public static void main(String[] ar)
{

int port = 6666;

try
{
ServerSocket ss = new ServerSocket(port);
System.out.println("Waiting for a client...");

Socket socket = ss.accept();

System.out.println();

OutputStream sout = socket.getOutputStream();

DataOutputStream out = new


DataOutputStream(sout);

Date dd = new Date();


String daytime = dd.toString();

out.writeUTF(daytime + "\r\n");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

daytimeClient.java :

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

public class daytimeClient


{
public static void main(String[] ar)
{
int serverPort = 6666;
Sardar Vallabhbhai Patel Institute of Technology - Vasad
MCA Semester 5 - 37 -
Nikhil N Kulkarni Roll No : 06 MCG 35

String address = "127.0.0.1";

try
{
InetAddress ipAddress = InetAddress.getByName(address);

Socket socket = new Socket(ipAddress, serverPort);


InputStream sin = socket.getInputStream();

DataInputStream in = new DataInputStream(sin);

String dtime = null;


dtime = in.readUTF();

System.out.println(" Day and time at server := " +dtime);

}
catch(Exception e)
{
e.printStackTrace();
}
}
}

OUTPUT :

D:\SemesterV\NT2\daytime>java daytimeServer
Waiting for a client...

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 - Vasad

You might also like