You must Sign In to post a response.
  • Category: Java Programming

    Java programming: Simple Client Server Chat Application

    Server Chat Code

    import java.awt.*;
    import java.net.*;
    import javax.swing.*;
    import java.awt.event.*;
    class ServerChat extends JFrame implements Runnable,ActionListener
    {
    TextArea msgArea;
    Thread recieveThread;
    TextField msgText;
    Button sendButton;
    DatagramSocket ds;
    int cport=10,sport=11;
    ServerChat() throws Exception
    {
    msgArea=new TextArea(10,10);
    msgText=new TextField(10);
    sendButton=new Button("send");

    setLayout(new FlowLayout(FlowLayout.CENTER));
    add(msgArea);
    add(msgText);
    add(sendButton);

    sendButton.addActionListener(this);
    setBounds(10,10,200,200);
    setVisible(true);
    ds=new DatagramSocket(sport);
    recieveThread=new Thread(this);
    recieveThread.start();
    }
    public void actionPerformed(ActionEvent e)
    {

    try
    {
    String message=msgText.getText();
    DatagramPacket dp=new DatagramPacket(message.getBytes(),message.length(),InetAddress.getLocalHost(),cport);

    ds.send(dp);
    msgArea.append("You:"+message+"\n");
    }catch(Exception e1){}

    }
    public void run()
    {

    byte b[]=new byte[1000];
    while(true)
    {
    try
    {
    DatagramPacket dp=new DatagramPacket(b,b.length);
    ds.receive(dp);
    String data=new String(dp.getData(),0,dp.getLength());
    msgArea.append("Client:"+data+"\n");
    }catch(Exception e){}
    }

    }
    public static void main(String x[]) throws Exception
    {
    new ServerChat();
    }


    }

    Client chat Code

    import java.awt.*;
    import java.net.*;
    import javax.swing.*;
    import java.awt.event.*;
    class ClientChat extends JFrame implements Runnable,ActionListener
    {
    TextArea msgArea;
    Thread recieveThread;
    TextField msgText;
    Button sendButton;
    DatagramSocket ds;
    int cport=10,sport=11;
    ClientChat() throws Exception
    {
    msgArea=new TextArea(10,10);
    msgText=new TextField(10);
    sendButton=new Button("send");

    setLayout(new FlowLayout(FlowLayout.CENTER));
    add(msgArea);
    add(msgText);
    add(sendButton);

    sendButton.addActionListener(this);
    setBounds(10,10,200,200);
    setVisible(true);
    ds=new DatagramSocket(cport);
    recieveThread=new Thread(this);
    recieveThread.start();
    }
    public void actionPerformed(ActionEvent e)
    {

    try
    {
    String message=msgText.getText();
    DatagramPacket dp=new DatagramPacket(message.getBytes(),message.length(),InetAddress.getLocalHost(),sport);

    ds.send(dp);
    msgArea.append("You:"+message+"\n");
    }catch(Exception e1){}

    }
    public void run()
    {

    byte b[]=new byte[1000];
    while(true)
    {
    try
    {
    DatagramPacket dp=new DatagramPacket(b,b.length);
    ds.receive(dp);
    String data=new String(dp.getData(),0,dp.getLength());
    msgArea.append("Server:"+data+"\n");
    }catch(Exception e){}
    }

    }
    public static void main(String x[]) throws Exception
    {
    new ClientChat();
    }


    }

    Both the program will run seprately on cmd prompt. The two window are open, one for client and other for server.
  • #7701
    The server and client program run on same computer.

  • #7704
    Dear member
    Is it possible to run the client and server on two different computer?
    How to run the client on one computer and server on other computer?

  • #7713
    Dear member
    The client and server application also run on two different computer.
    For this the two computer are connected with each other on network and capable of sharing data.

  • #8842


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.