This article uses python to implement basic socket server and client communication, step-by-step details and complete code are available, as needed.
(i) Server-side code:
1. Introduce the socket module, and create a socket instance, the server needs to use bind to bind the instance to the specified domain name and port, the domain name and port number in the tuple to pass in the
import socket # Create socket object socket_server=() # bind socket_server to the specified ip address socket_server.bind(("localhost",8888))
2. Set the port to listen to, listen() write a number to indicate the number of links that can be accepted.
#Listening ports socket_server.listen(1)
3. Wait for the client to connect using the accept method, which is a blocking method and will continue to block at this step if there is no connection. A successful connection will return a tuple ofconnected object together withClient Address Information
result=socket_server.accept() conn=result[0] # Client connection object address=result[1] #Client Address Information
4. Use the recv method of the connection object to receive messages from the client, with a buffer size of 1024 tables, which can be set by yourself, and decode them into UTF-8 format.
data = (1024).decode("UTF-8") print(f"The message from the client is:{data}")
5. Use the send method of the connection object to send a message to the client.
msg=input("Please enter a message in reply:") (("UTF-8"))
6. Separate disconnections at the end of communications
() socket_server.close()
Complete server-side code:
import socket # Create socket object socket_server=() # bind socket_server to the specified ip address socket_server.bind(("localhost",8888)) #Listen to the port, listen() is a number that indicates the number of links that can be accepted. socket_server.listen(1) # Wait for the client to connect, the result received is a binary, accept() is a blocking method, if there is no connection, it will not be executed. result=socket_server.accept() conn=result[0] # Client connection object address=result[1] # Client address information print(f"The received client connection information is{address}") while True: # Receive client information, recv accepted parameters is the buffer size, generally 1024 can be returned is a byte array, bytes object, not a string, and then decode its decode for the string objects data = (1024).decode("UTF-8") print(f"The message from the client is:{data}") #Respond to messages msg=input("Please enter a message in reply:") if msg=='exit': break (("UTF-8")) #Close the connection () socket_server.close()
(ii) Client code:
1. Introduce the included socket module and create a socket instance, then link the server's domain name and port, also tuples passed in as parameters
import socket # Create socket object socket_client=() # Make the socket object socket_client connect to the server. socket_client.connect(("localhost",8888))
2. Then you can use send to send a message to the server.
msg=input("Please enter the message you want to send:") socket_client.send(("UTF-8"))
3. After sending, use recv to wait for a reply from the server, again setting up the buffer and decoding the message.
data=socket_client.recv(1024).decode("UTF-8") print(f"The server replies with the message:{data}")
4. Disconnection at the end of the communication
socket_client.close()
Full client code:
import socket # Create socket object socket_client=() # Make the socket object socket_client connect to the server. socket_client.connect(("localhost",8888)) while True: msg=input("Please enter the message you want to send:") if msg=='exit': break #Send a message socket_client.send(("UTF-8")) #Receive messages data=socket_client.recv(1024).decode("UTF-8") print(f"The server replies with the message:{data}") socket_client.close()
This article on the implementation of basic Python Socket server-side communication with the client is introduced to this article, more related Python Socket server-side content with the client, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future!