UDP with Python

 

UDP with socket programming in python



UDP(User Datagram Protocol)

  • Applications send messages(datagrams) to other hosts on an IP network.  
  • Prior communications are not required to set up communication channels 
  • Is a simple connection-less communication model(no handshakes) with a minimum of protocol mechanism. 
  • There is no guarantee of delivery, ordering, or duplicate protection.
  • Suitable for purposes where error checking and connections are not necessary.  

TCP vs UDP 

TCP 

  • Reliable : TCP manages message acknowledgment, re-transmission and timeouts. If data gets lost along the way, data will be re-sent. 
  • Ordered : even if data segments arrive in wrong order, TCP buffers the out of order data until all data can be properly reordered and delivered to the application 
  • Heavy Weight : TCP requires 3 packets to set up socket communication before any data can be sent. 
  • Streaming : data is divided into segments and are being streamed as byte format. 

UDP 

  • Unreliable : cannot check whether UDP packet has arrived because there is no acknowledgement, re-transmission or timeout. 
  • Not ordered : order of the data packets is not guaranteed 
  • Lightweight : due to no ordering, no tracking of connections.
  • Datagrams : packets are sent individually and are checked for integrity on arrival. 
  • No congestion control: congestion control measures must be implemented at the application level or in the network. 
  • Broadcast : being connection-less, UDP can broadcast. Sent packets can be addressed to all devices within the subnet.  

UDP (socket) programming with python - Client 

  • basic structure of the code is similar to TCP but there exists some difference. 
  • sendto(bytes, address) 
  • Use sendto() instead of send() or sendall() : sendto() method is used to send databrams to a UDP socket. The communication could be from either side. For sendto() to be used, the socket should not be in already connected state.

UDP (socket) programming with python - Server 

  • The main difference from TCP is that we can use recvfrom() method that does not require any addresses. This means the server will listen for any connections from any clients. This recvfrom() method will there by return a tuple (message in bytes, client's address) and we can identify which client sent that message. 
  •  This code has no functions for multi-threading and non-blocking, so it will be executed sequentially. After I figure out some more cool things about threading and non-blocking, I will add it to the code. 

Reference 

Comments

Popular posts from this blog

TCP with Python

Operating System - Learn Thread with C/C++, Python

Buffer related to socket.recv()