A tutorial on UDP network programming under Python

Source: Internet
Author: User
TCP is to establish a reliable connection, and both sides of the communication can send data in the form of a stream. The relative tcp,udp is a non-connection oriented protocol.

When using the UDP protocol, you do not need to establish a connection, just need to know the other side's IP address and port number, you can directly send packets. But I don't know if I can get there.

Although the transmission of data with UDP is unreliable, its advantage is that it is faster than TCP, and the UDP protocol can be used for data that does not require reliable arrival.

Let's take a look at how the data is transmitted over the UDP protocol. Like TCP, both sides of the communication using UDP are also divided into clients and servers. The server first needs to bind the port:

s = socket.socket (socket.af_inet, socket. SOCK_DGRAM) # Bound Port: S.bind ((' 127.0.0.1 ', 9999))

When you create a socket, SOCK_DGRAM specifies that the type of socket is UDP. The binding port is the same as TCP, but does not need to call the Listen () method, but instead receives data from any client directly:

print ' Bind UDP on 9999 ... ' While True:  # receives data:  addr = S.recvfrom (1024x768)  print ' Received from%s:%s. '% addr  s.sendto (' Hello,%s ! '% data, addr

The Recvfrom () method returns the address and port of the data and the client, so that when the server receives the data, it can call SendTo () directly to send the data to the client with UDP.

Note that multiple threads are omitted here, because this example is simple.

When a client uses UDP, it still creates a UDP-based socket, and then does not need to call connect () to send data directly to the server via SendTo ():

s = socket.socket (socket.af_inet, socket. SOCK_DGRAM) for data in [' Michael ', ' Tracy ', ' Sarah ']:  # Send data:  s.sendto (' 127.0.0.1 ', 9999)  # receive C3/>print S.recv (1024x768) s.close ()

Receiving data from the server still calls the Recv () method.

The server and client tests are still started with two command lines, with the following results:

Summary

UDP is used similar to TCP, but does not require a connection to be established. In addition, the server-bound UDP port and TCP port do not conflict, that is, UDP 9999 port and TCP 9999 port can be bound separately.

Source Code Reference: Https://github.com/michaelliao/learn-python/tree/master/socket

  • 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.