Python's UDP programming

Source: Internet
Author: User

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.

Server

#-*-Coding:utf8-*-Importsocket, ThreadingdefUdplink (S, data, addr):Print 'Received from%s:%s'%addr, Data s.sendto ('Hello,%s'%data, addr)#sock_dgram Specifies that the type of socket is UDPs =Socket.socket (socket.af_inet, socket. SOCK_DGRAM)#Binding PortS.bind (('127.0.0.1', 9999))#UDP programming does not require monitoringPrint 'Bind UDP on 9999 ...' whileTrue:data, addr= S.recvfrom (1024) T= Threading. Thread (Target=udplink, args=(S, data, addr)) T.start ()

Client

#-*-Coding:utf8-*-Importsocket, times=Socket.socket (socket.af_inet, socket. SOCK_DGRAM) forDatainch['David','Yt','Amenda']:    #Send data:S.sendto (Data, ('127.0.0.1', 9999))    #Receive data:    PrintS.RECV (1024) Time.sleep (2) S.close ()

2015-05-11

Python's UDP programming

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.