udp-based socket of network programming (socket) socket

udp-based network programming

  Features: No need to establish a link in advance, you need to obtain ip and port every time you send and receive

     Also known as the datagram protocol, one send corresponds to one receive, and there will be no sticky packet problem

       Unreliable transmission, sending data does not need to reply ACK confirmation information

       There is no link, the startup of the server or client does not need to be sequential

  

udp server socket

1 ss = socket()    #Create a server socket 
2 ss.bind() #Bind        the server socket 
3 inf_loop: #Server        infinite loop 
4 cs = ss.recvfrom()/ss.sendto( ) #Dialogue (receive and send) 
5 ss.close()        

 

socket for udp client

cs = socket()    #Create a client socket 
comm_loop: #Communication       loop cs.sendto 
    ()/cs.recvfrom() #Dialogue    ( send/receive) 
cs.close() #Close                       the client socket

 

 

sample code

Socket communication based on UDP protocol

import socket

server = socket.socket (socket.AF_INET, socket.SOXK_DGRAM)

server.bind(('127.0.0.1',8080))

while True:
    client_data,client_addr=server.recvfrom(1024)
    server.sendto(client_data.upper(),client_addr)
Server
import socket

client = socket.socket (socket.AF_INET, socket.SOCK_DGRAM)

while True:
    msg = input('    : ' ).strip()
    client.sendto(msg.encode('utf-8'),('127.0.0.1',8080))
    res,server_addr =  client.recvfrom(1024)
client

 

*********************

Common port: web port 80

      dns port 53

      dhcp port 67

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324693078&siteId=291194637