0% found this document useful (0 votes)
22 views2 pages

Chat

This document outlines a communication protocol between a client and server. It describes a two-way communication where the client can send data to the server and vice versa. It provides an algorithm for how the client will send a file name to the server, how the server will receive it, open the file, and send the contents back line by line to the client. Example Java code is given for both the client and server classes to implement this protocol. The program is run and outputs the message sent from client to server and the server's response, demonstrating the successful two-way communication.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views2 pages

Chat

This document outlines a communication protocol between a client and server. It describes a two-way communication where the client can send data to the server and vice versa. It provides an algorithm for how the client will send a file name to the server, how the server will receive it, open the file, and send the contents back line by line to the client. Example Java code is given for both the client and server classes to implement this protocol. The program is run and outputs the message sent from client to server and the server's response, demonstrating the successful two-way communication.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

EX-NO:

AIM:

To implement a communication protocol between client and server.

PROCEDURE:

Before going into the details of this server-to-client communication, it is advised to go


through Networking – Introduction and Communication with TCP/IP Protocol to know the
terms and basics of networking and the way Java supports.

This is the first application (second one being chat application) in two-way communication.
In one-way communication, either client sends to server or server sends to client. But one
will not reciprocate (reverse communication) with the other. In two-way communication,
client sends to server and also server sends back to client.

ALGORITHM:

 To take input for file name from keyboard. Remember, this file should exist on server.
For this, it uses input stream.
 The file read from keyboard should be sent to the server. For this client uses output
stream.
 The file contents sent by the server, the client should receive and print on the console.
 First job is to read the file name coming from client. For this, it uses input stream.
 Second one is to open the file, using some input stream, and read the contents.
 Third one is, as the reading is going on, to send the contents each line separately.

PROGRAM:
import java.io.*;
import java.net.*;
public class MyClient
{
public static void main(String[] args)
{
try
{
Socket s=new Socket("192.168.1.147",6662);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
import java.io.*;
import java.net.*;
public class MyServer
{
public static void main(String[] args)
{
try
{
ServerSocket ss=new ServerSocket("192.168.1.147",44346);
Socket s=ss.accept();
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:
:/>java server.java
:/>java client.java
Connection to the server
Enter the message=hello server
Replay from server=hello server

RESULT:
Thus the above program has been implemented and executed successfully.

You might also like