Realize text chat communication between two hosts. The communication is carried out in an IPv6 environment and completes login and registration functions, point-to-point chat, group chat and other functions.
UDP is a connectionless protocol. The source and terminal do not establish a connection before transmitting data. When it wants to transmit, it simply grabs the data from the application and throws it on the network as quickly as possible.
The main methods used at the communication protocol level are:
socket([family[, type[, proto]]])
bind(address: Union[_Address, bytes])
sendto(data, address,)
recvfrom(bufsize, flags,)
In the process of programming, a database is needed to store the user's user name, password, status, user IP address and port. When selecting the database, the SQLite3 lightweight database was used to facilitate database migration, etc.
function name | Function |
---|---|
init(self) | Used to create and open databases |
select_all(self) | Used to obtain all user information in the corresponding table of the database |
register(self, username, password, ip, port) | Called during registration to add user information |
search_username(self, username) | Find user information based on username |
login_success(self, username, ip, port) | After successful login, change the user status information in the database |
login_check(self, username, password, ip, port) | Used to check if the username and password are correct when logging in |
logout_success(self, username) | Change the user's status when the user logs out |
db_close(self) | Close database |
Enter the directory where the code is located on the two hosts that can communicate, and execute python3 Client.py
and python3 Server.py
respectively. The client can open multiple servers according to needs, but the server can only open one. When starting the server, the database will be opened.
Make selections according to the prompts to achieve different functions. In peer-to-peer chat, either party can close the chat by sending END, and the other party can confirm according to the prompts. The implementation principle of group chat is similar, and the process of running the test is also similar.