Java Blog

 
 
 
// Chat client

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

class chatClient extends Frame implements Runnable
{
    Socket soc;    
    TextField tf;
    TextArea ta;
    Button btnSend,btnClose;
    String sendTo;
    String LoginName;
    Thread t=null;
    DataOutputStream dout;
    DataInputStream din;
    chatClient(String LoginName,String chatwith) throws Exception
    {
        super(LoginName);
        this.LoginName=LoginName;
        sendTo=chatwith;
        tf=new TextField(50);
        ta=new TextArea(50,50);
        btnSend=new Button(\"Send\");
        btnClose=new Button(\"Close\");
        soc=new Socket(\"127.0.0.1\",5217);

        din=new DataInputStream(soc.getInputStream()); 
        dout=new DataOutputStream(soc.getOutputStream());        
        dout.writeUTF(LoginName);

        t=new Thread(this);
        t.start();

    }
    void setup()
    {
        setSize(600,400);
        setLayout(new GridLayout(2,1));

        add(ta);
        Panel p=new Panel();

        p.add(tf);
        p.add(btnSend);
        p.add(btnClose);
        add(p);
        show();        
    }
    public boolean action(Event e,Object o)
    {
        if(e.arg.equals(\"Send\"))
        {
            try
            {
                dout.writeUTF(sendTo + \" \"  + \"DATA\" + \" \" + tf.getText().toString());            
                ta.append(\"\\n\" + LoginName + \" Says:\" + tf.getText().toString());    
                tf.setText(\"\");
            }
            catch(Exception ex)
            {
            }    
        }
        else if(e.arg.equals(\"Close\"))
        {
            try
            {
                dout.writeUTF(LoginName + \" LOGOUT\");
                System.exit(1);
            }
            catch(Exception ex)
            {
            }

        }

        return super.action(e,o);
    }
    public static void main(String args[]) throws Exception
    {
        chatClient Client1=new chatClient(args[0],args[1]);
        Client1.setup();                
    }    
    public void run()
    {        
        while(true)
        {
            try
            {
                ta.append( \"\\n\" + sendTo + \" Says :\" + din.readUTF());

            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
    }
}

// Chat Server

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

class chatServer
{
    static Vector ClientSockets;
    static Vector LoginNames;

    chatServer() throws Exception
    {
        ServerSocket soc=new ServerSocket(5217);
        ClientSockets=new Vector();
        LoginNames=new Vector();

        while(true)
        {    
            Socket CSoc=soc.accept();        
            AcceptClient obClient=new AcceptClient(CSoc);
        }
    }
    public static void main(String args[]) throws Exception
    {

        chatServer ob=new chatServer();
    }

class AcceptClient extends Thread
{
    Socket ClientSocket;
    DataInputStream din;
    DataOutputStream dout;
    AcceptClient (Socket CSoc) throws Exception
    {
        ClientSocket=CSoc;

        din=new DataInputStream(ClientSocket.getInputStream());
        dout=new DataOutputStream(ClientSocket.getOutputStream());

        String LoginName=din.readUTF();

        System.out.println(\"User Logged In :\" + LoginName);
        LoginNames.add(LoginName);
        ClientSockets.add(ClientSocket);    
        start();
    }

    public void run()
    {
        while(true)
        {

            try
            {
                String msgFromClient=new String();
                msgFromClient=din.readUTF();
                StringTokenizer st=new StringTokenizer(msgFromClient);
                String Sendto=st.nextToken();                
                String MsgType=st.nextToken();
                int iCount=0;

                if(MsgType.equals(\"LOGOUT\"))
                {
                    for(iCount=0;iCount<LoginNames.size();iCount++)
                    {
                        if(LoginNames.elementAt(iCount).equals(Sendto))
                        {
                            LoginNames.removeElementAt(iCount);
                            ClientSockets.removeElementAt(iCount);
                            System.out.println(\"User \" + Sendto +\" Logged Out ...\");
                            break;
                        }
                    }

                }
                else
                {
                    String msg=\"\";
                    while(st.hasMoreTokens())
                    {
                        msg=msg+\" \" +st.nextToken();
                    }
                    for(iCount=0;iCount<LoginNames.size();iCount++)
                    {
                        if(LoginNames.elementAt(iCount).equals(Sendto))
                        {    
                            Socket tSoc=(Socket)ClientSockets.elementAt(iCount);                            
                            DataOutputStream tdout=new DataOutputStream(tSoc.getOutputStream());
                            tdout.writeUTF(msg);                            
                            break;
                        }
                    }
                    if(iCount==LoginNames.size())
                    {
                        dout.writeUTF(\"I am offline\");
                    }
                    else
                    {

                    }
                }
                if(MsgType.equals(\"LOGOUT\"))
                {
                    break;
                }

            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }



        }        
    }
}
}

    Related Post:
  1. Biological Sequence Allignment Using Dynamic Programing

  2. Program to show an example of searching Strings for subStrings

  3. Program to display reverse $ tree

  4. Program to show an example of creating a String object from a StringBuffer Object

  5. Program of an applet to illustrate BarChart

  6. Program to show an example of extraction of characters from a StringBuffer objects

  7. Program to display Floyds Triangle of a numbers

  8. Define an array of ten String elements each containing an arbitrary string of the form “month/day/year”

  9. Program of concatenating and Buffering files

  10. Program to show an example of Driving and using a Class

  11. Program to copy the elements of an array into another array in reverse order and print them

  12. An applet program to draw different shapes

  13. RMI based application for login validation based on data saved in text file stored on the server and will display appropriate message to the client..

  14. Program to read the table elements into a two-dimensional array temperature

  15. Program that takes a long sentence and a character from the client and count the occurrence of the character in the sentence at the server side

  16. Program to show an example of creating a class and using its object

  17. RMI based application, which reads a file with list of marks of student from a client, send it to server and find how many students having distinct

  18. Program of date server and client

  19. Program to read command line arguments

  20. program which takes a long sentence from the client passes to the server,server separates out each word and sends them back to client in reverse order

 
 
Didn't find what you were looking for? Find more on Socket program of multi client chat server