IPPROTO_IP vs IPPROTO_TCP/IPPROTO_UDP

Documentation for socket() on Linux is split between various manpages including ip(7) that specifies that you have to use 0 or IPPROTO_UDP for UDP and 0 or IPPROTO_TCP for TCP. When you use 0, which happens to be the value of IPPROTO_IP, UDP is used for SOCK_DGRAM and TCP is used for SOCK_STREAM. In my … Read more

What is a message boundary?

No, message boundaries have nothing to do with destinations or ports. A “message boundary” is the separation between two messages being sent over a protocol. UDP preserves message boundaries. If you send “FOO” and then “BAR” over UDP, the other end will receive two datagrams, one containing “FOO” and the other containing “BAR”. If you … Read more

how listening to a socket works

A client socket does not listen for incoming connections, it initiates an outgoing connection to the server. The server socket listens for incoming connections. A server creates a socket, binds the socket to an IP address and port number (for TCP and UDP), and then listens for incoming connections. When a client connects to the … Read more

Setting the source IP for a UDP socket

Nikolai, using a separate socket and bind(2) for each address or messing with routing tables is often not a feasible option e.g. with dynamic addresses. A single IP_ADDRANY-bound UDP server should be able to appear to respond on the same dynamically-assigned IP address a packet is received on. Luckily, there is another way. Depending on … Read more

What’s the difference between WebSocket and plain socket communication?

webSockets and regular sockets are not the same thing. A webSocket runs over a regular socket, but runs its own connection scheme, security scheme and framing protocol on top of the regular socket and both endpoints must follow those additional steps for a connection to even be made. You can see the webSocket protocol here: … Read more

Socket and file descriptors

Yes, sockets are also indices into the same table as files. At least for UNIX systems (like Linux and OSX), Windows is different, which is why you can’t use e.g. read and write to receive and send data. Each process has its own “file” descriptor table.