[go: up one dir, main page]

0% found this document useful (0 votes)
6 views9 pages

EXNO3

lab

Uploaded by

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

EXNO3

lab

Uploaded by

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

EXNO: 3 Applications using TCP sockets like: a) Echo Client and echo server b) chat

Aim:

To create a java program for implementing socket program of TCP Echo client and Echo
server.

Algorithm:

Client

1.Start

2.Create the TCP socket

3.Establish connection with the server

4. Get the message to be echoed from the user

5.Send the message to the server

6. Receive the message echoed by the server

7. Display the message received from the server

8.Terminate the connection

9. Stop

Server

1. Start

2. Create TCP socket, make it a listening socket

3. Accept the connection request sent by the client for connection establishment

4. Receive the message sent by the client

5. Display the received message

6.Send the received message to the client from which it receives

7. Close the connection when client initiates termination and server becomes a listening server,
waiting for clients.

8. Stop

Program:

EchoServer.java

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

public class EServer

Public static void main(String args[])

ServerSocket

s=null;

String line;

DataInputStream is;

PrintStream ps;

Socket c=null;

try

s=new

ServerSocket(9000);

catch(IOException e)

System.out.println(e);

tr y

c=s.accept();

is=new

DataInputStream(c.getInputStream());

ps=new

PrintStream(c.getOutputStream());
while(true)

line=is.readLine( );

ps.println(line);

catch(IOExceptione)

System.out.println(e);

EClient.java

Import java.net.*;

import java.io.*;

public class EClient

Public static void main(String arg[])

Socket c=null;

String line;

DataInputStream is,is1;

PrintStream os;

try

InetAddress ia = InetAddress.getLocalHost();

c=new Socket(ia,9000);
}

catch(IOException e)

System.out.println(e);

try

os=new

PrintStream(c.getOutputStream());

is=new DataInputStream(System.in);

is1=new DataInputStream(c.getInputStream());

while(true)

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

line=is.readLine();

os.println(line);

System.out.println("Server:"+ is1.readLine());

catch(IOException e)

System.out.println("Socket Closed!");

}}

OUTPUT

Server

C:\ProgramFiles\Java\jdk1.5.0\bin>javacEServer.java
C:\Program Files\Java\jdk1.5.0\bin>java EServer

C:\Program Files\Java\jdk1.5.0\bin>Client

C:\Program Files\Java\jdk1.5.0\bin>javac EClient.java

C:\ProgramFiles\Java\jdk1.5.0\bin>java

EClient Client: Hai Server

Server:Hai

Server Client:

Hello

Server:Hello

Client:end

Server:end

Client:ds

Socket

Closed!

B) Chat

ALGORITHM

Client

1. Start

2. Create the UDP datagram socket

3. Get the request message to be sent from the user

4. Send the request message to the server

5. If the request message is “END” go to step 10

6. Wait for the reply message from the server

7. Receive the reply message sent by the server

8. Display the reply message received from the server

9. Repeat the steps from 3to8

10.Stop
Server

1.Start

2.Create UDP datagram socket, make it a listening socket

3. Receive the request message sent by the client

4. If the received message is “END” go to step 10

5.Retrieve the client’s IP address from the request message received

6. Display the received message

7. Get the reply message from the user

8. Send the reply message to the client

9. Repeat the steps from 3 to 8.

10. Stop.

PROGRAM

UDPserver.java

Import java.io.*;

import java.net.*; class

UDPserver

Public static DatagramSocket ds;

Public static byte buffer[]=new byte[1024];

public static int clientport=789,serverport=790;

public static void main(String args[])throws Exception

ds=new DatagramSocket(clientport);

System.out.println("press ctrl+c to quit the program");

BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));

InetAddress ia=InetAddress.geyLocalHost();

while(true)
{

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

ds.receive(p);

String psx=new String(p.getData(),0,p.getLength());

System.out.println("Client:" + psx);

System.out.println("Server:");

String str=dis.readLine();

if(str.equals("end"))

break;

buffer=str.getBytes();

ds.send(new DatagramPacket(buffer,str.length(),ia,serverport));

UDPclient.java

Import java .io.*;

import bjava.net.*;

class UDPclient

Public static DatagramSocket ds;

Public static int clientport=789,

serverport=790;

public static void main(String args[])throws Exception

Byte buffer[]=new byte[1024];

ds=new DatagramSocket(serverport);

BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));


System.out.println("server waiting");

InetAddress ia=InetAddress.getLocalHost();

while(true)

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

String str=dis.readLine();

if(str.equals("end"))

break;

buffer=str.getBytes();

ds.send(new DatagramPacket(buffer,str.length(),ia,clientport));

DatagramPacket p=new DatagramPacket(buffer,buffer.length); ds.receive(p);

String psx=new String(p.getData(),0,p.getLength());

System.out.println("Server:" + psx);

OUTPUT:

Server

C: \Program Files\Java\jdk1.5.0\bin>javac UDPserver.java

C:\ProgramFiles\Java\jdk1.5.0\bin>java

UDPserver press ctrl+c to quit the program

Client:Hai

Server

Server:Hello

Client

Client:How are You Server:I am Fine

Client
C:\Program Files\Java\jdk1.5.0\bin>javac UDPclient.java

C:\ProgramFiles\Java\ jdk1.5.0 \ bin>java UDPclient server waiting

Client:Hai

Server

Server:Hello Clie

Client:How are

You Server:I am

Fine Client:end

You might also like