Implementing Client Server chat Program in Java computer Networks Concept

Here is a client server program implemented in java . The program was complied using Netbeans IDE with both programs opened in different tabs and was able to make connection and send messages from client to server. The program was tested in LAN network. if you are working on some other network please make changes required in the code.

CLIENT.JAVA


package cn;
import java.io.*;
import java.net.*;

//cleint side program
public class client
{
    Socket requestSocket;
    ObjectOutputStream out;
    ObjectInputStream in;
    String msg;
    int portno;
    server(){}
    void run()
    {
        try
        {
            portno=2004;
            requestSocket=new Socket("0.0.0.0",portno);
            System.out.println("Connect to local host "+portno);
            out=new ObjectOutputStream(requestSocket.getOutputStream());
            out.flush();
            in=new ObjectInputStream(requestSocket.getInputStream());
            do
            {
                try
                {
                    msg=(String)in.readObject();
                    System.out.println("Server> "+msg);
                    BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("Enter the message: ");
                    msg=obj.readLine();
                    sendMessage(msg);
                }
                catch(ClassNotFoundException e)
                {
                    System.err.println("data received in unknown format");
                }
            }
            while(!msg.equals("bye"));
        }
        catch(UnknownHostException u)
        {
            System.err.println("You are trying to connect to an unknown host");
        }
        catch(IOException io)
        {
            io.printStackTrace();
        }
        finally
        {
            try
            {
                in.close();
                out.close();
                requestSocket.close();
            }
            catch(IOException i)
            {
                i.printStackTrace();
            }
        }
    }
    void sendMessage(String msg)
    {
        try
        {
            out.writeObject(msg);
            out.flush();
            System.out.println("Client>" +msg);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
    public static void main(String[] args)throws IOException
    {
        cleint g=new client();
        g.run();
    }
}




SERVER.JAVA


package cn;
import java.io.*;
import java.net.*;
public class server
{
    ServerSocket providerSocket;
    Socket connection=null;
    ObjectOutputStream out;
    ObjectInputStream in;
    String msg;
    int portno;
    client(){}
    void run()
    {
        portno=2004;
        try
        {
            providerSocket=new ServerSocket(portno,10);
            System.out.println("waiting for connection");
            connection=providerSocket.accept();
            System.out.println("Connection received from "+connection.getInetAddress().getHostName());
            out=new ObjectOutputStream(connection.getOutputStream());
            out.flush();
            in=new ObjectInputStream(connection.getInputStream());
            sendMessage("Connection successfull");
            do
            {
                try
                {
                    msg=(String)in.readObject();
                    System.out.println("Cleint>" +msg);
                    BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
                    System.out.println("Enter the message:");
                    msg=obj.readLine();
                    sendMessage(msg);
                    if(msg.equals("bye"))
                        sendMessage(msg);
                }
                catch(ClassNotFoundException e)
                {
                    System.err.println("Data received in unkown format");
                }
            }
            while(!msg.equals("bye"));
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                in.close();
                out.close();
                providerSocket.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    void sendMessage(String msg)
    {
        try
        {
            out.writeObject(msg);
            out.flush();
            System.out.println("Server>"+msg);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
    public static void main(String args[])
    {
        server g=new server();
        while(true)
        {
            g.run();
        }
    }
}
OUTPUT:


     SERVER:

waiting for connection Connection received from I2-03 client>Connection successfull Cleint>HI SERVER Enter the message: Server>HI CLIENT

CLIENT:

Connect to local host 2004 Server> Connection successful Enter the message: Client>HI SERVER Enter the message: Server>HI SERVER

2 comments:

Anonymous said...

What's up everyone, it's my first go to see at this web page,
and post is in fact fruitful designed for me,
keep up posting these types of articles.

my web-site :: pauldingsentinel.com

E Tar said...

how do you connect to another computer and chat between them??

Post a Comment

If You Are Asking Some Question On This Comment Then Click On Subscribe by email Link