UDP-based sockets
Non-connected unreliable data transmission, can be no server side, but no server side, the data sent will be directly discarded, and can not reach the server side
Because UDP is non-connected (in fact there is a link, otherwise through what to pass data to fetch data), you can use multiple clients to connect the server side, but this is not concurrent access.
Attention:
1. Messages are sent to the sending buffer on their own side, and the received messages are received from the buffers in their own
Tcp:send Send Message, recv receive message
Udp:sendto Send Message, recvfrom receive message
2. TCP is based on data flow, and UDP is datagram-based:
Send (Bytes_data): sends the data stream, the data stream Bytes_data if is empty, own this part of the buffer also is empty, the operating system does not control the TCP protocol to send the empty packet
Sendinto (Bytes_data,ip_port): Send a datagram, Bytes_data is empty, and ip_port, all even send empty bytes_data, the datagram is not empty, their own buffer to receive the contents of the end, The operating system will control the UDP protocol packet.
3.1 TCP protocol
(1) If the data in the message buffer is empty, then the recv will be blocked (blocking is simple, is waiting to receive)
(2) Only the TCP protocol client send an empty data is really empty data, the client even if there are infinite send empty, also with no one.
(3) TCP link-based communication
- Based on the link, the listen is required, specifying the size of the half-connection pool
- Based on the link, the server must run first, and then the client initiates the link request
- For Mac Systems: If one end of the link is broken, the other end of the link is also finished recv will not block, received is empty (workaround is: The service side after receiving the message plus if judgment, the empty message will break off the communication loop)
- For Windows/linux Systems: If one end of the link is broken, then the other end of the link is also finished recv will not be blocked, received is empty (workaround is: server-side communication loop with exception handling, catch the exception after the break off the communication loop)
3.2 UDP protocol
(1) If the data in the message buffer is "empty", Recvfrom will also block
(2) Only the UDP protocol client sendinto an empty data is not really empty data (including: empty data + address information, the resulting report will still not be empty), so as long as the client has a sendinto (whether or not to send empty data, is not really empty data), The server can recvfrom to the data.
(3) UDP no link
- No links, so no listen, no more connection pool
- No link, UDP sendinto do not have a running server, you can kept the message, but the data is lost
- Recvfrom the data received is less than the data sent by Sendinto, the data is lost directly on Mac and Linux system, and it is sent on the Windows system more direct error than received
- Only sendinto send data without recvfrom data, data loss
Implementation code
Service side:
#导入socket模块import socket# Create Socketskt = Socket.socket (socket.af_inet,socket. SOCK_DGRAM) #绑定地址和端口skt. Bind ((' 127.0.0.1 ', 9090)) #循环while True: #调用接受消息 data,addr = Skt.recvfrom (1024x768) #接受成功回复消息 rst = B ' I am fine ' skt.sendto (rst,addr) print (' Server done ') #关闭链接 skt.close ()
Client
#导入模块import socket# Create Socketskt = Socket.socket (socket.af_inet,socket. SOCK_DGRAM) #创建发送消息和发送目标msg = B ' Hello world ' addr = (' 127.0.0.1 ', 9090) skt.sendto (msg,addr) #接受回复rst = Skt.recvfrom (1024) Print (RST) print (' client done ') #关闭链接skt. Close ()
Python learning--socket sockets (UDP connection)