listen:
Establish monitoring, we need to accept the possibility of establishing a function to be checked
DEF the listen (Self, backlog: int) #backlog specify how many customers allow up to connect to the server. Its value is at least 1. After receiving the connection request, which requests to queue, if the queue is full, it rejects the request.
Note: backlog should be understood as a blocking queue length, total client connection to the server-side, a total backlog + 1 Ge. Blocking queue FIFO, when the end of the connecting client blocking queue in the customer service end of the first successful connection to the server.
accept:
DEF the Accept (Self): "" " the Accept () -> (socket object, address information) wait for an incoming connection returns a new socket. indicates that the connection and the address of the client. For IP sockets, address information is a pair (hostaddr, Port). "" "
accept () accepts a connection request of a client, and returns a new socket, socket server is different from the end () Returns the client listening socket and receive a connection request; communicate with the client via this new transmitting and receiving data on the socket to complete. Each connecting incoming client socket object returns a different client and the client socket belonging accept function by
Test: Suppose server permanently open, provided the listen allows an end user to wait, while starting three clients:
Start the first user terminal, program running
Start the second client, running the program, in which waiting listen first client disconnect
Start the third client, sub client error: onnectionRefusedError: [WinError 10061] Because the target machine actively refused, unable to connect.
import socket sk = socket.socket() sk.bind(('127.0.0.1',8080)) sk.listen(1) while True: conn,addr = sk.accept() while True: ret = conn.recv(1024).decode('utf-8') print(ret) if ret == 'bye': conn.send(b'bye') break msg = input('>>') conn.send(msg.encode('utf-8')) conn.close() sk.close()
import socket sk = socket.socket() sk.connect(('127.0.0.1',8080)) while True: info = input('>>') sk.send(info.encode('utf-8')) ret = sk.recv(1024).decode('utf-8') print(ret) if ret == 'bye': sk.send(b'bye') break sk.close()
import socket sk = socket.socket() sk.connect(('127.0.0.1',8080)) while True: info = input('>>') sk.send(info.encode('utf-8')) ret = sk.recv(1024).decode('utf-8') print(ret) if ret == 'bye': sk.send(b'bye') break sk.close()
import socket sk = socket.socket() sk.connect(('127.0.0.1',8080)) while True: info = input('>>') sk.send(info.encode('utf-8')) ret = sk.recv(1024).decode('utf-8') print(ret) if ret == 'bye': sk.send(b'bye') break sk.close()
Test 2, after starting the UE 1, the UE 1 or 2 off, and then start the UE 3, UE 3 will not be given
to sum up:
listen socket object likened to the train station
accept socket object travelers need to understand reception
If you want to receive visitors, we must first wait for the train station (ie listen listening socket object)
Whether arriving passengers, passengers do not need to wait (ie accept if there is a new socket object created in connection check) at the entrance examination,
If the number exceeds the limit railway station (listen to limit the number of queues), the client sends a request being given, can only wait for the end users are connecting to disconnect in order to successfully enter the queue waiting