Python UDP Instance

Source: Internet
Author: User

UDP communication Process

UDP does not need to go through 3 handshake and 4 waves, do not need to establish a connection in advance, directly send data on the line.

Server Side

Import socketbufsize = 1024ip_port = (' 127.0.0.1 ', 9999) server = Socket.socket (socket.af_inet, socket. SOCK_DGRAM)  # UDP protocol Server.bind (Ip_port) while True:    data,client_addr = Server.recvfrom (BUFSIZE)    print (' Data received by server '    server.sendto (Data.upper (), client_addr) Server.close ()

Client Side

Import socketbufsize = 1024client = Socket.socket (socket.af_inet,socket. SOCK_DGRAM) while True:    msg = input (">>"). Strip ()    Ip_port = (' 127.0.0.1 ', 9999)    Client.sendto ( Msg.encode (' Utf-8 '), ip_port)    data,server_addr = Client.recvfrom (BUFSIZE)    print (' Client Recvfrom ', Data,server _ADDR) Client.close ()

Output results

Server:server received data b ' Hello ' server received data B ' world ' client:>> Hello client recvfrom  B ' Hello ' (' 127.0.0.1 ', 9999) >> World Client Recvfrom  

  

Adhesive packet Analysis The first kind:

Server

From socket import *import timeserver=socket (Af_inet,sock_dgram) server.bind ((' 127.0.0.1 ', 8880)) res1= Server.recvfrom #b ' Hello ' Print (' First: ', res1) Res2=server.recvfrom (1024x768) #b ' World ' Print (' second: ', Res2) Server.close ()

  

Client

From socket import *import timeclient = socket (af_inet, sock_dgram) client.sendto (b ' Hello ', (' 127.0.0.1 ', 8880)) Client.sendto (b ' World ', (' 127.0.0.1 ', 8880)) Client.close ()

Server end result: no sticky pack generated

First time: (b'hello', ('127.0.0.1', 63959)) Second time: (b'  World', ('127.0.0.1', 63959))

The second type:
Server:from Socket Import *import timeserver=socket (Af_inet,sock_dgram) server.bind ((' 127.0.0.1 ', 8880)) res1= Server.recvfrom (2) #b ' He ' print (' first time: ', res1) time.sleep (6) Res2=server.recvfrom (1024x768) #b ' World ' Print (' second: ', Res2) Server.close () client:from socket import *import timeclient = socket (af_inet, sock_dgram) client.sendto (b ' Hello ', (' 127.0.0.1 ', 8880)) Time.sleep (5) client.sendto (b ' World ', (' 127.0.0.1 ', 8880)) Client.close ()

Output results

Windows Direct error: Traceback (most recent calls last):/luffy/Third module/Chapter II network Programming/06 UDP protocol-based sockets/service-side. Py ", line, in <module>    Res1=server.recvfrom (2) #b ' he ' OSError: [Winerror 10040] A message sent on a datagram socket is larger than the internal message buffer or some other network restriction, or the buffer used by the user to receive datagrams is smaller than the datagram. Mac or Linux: Directly drops the packet, only receives B ' he ', the back of the Llo will not receive

  

TCP VS UDP

TCP Link-based communication

    • Based on the link, the listen is required to specify the size of the 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)

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

 

  

Python UDP Instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.