Chat
Chat
AIM:
PROCEDURE:
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.