In this paper , We're based on a multithreaded framework , Realize the function of registration and login , This chapter , On this basis, we can realize public chat 、 The private chat 、 Display online user list function .

Public chat

Next, we will explain in detail the flow chart of the last few articles, as follows :

 Insert picture description here
As shown in the figure above , We removed the network connection and client login 、 Registration and other functions , Go straight into the chat process :

  1. The client selects the public chat function from the menu ;
  2. Enter the message you want to chat with ;
  3. Enter to send a chat message ;
  4. After the sub thread of the server receives the public chat data , Enter the public chat process ;
  5. Find all online users , Send the chat message to all online users ;
  6. After the client enters the chat, it will create a sub thread , This sub thread will receive data information sent by all servers in a loop .

The private chat

 Insert picture description here
As shown in the figure above :

  1. The client selects the private chat function from the menu ;
  2. Enter the object to chat with and chat information ;
  3. Send chat message to server ;
  4. After the sub thread of the server receives the public chat data , Enter the public chat process ;
  5. Find all online users , Send the chat message to all online users ;
  6. The client thread will receive all the data sent by the server in a loop .

Show online users

 Insert picture description here
As shown in the figure above :

  1. The client chooses to display the online user function from the menu ;
  2. Encapsulate and display online user packets , And send the packet to the server ;
  3. When the server receives the packet , Enter the display online user function module ;
  4. Check the online user data information database , Will online users 【fd Not for -1】 The name is encapsulated in the packet , Fill one at a time ,stat Field filling ONLINEUSER_OK;
  5. All users are sent , Add a packet ,stat fill ONLINEUSER_OVER;
  6. After receiving the online user packet sent by the server , The client terminal thread enters the display online user sub module ; Extract the online user's name from the packet and print , Judge the packet stat Is it ONLINEUSER_OVER, If not, continue to receive the next packet , If it is , The user will be prompted that the display is complete .

Run a screenshot

Now preset :
Customer A:yikoulinux
Customer B:yikoupeng

Public chat

Customer B A public chat :

Customer A Receive the message :

The server log:

The private chat

Customer B Send a private message to A:

Customer A Messages received :

Display online user information