Java multi-client chat program implementation program

Source: Internet
Author: User
Tags sendmsg socket server port
The code is as follows: Copy code


Import java. io .*;
Import java.net .*;
Import java. util .*;


Public class HeartServer {
    
/*
* Member variable debut
*/
List <ClientThread> clients = new ArrayList <ClientThread> ();
   
/**
* This is the portal. Check it out here...
* @ Param args
*/
Public static void main (String [] args ){
New HeartServer (). start ();
    }
   
/**
* Starting the server...
     *
*/
Public void start (){
Try {
Boolean iConnect = false;
ServerSocket ss = new ServerSocket (1720 );
IConnect = true;
While (iConnect ){
System. out. println ("server port bound successfully! ");
Socket s = ss. accept ();
ClientThread currentClient = new ClientThread (s); // create a thread reference
System. out. println ("client found! ");
Clients. add (currentClient); // add the current client to the set
New Thread (currentClient). start ();
System. out. println ("The client process has been started! ");
            }
} Catch (IOException e ){
System. out. println ("IOException ");
E. printStackTrace ();
        }
    }
   
/**
* Leave a home address for each client (client process)
     *
*/
Class ClientThread implements Runnable {
/*
* The member variable is coming again...
*/
Private Socket s;
Private DataInputStream dis;
Private DataOutputStream dos;
Private String str;
Private boolean iConnect = false;
       
/**
* Minor structure
*/
ClientThread (Socket s ){
This. s = s;
IConnect = true;
        }
       
Public void run (){
System. out. println ("run method started! ");
Try {
               
While (iConnect ){
System. out. println ("the while loop in the RUN method starts, waiting for the client to send a message ...");
Dis = new DataInputStream (s. getInputStream ());
Str = dis. readUTF ();
System. out. println (str );
For (int I = 0; I <clients. size (); I ++ ){
System. out. println ("forwarding messages..." + I );
ClientThread c = clients. get (I );
C. sendMsg (str );
                    }
                }
} Catch (IOException e ){
E. printStackTrace ();
            }
           
        }
       
/**
* Forward messages. I am the master...
* Send messages sent to the server to each connected client.
*/
Public void sendMsg (String str ){
Try {
System. out. println ("create an output pipeline! ");
Dos = new DataOutputStream (this. s. getOutputStream ());
System. out. println ("The message is being written to the client! ");
Dos. writeUTF (str );
System. out. println ("The message is successfully written to the client! ");
} Catch (IOException e ){
E. printStackTrace ();
            }
           
        }
       
    }
   
}


Program

The code is as follows: Copy code

Import java. awt .*;
Import java. awt. event .*;
Import java. io .*;
Import java. lang .*;
Import java.net .*;

Public class HeartClient extends Frame {

/*
* Member method appearance...
*/
Private TextField tfText;
Private TextArea taContent;
Private Socket s;
Private DataOutputStream dos;
Private DataInputStream dis;
   
/**
* Note: The entry... ^
* @ Param args
*/
Public static void main (String [] args ){
New HeartClient (). launchFrame ();
       
    }
   
/**
* Loading GU
*/
Public void launchFrame (){
TfText = new TextField ();
TaContent = new TextArea ();
This. setSize (300,300 );
This. setLocation (300,300 );
This. tfText. addActionListener (new TFListener ());
This. add (tfText, BorderLayout. SOUTH );
This. add (taContent, BorderLayout. NORTH );
This. addWindowListener (new WindowAdapter (){
@ Override
Public void windowClosing (WindowEvent e ){
System. exit (0 );
}});
This. pack ();
This. connect ();
This. setVisible (true );
    }

/**
* I am trying to connect to the server...
*/
Public void connect (){
Try {
S = new Socket ("127.0.0.1", 1720 );
Dos = new DataOutputStream (s. getOutputStream ());
Dis = new DataInputStream (s. getInputStream ());
New Thread (new SendThread (). start ();
// Dos. writeUTF ("Hello, I find u! ");
} Catch (UnknownHostException e ){
System. out. println ("UnknownHostException ");
E. printStackTrace ();
} Catch (IOException e ){
System. out. println ("IOException ");
E. printStackTrace ();
} Finally {
// Disable Donnie...
        }
       
    }

/**
* Don't wait for tfText (the listener class of TextField tfText)
*/
Class TFListener implements ActionListener {
Private String str;
@ Override
Public void actionreceivmed (ActionEvent e ){
Str = tfText. getText (). trim ();
TfText. setText ("");
Try {
Dos. writeUTF (str );
} Catch (IOException e1 ){
System. out. println ("IOException ");
E1.printStackTrace ();
            }
        }
       
    }
   
/**
* The thread in which the client receives messages...
     *
*/
Class SendThread implements Runnable {
Private String str;
Private boolean iConnect = false;
       
Public void run (){
IConnect = true;
RecMsg ();
           
        }
       
/**
* Message, look at moves, where to run... (implementation of the client to receive messages)
* @ Throws IOException
*/
Public void recMsg (){
Try {
While (iConnect ){
Str = dis. readUTF ();
TaContent. setText (str );
                }
} Catch (IOException e ){
E. printStackTrace ();
            }
           
        }
       
    }
   
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.