TCP server (server)
If you want to perform the functions of the tcp server, you need to perform the following process
- Socket create a socket
- The bind method binds IP and port
- listen makes the socket passive
- accept waits for unprovoked links (you must create a new listening socket and redefine a new receiver)
- recv/send receive send data
For accept, you must receive two functions: one is to define a new socket, and the other is to listen. Monitor it
import socket def main(): #1. Buy a mobile phone (create socket) #This socket is used to initialize the response. Finally, the newly defined socket is needed for real communication tcp_server_scoket =socket.socket(socket.AF_INET,socket.SOCK_STREAM) #2. Insert the mobile phone card (bind the local information) tcp_server_scoket.bind(("",7890)) #3. Set the phone to the normal ring mode (change the default socket from active to passive listen) tcp_server_scoket.listen(128) print("------1----------") #4. Wait for others to call (wait for the client's link accpet) #Redefine new sockets and new binding IP and ports new_client_socket,client_addr =tcp_server_scoket.accept() print("--------2--------") print(client_addr) #Receive requests from clients recv_data =new_client_socket.recv(1024) print(recv_data) #Some data will be sent to the client new_client_socket.send("hhhhhhh".encode("utf-8")) new_client_socket.close() tcp_server_scoket.close() if __name__ == '__main__': main()
To sum up, there are still great differences between the above and udp receiving messages. For example, TCP signaling requires a new socket, which will be redefined later. Port binding is the same as udp. You must do it earlier. The accept function called through the new socket and the monitored IP and port will be congested when the server does not receive the information sent by the client. If the information sent by the client is not received, the program cannot continue to execute forward. Once someone sends the information and is received by the client, the client will continue to execute, This causes the program to directly receive a user's information, and can only receive it once, which will have this disadvantage. In order to solve this disadvantage, we need to re optimize the code. Make the TCP server cycle to serve multiple clients
The TCP server circulates to serve multiple clients
import socket def main(): #1. Buy a mobile phone (create socket) #This socket is used to initialize the response. Finally, the newly defined socket is needed for real communication tcp_server_scoket =socket.socket(socket.AF_INET,socket.SOCK_STREAM) #2. Insert the mobile phone card (bind the local information) tcp_server_scoket.bind(("",7890)) #3. Set the phone to the normal ring mode (change the default socket from active to passive listen) tcp_server_scoket.listen(128) #This while True: the loop serves multiple clients while True: print("Wait for a new client to arrive....") #4. Wait for others to call (wait for the client's link accpet) new_client_socket,client_addr =tcp_server_scoket.accept() print("A client has arrived") print("A new client has arrived%s"%str(client_addr)) #Receive requests from clients #This while True loop serves the same client multiple times while True: recv_data =new_client_socket.recv(1024) print("The request sent by the client is%s"%recv_data.decode("utf-8")) #Some data will be sent to the client new_client_socket.send("hhhhhhh".encode("utf-8")) new_client_socket.close() print("Service completion") #If the listening socket is closed, you cannot wait for a new client to arrive, that is, XXXX Accept will fail tcp_server_scoket.close() if __name__ == '__main__': main()
Continue to optimize and have another delicious soup
import socket def main(): #1. Buy a mobile phone (create socket) #This socket is used to initialize the response. Finally, the newly defined socket is needed for real communication tcp_server_scoket =socket.socket(socket.AF_INET,socket.SOCK_STREAM) #2. Insert the mobile phone card (bind the local information) tcp_server_scoket.bind(("",7890)) #3. Set the phone to the normal ring mode (change the default socket from active to passive listen) tcp_server_scoket.listen(128) #This while True: the loop serves multiple clients while True: print("Wait for a new client to arrive....") #4. Wait for others to call (wait for the client's link accpet) new_client_socket,client_addr =tcp_server_scoket.accept() print("A client has arrived") print("A new client has arrived%s"%str(client_addr)) #Receive requests from clients #This while True loop serves the same client multiple times while True: recv_data =new_client_socket.recv(1024) print("The request sent by the client is%s"%recv_data.decode("utf-8")) #Some data will be sent to the client #If recv is unblocked, there are two ways #1. Data sent by the client 2 The client calls close, which causes the recv to unblock if recv_data: #Some data will be sent to the client new_client_socket.send("hhhhhhh".encode("utf-8")) else: break new_client_socket.close() print("Service completion") #If the listening socket is closed, you cannot wait for a new client to arrive, that is, XXXX Accept will fail tcp_server_scoket.close() if __name__ == '__main__': main()
Generally speaking, similar to udp, several points need to be noted. TCP service requires two end linkage, one is the client and the other is the server.