Monday, August 9, 2010

Java : Simple Client-Server chat application

We all might have use some or other chat application in some or other form, the basic principle of a chatting is very simple. There is a Server which acts as a controller and  Clients which can be communicate either with server or among client themselves. Lets look at a simple example in java.

In our example we have a Server.java which as the name suggest acts as a server and we have a Client.java which will act as a client. In both we used a threading concept, for receiving and sending a message to and fro.

We used Socket programming concept of ServerSocket and Socket

Kindly go through the code and see output.

//Server.java

import java.net.*;
import java.io.*;
import java.util.*;

public class Server
{
    public static void main(String args[]) throws IOException
    {
        //Register service on port 1436
        ServerSocket serverSocket = new ServerSocket(1463);
       
        //For taking input
        final Scanner scan = new Scanner(System.in);

        while(true)
        {
            //Wait and accept a connection of multiple client
            final Socket socket=serverSocket.accept();
           
            //Start new Thread for receiving message
            new Thread(new Runnable()
            {
                public void run()
                {    try
                    {
                        while(true)
                        {
                            DataInputStream dis = new DataInputStream(socket.getInputStream());
                            System.out.println(dis.readUTF());
                        }
                    }
                    catch (Exception ie)
                    { }
                }
            }).start();
           
            //Start new Thread for sending message
            new Thread(new Runnable()
            {
                public void run()
                {    try
                    {
                        while(true)
                        {
                            String userInput = scan.nextLine();
                            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
                            dos.writeUTF("Server says : "+userInput);
                        }
                    }
                    catch (Exception ie)
                    { }
                }
            }).start();

        }//while loop

    }//main
}

//Client.java

import java.net.*;
import java.io.*;
import java.util.*;

public class Client
{
    public static void main(String args[]) throws IOException
    {
        //Open your connection to a server, at port 1436
        final Socket socket = new Socket("localhost",1463);       
       
        //For taking input
        final Scanner scan = new Scanner(System.in);       
       
        //Start new Thread for sending message
        new Thread(new Runnable()
        {
            public void run()
            { try
                {
                    while(true)
                    {
                        String userInput = scan.nextLine();
                        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
                        dos.writeUTF("Client says : "+userInput);
                    }
                }
                catch (Exception ie)
                { }
            }
        }).start();
       
        //Start new Thread for receiving message
        new Thread(new Runnable()
        {
            public void run()
            { try
                {
                    while(true)
                    {
                        DataInputStream dis = new DataInputStream(socket.getInputStream());
                        System.out.println(dis.readUTF());
                    }
                }
                catch (Exception ie)
                { }
            }
        }).start();
    }//main
}


Output




Please write your comments.

11 comments:

  1. Wow, this helped me out a ton. Thanks a lot and great work!

    ReplyDelete
  2. tnx too much its helped me a lot

    ReplyDelete
  3. it is very excellent to learn the basic thing to establish the communication in core java with out any complecation. thank uuuuuuuuuuu...............
    it helps me a lot

    ReplyDelete
  4. Thank you.. Simple and Clean.

    ReplyDelete
  5. The elеctrіc pulses thаt the аurаwavе and its
    might аs a TENS gimmick if you are fitted with a cardiac sinoаtгial node or are distrеss
    from any nuisancе that has not been medіcallу diagnosed.
    Look at my web blog http://aurawavereview.com/

    ReplyDelete
  6. Perhaps this is pаrtiallу respοnsible fог the aсhieνer
    that it's one of many TENS units cost more than $200 some are fifty-fifty $400 or more than, but the Aurawave comes in.

    my website :: aurawave tens

    ReplyDelete
  7. Fundamentally the flex belt is a belt you strap on about your waist
    and three particularly placed pads are what help contract your abs.



    Also visit my site ... flex belt coupons

    ReplyDelete
  8. The issue is you ought to obtain the product from a reliable model.

    Have you experienced the possibility of seeking to use a skin tag elimination package?



    My web-site; http://enneadseries.com/

    ReplyDelete