Friday, 3 February 2012

Tagged under: , , , , ,

Client Server Chat

In this tutorial we will learn how to write a simple client server chat program in java. We will right both the server and client side of this program. One of the ways computers can communicate to each other through the internet is by using TCP/IP.
network socket is an endpoint of an inter-process communication flow across a computer network. Today, most communication between computers is based on the Internet Protocol; therefore most network sockets are Internet sockets.
socket address is the combination of an IP address and a port number, much like one end of a telephone connection is the combination of a phone number and a particular extension. Based on this address, internet sockets deliver incoming data packets to the appropriate application process or thread.

An Internet socket is characterized by a unique combination of the following:
  • Local socket address: Local IP address and port number
  • Remote socket address: Only for established TCP sockets. As discussed in the client-server section below, this is necessary since a TCP server may serve several clients concurrently. The server creates one socket for each client, and these sockets share the same local socket address.
  • Protocol: A transport protocol (e.g., TCP, UDP, raw IP, or others). TCP port 53 and UDP port 53 are consequently different, distinct sockets.

So, now lets get started with the actual code...
  • Client.java:
import java .io.*;
import java.net.*;
public class Client
{
Socket soc;
BufferedReader inFromUser,inFromServer;
PrintWriter outToServer;
String str;
public Client()
{
try
{
soc=new Socket(InetAddress.getLocalHost(),9999);
inFromUser=new BufferedReader(new InputStreamReader(System.in));
outToServer=new PrintWriter(soc.getOutputStream(), true);
System.out.println("Client Started");
while(true)
{
str=inFromUser.readLine();
outToServer.println(str);
new InnerClient();
}
}
catch (Exception e){e.printStackTrace();}
}
class InnerClient extends Thread
{
String str1;
InnerClient()
{
try
{inFromServer=new BufferedReader(new InputStreamReader(soc.getInputStream()));
start();
}
catch (Exception e){e.printStackTrace();}
}
public void run()
{
try
{
while(true)
{
str1=inFromServer.readLine();
System.out.println("Server says : " +str1);
}
}
catch (Exception e){e.printStackTrace();}
}
}
public static void main(String args[])
{
new Client();
}
}
  • Server.java:
import java.io.*;
import java.net.*;
public class Server extends Thread
{
ServerSocket ss;
Socket soc;
BufferedReader inFromUser,inFromClient;
PrintWriter outToClient;
String str;
public Server()
{
try
{
System.out.println("Chat Server");
ss=new ServerSocket(9999);
System.out.println("Server started.Wawiting for Client......");
soc=ss.accept();
System.out.println("client connected.");
inFromUser= new BufferedReader(new InputStreamReader(soc.getInputStream()));
start();
new InnerServer();
}
catch (Exception e) {e.printStackTrace();}
}
public void run()
{
try
{
while(true)
{
str= inFromUser.readLine();
System.out.println("Client says:"+str);
}
}
catch (Exception e) {e.printStackTrace();}
}
class InnerServer
{
String str1;
InnerServer()
{
try
{
inFromClient=new BufferedReader(new InputStreamReader(System.in));
outToClient=new PrintWriter(soc.getOutputStream(),true);
while(true)
{
str1=inFromClient.readLine();
outToClient.println(str1);
}
}
catch(Exception e){e.printStackTrace();}
}
}
public static void main(String args[])
{
new Server();
}
}



Note: If you want to implement the Server & Client on Different PC's then the only change required is changing the InetAddress.getLocalHost() in Client.java to the IP address of the Server PC!!

SNAPSHOTS OF SERVER & CLIENT SIDES

 

You can for further assistance.

Kindly Bookmark and Share it:

3 comments:

  1. Where the logs of chats gets saved??

    ReplyDelete
  2. In this particular code, the logs are not saved anywhere...But if you wish to save the logs, you can easily do it by writing a few lines of code, for saving the logs in a text file to be simple, or even connect to a database and store the logs there.

    ReplyDelete