[go: up one dir, main page]

0% found this document useful (0 votes)
60 views16 pages

OOADM Socket

The document describes a client-server program that implements a simple echo protocol using TCP. The server creates a server socket, accepts a connection from the client and reads/writes data from the socket streams. The client connects to the server, writes data to the output stream and reads the response from the input stream. It then displays the response. The document also describes a menu-driven client-server program where the server maintains a threaded queue and allows insert, delete and search operations on the queue, and the client connects to the server to perform these operations.

Uploaded by

divya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views16 pages

OOADM Socket

The document describes a client-server program that implements a simple echo protocol using TCP. The server creates a server socket, accepts a connection from the client and reads/writes data from the socket streams. The client connects to the server, writes data to the output stream and reads the response from the input stream. It then displays the response. The document also describes a menu-driven client-server program where the server maintains a threaded queue and allows insert, delete and search operations on the queue, and the client connects to the server to perform these operations.

Uploaded by

divya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

Lok Jagruti Institute Of Computer Applications

Sem V Roll No: 11

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;

String str= 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

Data Received from the Client is hello


Data Sending to Client............
Data send successfully.....

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>
*/

public class client extends Applet implements ActionListener


{
TextField t1,t2;
Label l1,l2;
Button submit;

public void init()


{
t1 = new TextField(30);
t2 = new TextField(30);
l1 = new Label("Enter the message");
l2 = new Label("Enter the port ");
submit = new Button("Submit");

add(l1);
add(t1);
add(l2);
add(t2);
add(submit);

submit.addActionListener(this);
}

public void actionPerformed(ActionEvent e)


{
String str=null;
str=e.getActionCommand();

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)
{

}
}
}

public void paint(Graphics g)


{
String msg = t1.getText();
g.drawString(msg,250,240);
}
}

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");
}
}

void Search(int d,Socket s) throws Exception


{
int i;
for(i=front;i<rear;i++)
{
if(data[i]==d)
{
PrintStream p=new PrintStream(s.getOutputStream());
p.println("Data found at position "+i);
break;
}
}
if(i==rear)
{
PrintStream p=new PrintStream(s.getOutputStream());
p.println("Data Not Found....");
}
}

void Display(Socket s) throws Exception


{
PrintStream p=new PrintStream(s.getOutputStream());

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:");

InputStreamReader isrk=new InputStreamReader(System.in);


BufferedReader brk=new BufferedReader(isrk);
ch=Integer.parseInt(brk.readLine());

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

BufferedReader br=new BufferedReader(isr);


System.out.println(br.readLine());

}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

Enter Your choice: 1


Enter Data:
22

Inserted Successfully

1.Insert:
2.Delete:
3.Search:
4.Display:
0.Quit:

Enter Your choice: 1


Enter Data:
43

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:

Enter Your choice: 1


Enter Data:
44

Inserted Successfully

1.Insert:
2.Delete:
3.Search:
4.Display:
0.Quit:

Enter Your choice: 4


22
43
44

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;

Date d = new Date();


PrintStream ps;

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 -

You might also like