You are on page 1of 9

AIM:- Create a program that will communicate with the server.

[Echo Server/One way communication]


Theory:-
Java Socket programming is used for communication between the applications running on different JRE.
Java Socket programming can be connection-oriented or connection-less.
Socket and ServerSocket classes are used for connection-oriented socket programming and
DatagramSocket and DatagramPacket classes are used for connection-less socket programming.
In this application, client sends a message to the server, server reads the message and prints it. Here, two
classes are being used: Socket and ServerSocket. The Socket class is used to communicate client and
server. Through this class, we can read and write message. The ServerSocket class is used at server-side.
The accept() method of ServerSocket class blocks the console until the client is connected. After the
successful connection of client, it returns the instance of Socket at server-side.

Server.java
import java.io.*;
import java .util.*;
import java.net.*;
class server
{
public static void main(String [] args)
{
try
{
ServerSocket ss = new ServerSocket(8080);
Socket s =ss.accept();
DataInputStream di = new DataInputStream(s.getInputStream());
String str= di.readUTF();
System.out.println(str);
di.close();
s.close();
ss.close();
}
catch(Exception e)
{
System.out.println("Program failed");
}
}
}

Client.java
import java.io.*;
import java .util.*;
import java.net.*;
class client
{
public static void main(String [] args)
{
try
{
Socket sc =new Socket("localhost",8080);
DataOutputStream ds = new DataOutputStream(sc.getOutputStream());
ds.writeUTF("hello wORLD" );
ds.close();
sc.close();
}
catch(Exception e)
{
System.out.println("Program failed");
}
}
}
OUTPUT:-

AIM:-Create a Single Client chat application (One to One Chat Program)

Theory:-
Java Socket programming is used for communication between the applications running on different JRE.
Java Socket programming can be connection-oriented or connection-less.
Socket and ServerSocket classes are used for connection-oriented socket programming and DatagramSocket and
DatagramPacket classes are used for connection-less socket programming.
In this application, client sends a message to the server, server reads the message and prints it. Here, two classes are
being used: Socket and ServerSocket. The Socket class is used to communicate client and server. Through this class,
we can read and write message. The ServerSocket class is used at server-side. The accept() method of ServerSocket
class blocks the console until the client is connected. After the successful connection of client, it returns the instance
of Socket at server-side.

Chatserver.java

import java.net.*;
import java.io.*;
public class Mychatserver
{
public static void main(String args[]) throws Exception
{
ServerSocket ss= new ServerSocket(2000);
Socket sk= ss.accept();
BufferedReader cin= new BufferedReader(new InputStreamReader(sk.getInputStream()));
PrintStream cout= new PrintStream(sk.getOutputStream());
BufferedReader stdin= new BufferedReader(new InputStreamReader(System.in));
String s;
while (true)
{
s= cin.readLine();
if (s.equalsIgnoreCase("END"))
{
cout.println("Bye");
break;
}
System.out.println("Client :"+s+"\n");
System.out.println("Server:");
cout.println(s);
}
ss.close();
sk.close();
cin.close();
cout.close();
stdin.close();
}
}
Chatclient.java
import java.net.*;
import java.io.*;
public class Myclientserver
{
public static void main(String args[] )throws Exception
{
Socket sk= new Socket ("localhost",2000);

BufferedReader sin= new BufferedReader(new InputStreamReader(sk.getInputStream()));


PrintStream sout= new PrintStream(sk.getOutputStream());
BufferedReader stdin= new BufferedReader(new InputStreamReader(System.in));
String s;
while( true)
{
System.out.println("Client:");
s=stdin.readLine();
sout.println(s);
s=sin.readLine();
System.out.println("Server :"+s+"\n");
if (s.equalsIgnoreCase("Bye"))
break;
}
sk.close();
sin.close();
sout.close();
stdin.close();
}
}
OUTPUT:-

Server

Client
Aim:- Create a multi Client Chat Server

Theory:-
Server class : The main server implementation is easy and similar to the previous article. The
following points will help understand Server implementation :
The server runs an infinite loop to keep accepting incoming requests.
When a request comes, it assigns a new thread to handle the communication part.
The sever also stores the client name into a vector, to keep a track of connected devices. The
vector stores the thread object corresponding to the current request. The helper class uses
this vector to find the name of recipient to which message is to be delivered. As this vector holds
all the streams, handler class can use it to successfully deliver messages to specific clients.
Invoke the start() method.
 
ClientHandler class : Similar to previous article, we create a helper class for handling various
requests. This time, along with the socket and streams, we introduce a name variable. This will
hold the name of the client that is connected to the server. The following points will help
understand ClientHandler implementation :
Whenever the handler receives any string, it breaks it into the message and recipient part. It uses
Stringtokenizer for this purpose with ‘#’ as the delimiter. Here it is assumed that the string is
always of the format:
message # recipient
It then searches for the name of recipient in the connected clients list, stored as a vector in the
server. If it finds the recipients name in the clients list, it forwards the message on its output
stream with the name of the sender prefixed to the message.

Myserver.java
import java.net.*;
import java.io.*;
public class myserver
{
public static void main(String args[])
{
Socket s =null;
ServerSocket ss2=null;
System.out.println("Server Listening");
try{
ss2 = new ServerSocket(4445);
}
catch(IOException e)
{
System.out.println("Server error");
}

while(true)
{
try{

s = ss2.accept();
System.out.println("connection Established");
ServerThread st = new ServerThread(s);
st.start();
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Connection Error");
}
}
}
}

class ServerThread extends Thread


{
String line = null;
BufferedReader is =null;
PrintWriter os = null;
Socket s= null;
public ServerThread(Socket s)
{
this.s=s;
}
public void run()
{
try
{
is= new BufferedReader(new InputStreamReader(s.getInputStream()));
os = new PrintWriter(s.getOutputStream());
}
catch(IOException e)
{
System.out.println("IO error in server thread");
}
try
{
line= is.readLine();
while(line.compareTo("QUIT")!=0)
{
os.println(line);
os.flush();
System.out.println("Response to Client:" +line);
line= is.readLine();
}
}
catch(IOException e)
{
System.out.println(e);
}
finally
{
try
{
System.out.println("Connection Closing");
if(is!=null)
{
is.close();
System.out.println("Socket Input Stream Closed");
}
if(os!=null)
{
os.close();
System.out.println("Socket Out Closed");
}
if(s!=null)
{
s.close();
System.out.println("Socket Closed");
}
}
catch(IOException ie)
{
System.out.println("Socket Close Error");
}
}
}
}

MyClient.java
import java.net.*;
import java.util.*;
import java.io.*;
class myclient
{
public static void main(String args[])
{
try
{
Socket s =new Socket("175.177.1.119",5056);
BufferedReader bf =new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintStream ps =new PrintStream(s.getOutputStream());
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
String str;
while( true)
{
System.out.println("Client; ");
str = bfr.readLine();
System.out.println(str);
str = bf.readLine();
System.out.println("Server :"+str+"\n");
if(str.equalsIgnoreCase("BYE"))
break;
}
s.close();
bf.close();
ps.close();
bfr.close();
}
catch (Exception e)
{
System.out.println("Failed");

Output:-
Server:-
 
Client 1:

Client 2:

You might also like