The socket_udp of Python

Source: Internet
Author: User

UDP Service Side & client programming

" "UDP programming creates a socket object, socket. Sock_dgram bind IP and Port,bind () method transfer data 1. Receive data, Socket.recvfrom (Bufsize[,flags]), get a 2-yuan ancestor (string,address) 2. Send data, SOCK Et.sendto (string,address), send to an address information release resources" "ImportSocketserver= Socket.socket (type=socket. SOCK_DGRAM) Server.bind (('0.0.0.0', 9999)) Data= SERVER.RECV (1024)#Blocking Wait Datadata = Server.recvfrom (1024)#Blocking wait data (value, (ip,port))Server.sendto (b'Hello',('127.0.0.1', 10000) ) Server.close ()" "the UDP client programming process creates the socket object, socket. Sock_dgram send data, socket.sendto (string,address) sent to an address information to receive data, Socket.recvfrom (Bufsize[,flags]), to get a 2-yuan ancestor (string, Address) to release resources" "Client= Socket.socket (type=socket. SOCK_DGRAM) raddr= ('127.0.0.1', 10000) Client.connect (raddr) client.sendto (b'Hello', raddr) data= CLIENT.RECV (1024)#Blocking Wait Datadata = Client.recvfrom (1024)#Blocking wait data, (value, (ip,port))Client.close ()
Note: There is no connection protocol on UDP, so there can be only one end, such as client data destined to the server, server presence or not important UDP socket object created, when no local address and Port bind ()        can specify the local address and Port LADDR, The immediate use of Connect ()   can immediately occupy the local address and port, fill the remote address and Port Raddrsendto ()     can immediately occupy the local address and port, and send the data to the specified remote, only with the local bound port, SendTo can send data to any far end ()        need to use with connect (), you can use the data from the local port to the RADDR specified remote recv ()         requirements must be occupied by the local port, Returns the received Data Recvfrom ()   requires that the local port must be occupied, returning a two-tuple of the received data and the peer address

  

The socket_udp of Python

Related Article

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.