I'm writing a point-to-point message queue system, and it has to be able to operate over UDP. I could arbitrarily pick one side or the other to be the "server" but it doesn't seem quite right since both ends are sending and receiving the same type of data from the other.
Is it possible to bind() and connect() both ends so that they send/receive only from each other? That seems like a nicely symmetric way to do it.
UDP is connectionless, so there's little sense for the OS in actually making some sort of connection.
In BSD sockets one can do a
connect
on a UDP socket, but this basically just sets the default destination address forsend
(instead giving explicitly tosend_to
).Bind on a UDP socket tells the OS for which incoming address to actually accept packets (all packets to other addresses are dropped), regardless the kind of socket.
Upon receiving you must use
recvfrom
to identify which source the packet comes from. Note that if you want some sort of authentication, then using just the addresses involved is as insecure as no lock at all. TCP connections can be hijacked and naked UDP literally has IP spoofing written all over its head. You must add some sort of HMACThis page contains some great info about connected versus unconnected sockets: http://www.masterraghu.com/subjects/np/introduction/unix_network_programming_v1.3/ch08lev1sec11.html
This quote answers your question:
I have not used connect() under UDP. I feel connect() was designed for two totally different purposes under UDP vs TCP.
The man page has some brief details on the usage of connect() under UDP: