UDP sockets
UDP or user datagram protocol is an alternative protocol to its more common counterpart TCP. UDP like TCP is a protocol for packet transfer from 1 host to another, but has some important differences. UDP is a connectionless and non-stream oriented protocol. It means a UDP server just catches incoming packets from any and many hosts without establishing a reliable pipe kind of connection.
In this article we are going to see how to use UDP sockets in python. It is recommended that you also learn about .
Create udp sockets
A udp socket is created like this
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
The SOCK_DGRAM specifies datagram (udp) sockets.
Sending and Receiving
Since udp sockets are non connected sockets, communication is done using the socket functions sendto and recvfrom. These 2 functions dont require the socket to be connected to some peer. They just send and receive directly to and from a given address
Udp server
The simplest form of a udp server can be written in a few lines
import socket
port = 5000
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("", port))
print "waiting on port:", port
while 1:
data, addr = s.recvfrom(1024)
print data
A udp server has to open a socket and receive incoming data. There is no listen or accept. Run the above server from a...
Read full post here
Programming udp sockets in python
UDP or user datagram protocol is an alternative protocol to its more common counterpart TCP. UDP like TCP is a protocol for packet transfer from 1 host to another, but has some important differences. UDP is a connectionless and non-stream oriented protocol. It means a UDP server just catches incoming packets from any and many hosts without establishing a reliable pipe kind of connection.
In this article we are going to see how to use UDP sockets in python. It is recommended that you also learn about .
Create udp sockets
A udp socket is created like this
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
The SOCK_DGRAM specifies datagram (udp) sockets.
Sending and Receiving
Since udp sockets are non connected sockets, communication is done using the socket functions sendto and recvfrom. These 2 functions dont require the socket to be connected to some peer. They just send and receive directly to and from a given address
Udp server
The simplest form of a udp server can be written in a few lines
import socket
port = 5000
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("", port))
print "waiting on port:", port
while 1:
data, addr = s.recvfrom(1024)
print data
A udp server has to open a socket and receive incoming data. There is no listen or accept. Run the above server from a...
Read full post here
Programming udp sockets in python