The process is as follows:
"" "TCP server-side creation process
1. Create a server-side TCP Socket:server_socket to listen for client requests
2. Binding port
3. Server_socket turn on the monitor, the active connection mode becomes the passive acceptance mode
4. Wait for the client to receive the request, a connection, immediately respond, and create a client docking with the socket, with the socket to communicate with the client
5. Using the newly created socket to communicate with the client
6. Close the newly created socket and close the socket to end the communication with the current client
7. Server_socket shutdown, no new client requests are received
"""
The code is as follows:
1 fromSocket Import *2 3 4 def main ():5#1. Create a Tcp_server_socket socket object to listen for requests from clients6Tcp_server_socket =socket (af_inet,sock_stream)7 8 # re-use the same port without prompting the port to be occupied, to resolve when the socket is closed when in the Time_out state, you can also use the port immediately9 """Ten socket.setsockopt (level,option,value): Configure socket One level: rank, to which level operation (Ip,tcp,udp,socket level) A Option : Set which option - socket. SO_REUSEADDR: Reuse Address - value:true: Indicates reuse, False, means no longer used the """ - tcp_server_socket.setsockopt (sol_socket,so_reuseaddr,true) - # Hint: socket.setsockopt () method to set before Socket.bind () - +#2. Binding Ports -Tcp_server_socket.bind (("',9999)) + A#3. Tcp_server_socket start listening, active connect mode becomes passive connection mode atTcp_server_socket.listen (5) - -#4. Wait for the client's request, once connected, create a new new_socket socket with the client, and contact the client with this new keyword - whileTrue: -NEW_SOCKET,CLIENT_ADDR =tcp_server_socket.accept () -Print"New client connections:", CLIENT_ADDR) inNew_socket.send ("Welcome to use". Encode ()) - to#5. Use the newly created New_socket to communicate with the client + # While True here provides multiple services for the same client - whileTrue: theRecv_data = NEW_SOCKET.RECV (1024x768) * $ ifRecv_data:Panax NotoginsengPrint"New message:%s, from:%s"% (Recv_data.decode ('GBK'), client_addr)) - the # Returns a message to the client +New_socket.send ("Goodbye". Encode ()) A the Else: +Print'The other side has gone offline') - Break $ $#6close the newly created socket and close the socket to end the communication with the current client. - new_socket.close () - the#7. Server_socket shuts down, no new client requests are received - tcp_server_socket.close ()Wuyi the - if__name__ = ='__main__': WuMain ()
The results are as follows:
Summary:
The simplest process is to not have two while True, such a server equivalent to 100,861 days of service only one person after work
1. Add the first while True to realize a day can serve many people, but only one person at a time, and this person can only say a word after exiting the service, continue to receive the next customer "" "
2. The second while True implements the customer service can say many words, but still cannot access multiple customers at once (need to use multi-tasking)
PS: If there is a problem, please tell me in the comments, learn together!
Python Programming Series---Simple implementation of TCP server side