Development of TCP client program development steps
- Create the client socket object
- And server socket connection is established
- send data
- Receive data
- Close client socket
Import Socket IF the __name__ == ' __main__ ' : # Create client socket tcp # 1. AF_INET: indicates IPv4 # 2. SOCK_STREAM: tcp transport protocol tcp_client_socket = socket.socket (socket.AF_INET, socket.SOCK_STREAM) # and services client applications to establish a connection tcp_client_socket.connect (( " 192.168.131.62 " , 8080 )) # code execution to this, the connection is established successfully # data ready to be sent send_data = " hello server, my client is black! " .encode ( " GBK " ) # sending data tcp_client_socket.send (the send_data) # receiving data, the maximum number of bytes received is 1024 recv_data = tcp_client_socket.recv (1024 ) # of binary data is returned directly to the server program transmits Print (recv_data) # data is decoded = recv_data.decode recv_content ( " GBK " ) Print ( " data reception server as: " , recv_content) # close the socket tcp_client_socket.close ()
Development of TCP server program development steps
- Create a service endpoints socket object
- Binding port number
- Set up listeners
- Awaits a connection request from the client
- Receive data
- send data
- Closes the socket
Import socket IF __name__ == ' __main__ ' : # create a tcp server socket tcp_server_socket = socket.socket (socket.AF_INET, socket.SOCK_STREAM) # Set the port number multiplexing, so that the program exits port number immediate release tcp_server_socket.setsockopt ( socket.SOL_SOCKET, socket.SO_REUSEADDR, True) # to program bind port number tcp_server_socket.bind (( "" , 8989 )) # set to listen # 128: wait for the establishment of maximum number of connections, suggesting that: a single task is currently serving end, the same time only service with a client, the subsequent use of multi-tasking allows the server and multiple clients simultaneously serve, # do not need to let the client waits to establish a connection # the socket after listen only responsible for receiving customer connection request, can send and receive messages, send and receive messages using the new socket returned to complete tcp_server_socket.listen (128 ) # waiting for the client request to establish a connection, only the client and server to establish a successful connection before the code unblocking code execution to continue down the # 1 special socket and client communications: service_client_socket # 2. the client ip address and port number: for ip_port service_client_socket, for ip_port = tcp_server_socket.accept () # code execution described this connection is established Print ( " client the ip address and port number: " , for ip_port) # sent by a client data, the maximum number of bytes of data is received 1024 recv_data = service_client_socket.recv (1024 ) # get the length data recv_data_length = len (recv_data) Print ( " length of the received data is: ", Recv_data_length) # binary data decoding recv_content = recv_data.decode ( " GBK " ) Print ( " the receiving client data: " , recv_content) # data ready to be sent the send_data = " OK, the problem being processed ... " .encode ( " GBK " ) # sending data to the client service_client_socket.send (the send_data) # Close service client socket, and a termination of service client communications service_client_socket.close () # Close the socket server, termination and providing client service to establish a connection request tcp_server_socket.close ()