OOADM Socket
OOADM Socket
Program-1
Write simple echo client and server using TCP protocol.
Server-Side
==========
import java.io.*;
import java.net.*;
class EchoServer
{
public static void main(String args[])
{
Socket s = null;
ServerSocket ss = null;
BufferedReader br = null;
PrintStream pr = null;
try
{
ss = new ServerSocket(1049);
s = ss.accept();
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
pr = new PrintStream(s.getOutputStream());
str = br.readLine();
System.out.println("Data Received from the Client is " + str);
System.out.println("Data Sending to Client............");
pr.println(str);
System.out.println("Data send successfully.....");
}
catch(Exception e)
{
}
}
}
OOADM_Socket -1-
Lok Jagruti Institute Of Computer Applications
Sem V Roll No: 11
/*
OUTPUT :
=========
Z:\Sem5\Echoserver>javac *.java
Z:\Sem5\Echoserver>java EchoServer
Z:\Sem5\Echoserver>
*/
OOADM_Socket -2-
Lok Jagruti Institute Of Computer Applications
Sem V Roll No: 11
Client-Side
==========
import java.io.*;
import java.applet.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code = client width = 400 height = 300>
</applet>
*/
add(l1);
add(t1);
add(l2);
add(t2);
add(submit);
submit.addActionListener(this);
}
if(str.equals("Submit"))
{
OOADM_Socket -3-
Lok Jagruti Institute Of Computer Applications
Sem V Roll No: 11
try
{
String msg = t1.getText();
Socket s = new Socket("localhost",1049);
PrintStream pr = new PrintStream(s.getOutputStream());
InputStreamReader isr= new InputStreamReader(s.getInputStream());
BufferedReader br = new BufferedReader(isr);
pr.println(msg);
t2.setText(br.readLine());
repaint();
}
catch(Exception exc)
{
}
}
}
OOADM_Socket -4-
Lok Jagruti Institute Of Computer Applications
Sem V Roll No: 11
/*
OUTPUT
=======
*/
OOADM_Socket -5-
Lok Jagruti Institute Of Computer Applications
Sem V Roll No: 11
Program-2
Write a menu driven client to provide following operations. Server should be
multithreaded. Provide a means of consistency checking.
0. Quit
1. Insert in the array of the server
2. Delete from the array of the server
3. Query from the array of the server.
Server-Side
==========
import java.util.*;
import java.io.*;
import java.net.*;
class Queue
{
int data[]=new int[10];
int front,rear;
Queue()
{
front=0;
rear=0;
}
void Insert(int d,Socket s) throws Exception
{
if(rear!=10)
{
data[rear]=d;
rear=rear+1;
PrintStream p=new PrintStream(s.getOutputStream());
p.println("Inserted Successfully");
}
else
{
PrintStream p=new PrintStream(s.getOutputStream());
p.println("Queue is Full");
}
}
void Delete(Socket s) throws Exception
OOADM_Socket -6-
Lok Jagruti Institute Of Computer Applications
Sem V Roll No: 11
{
if(front<rear)
{
front++;
PrintStream p=new PrintStream(s.getOutputStream());
p.println("Data is deleted Successfully");
}
else
{
PrintStream p=new PrintStream(s.getOutputStream());
p.println("Queue is Empty");
}
}
p.println(data.length);
for(int i=front;i<rear;i++)
{
p.println(data[i]);
}
p.println("Displayed...");
}
OOADM_Socket -7-
Lok Jagruti Institute Of Computer Applications
Sem V Roll No: 11
class QueueServer
{
public static void main(String args[]) throws Exception
{
ServerSocket ss=null;
Socket s=null;
int ch,d;
try
{
Queue q=new Queue();
ss=new ServerSocket(1500);
s=ss.accept();
do
{
InputStreamReader isr=new InputStreamReader
(s.getInputStream());
BufferedReader br=new BufferedReader(isr);
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
d=Integer.parseInt(br.readLine());
q.Insert(d,s);
break;
case 2:
q.Delete(s);
break;
case 3:
d=Integer.parseInt(br.readLine());
q.Search(d,s);
break;
case 4:
q.Display(s);
break;
}
}while(ch!=0);
}
catch(UnknownHostException e)
{
System.out.println(e);
}
OOADM_Socket -8-
Lok Jagruti Institute Of Computer Applications
Sem V Roll No: 11
catch(IOException e)
{
System.out.println(e);
}
finally
{
try
{
s.close();
ss.close();
}
catch(UnknownHostException e)
{
System.out.println(e);
}
catch(IOException e)
{
System.out.println(e);
}
}
}
}
Client-Side
==========
import java.net.*;
import java.io.*;
class QueueClient
{
public static void main(String args[])
{
Socket s=null;
int ch,data;
try
{
s=new Socket("localhost",1500);
do
{
System.out.println("1.Insert:");
System.out.println("2.Delete:");
System.out.println("3.Search:");
System.out.println("4.Display:");
OOADM_Socket -9-
Lok Jagruti Institute Of Computer Applications
Sem V Roll No: 11
System.out.println("0.Quit:");
System.out.println("Enter Your choice:");
OutputStream o=s.getOutputStream();
PrintStream p=new PrintStream(o);
p.println(ch);
switch(ch)
{
case 1:
System.out.println("Enter Data:");
data=Integer.parseInt(brk.readLine());
p.println(data);
break;
case 2:
break;
case 3:
System.out.println("Enter Data for Search:");
data=Integer.parseInt(brk.readLine());
p.println(data);
break;
case 4:
InputStreamReader isr=new InputStreamReader
(s.getInputStream());
BufferedReader br=new BufferedReader(isr);
int len=Integer.parseInt(br.readLine());
for(int i=0;i<len;i++)
{
System.out.println(br.readLine());
}
break;
}
InputStreamReader isr=new InputStreamReader
(s.getInputStream());
OOADM_Socket - 10 -
Lok Jagruti Institute Of Computer Applications
Sem V Roll No: 11
}while(ch!=0);
}
catch(UnknownHostException e)
{
System.out.println(e);
}
catch(IOException e)
{
System.out.println(e);
}
finally
{
try
{
s.close();
}
catch(UnknownHostException e)
{
System.out.println(e);
}
catch(IOException e)
{
System.out.println(e);
}
}
}
}
/*
OUTPUT :
========
Z:\Sem5\PROTOCOL>java QueueClient
1.Insert:
2.Delete:
3.Search:
4.Display:
0.Quit:
OOADM_Socket - 11 -
Lok Jagruti Institute Of Computer Applications
Sem V Roll No: 11
Inserted Successfully
1.Insert:
2.Delete:
3.Search:
4.Display:
0.Quit:
Inserted Successfully
OOADM_Socket - 12 -
Lok Jagruti Institute Of Computer Applications
Sem V Roll No: 11
1.Insert:
2.Delete:
3.Search:
4.Display:
0.Quit:
Inserted Successfully
1.Insert:
2.Delete:
3.Search:
4.Display:
0.Quit:
Displayed...
*/
OOADM_Socket - 13 -
Lok Jagruti Institute Of Computer Applications
Sem V Roll No: 11
Program-3
Write simple DayTime client and server using TCP protocol.
Server-Side
==========
import java.net.*;
import java.io.*;
import java.util.*;
class DateServer
{
public static void main(String args[])
{
ServerSocket s = null;
Socket ds=null;
try
{
s = new ServerSocket(8);
while(true)
{
ds = s.accept();
ps = new PrintStream(ds.getOutputStream());
ps.println(d.toString());
}
}
catch(Exception e)
{ System.out.println(e); }
finally
{
try
{
s.close();
ds.close();
}
catch(Exception e)
{ System.out.println(e); }
OOADM_Socket - 14 -
Lok Jagruti Institute Of Computer Applications
Sem V Roll No: 11
}
}
}
Client-Side
=========
import java.net.*;
import java.io.*;
class DateClient
{
public static void main(String args[])
{
Socket c = new Socket();
InputStream is;
BufferedReader br;
try
{
c = new Socket("localhost",8);
is = c.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
System.out.println(br.readLine());
}
catch(Exception e)
{ System.out.println(e); }
finally
{
try
{
c.close();
}
catch(Exception e)
{ }
}
}
}
/*
OUTPUT :
========
OOADM_Socket - 15 -
Lok Jagruti Institute Of Computer Applications
Sem V Roll No: 11
Z:\sem5\SOCKET_PROG>java DateClient
Sat Dec 08 11:07:51 PST 2007
Z:\sem5\SOCKET_PROG>
*/
OOADM_Socket - 16 -