Detailed solution to the coding problem of socket socket in python3
- 2020-06-07 04:50:34
- OfStack
1. TCP
1. tcp server creation
# Create server
from socket import *
from time import ctime # The import ctime
HOST = '' # Any host
PORT = 21567 # Provide a port number at random
BUFSIZ = 1024 # Buffer size set to 1KB This capacity can be changed depending on network performance and program needs
ADDR = (HOST, PORT)
tcpSerSock = socket(AF_INET, SOCK_STREAM) # The distribution of TCP Server socket
tcpSerSock.bind(ADDR) # Bind to the server address and turn on TCP The call to the listener.
tcpSerSock.listen(5) #listen() Method is the maximum number of connection requests that are passed in before the connection is transferred or rejected
"""
1 Once we enter the infinite loop of the server, we wait (passively) for the client to connect. when 1 A connection request is made
We are now in a conversation loop in which we are waiting for a message from the client. If the message is blank, this means
This time we will break out of the dialog loop, close the current client connection, and wait for another 1 A guest
Client side connection. If you do get the message sent by the client, format it and return the same data, but in these
Data is prefixed with the current timestamp. The last 1 The line is never executed, it is only used to remind the reader if it is written 1 A place
Think about it programmatically 1 A more elegant exit, as discussed earlier, should be called close() Methods.
"""
while True:
print("waiting for connection")
tcpCliSock, addr = tcpSerSock.accept() # Receives client connection, returns client and address
print("...connected from:", addr)
while True:
data = tcpCliSock.recv(BUFSIZ).decode() # Dialogue (receive / Send) Receiving client data
if not data:
break
tcpCliSock.send(('service : '+ctime()+'--'+data).encode()) # Send timestamp and data Information to the client
tcpCliSock.close()
tcpSerSock.close()
2. Create tcp client
from socket import *
HOST = 'localhost'
PORT = 21567 # The port number PORT It should be exactly the same as what you set up for the server (otherwise, communication will not be possible)
BUFSIZ = 1024
ADDR = (HOST, PORT)
tcpCliSock = socket(AF_INET, SOCK_STREAM) # distribution TCP Client socket
tcpCliSock.connect(ADDR) # Active connection
"""
We must decode the string from the server side (by the aid of distutils.log.warn()
"""
while True:
data = input("> ")
if not data: # If the user does not enter, it terminates
break
tcpCliSock.send(data.encode()) # Sending client data To the server
data = tcpCliSock.recv(BUFSIZ).decode() # Receiving server's data
if not data: # Or the server terminates and yes recv() Method failed
break
print(' return :%s'%data)
tcpCliSock.close()
2. UDP
1. UDP server creation
"""
This script is created 1 a UDP The server, which takes the message from the client and returns it to the client with a timestamp prefix.
"""
from socket import *
from time import ctime
HOST = ""
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
udpServer = socket(AF_INET, SOCK_DGRAM)
udpServer.bind(ADDR)
while True:
print("waiting for masssage")
data,addr = udpServer.recvfrom(BUFSIZ) # receive
data = data.decode()
udpServer.sendto((ctime()+"--"+ data).encode(),addr)
print("received from and returned to ",addr)
udpServer.close()
2. Create UDP client
# This script is created 1 a UDP The client, which prompts the user for messages to be sent to the server, receives messages prefixed with a timestamp from the server, and then displays them to the user.
from socket import *
HOST = 'localhost'
PORT = 21567 # The port number PORT It should be exactly the same as what you set up for the server (otherwise, communication will not be possible)
BUFSIZ = 1024
ADDR = (HOST, PORT)
udpCS = socket(AF_INET, SOCK_DGRAM)
while True:
data = input("> ")
if not data:
break
udpCS.sendto(data.encode(),ADDR)
data,ADDR = udpCS.recvfrom(BUFSIZ)
if not data:
break
print(data)
udpCS.close()
In fact, the same is true. In python3, the content transmitted using sockets is transmitted as byte, which requires encode for transmission (send/sendto) and decode for reception (recv). It's easy to deal with the problem as long as you grasp the key point.