Using socket to realize the functions of network chat room and private chat

Use socket technology to realize the functions of network chat room and private chat. The specific contents are as follows

If you don't have much to say, please refer to the figure above:

1. Chat room group chat page

Contact list for online users

Socket connection page

Private chat page

Project introduction

Socket connection with the server: when each client connects to the server, the server will save each connected socket in the list collection.

Group chat function: when a user sends a group chat message to the server, the server will forward all information to all connected clients in the list.

Private chat function: after the user sends private chat information to the server, the server will send a message to a target IP.

Display online contact list: when a new user logs in successfully, the server will send the online contact information to the client in the form of JSON string. The client obtains the online contact information by parsing the JSON string.

Customize a powerful class socketevent: the client and server communicate through this class to save data, and then use the fastjson tool to convert the class object into JSON string for transmission.

Type: indicates what type of information is currently sent. The server and client parse data through this to determine which type of message they belong to

Keyip: the IP address of the client message initiator

Temporary IP: temporary IP address. If the type is private chat, this IP represents the IP address of the target contact

Server code (ServerSocket)

1. Receive client connections

2. Start the thread to receive information from the client in real time

3. Method of sending message to specified IP and method of sending message to all other IP except own IP

The client receives all the information from the server in the mainactivity, repacks it according to the type, and sends the information to each fragment using eventbus for display

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>