Beej's Guide to Network Programming (1994-2023)
beej.us
beej.us
I'm trying to finish draft 1 of my C Guide (which has turned into a monster--never attempt to write a _comprehensive_ language guide), and it's comments like these that really keep me going. Your kind words literally bring tears to my eyes.
I'm just genuinely so glad that it has been so helpful, and I never would have guessed it would have been useful for so long. And I'm smugly happy to contribute to the information-sharing ad-free small-web Internet in my own little way.
And thank you to everyone who has read it and to everyone who has sent in corrections and bug reports. I still do update it!
But thank you for the labor of love that these guides are.
back in the late 90's/early aughts it was absolutely the best way to learn network programming using BSD sockets. It originally picked it up to better understand circlemud code in college, it will always hold a special place in my heart.
I think maybe I should get a copy of the new version because I don't understand IPV6 at all.
It's one of the resources to learn networking.
The thing I've realised is that I don't understand what a connection actually is. So I don't understand this bit
"Why are they connectionless? Well, basically, it’s because you don’t have to maintain an open connection as you do with stream sockets. You just build a packet, slap an IP header on it with destination information, and send it out. No connection needed"
How can anything be sent with no connection? What is a connection?
With UDP, you build a packet, slap an IP header on it, and send it out in the hopes that the other side receives it.
With TCP, you can't just send data, you have to perform a three-way handshake first: send a packet with the SYN flag set, receive a SYN-ACK, and if you received a SYN-ACK send an ACK back.
Stateful firewalls, for instance, track the connection state for Network Address Translation (NAT) or firewalling purposes. When a TCP connection is opened (SYN), the connection is considered 'new'. After the handshake is completed (SYN-ACK, ACK), the state changes to 'established'. The lifetime of the connection ends after a TCP packet with the RST or FIN flags set, and the state changes to 'closed'.
Cool, related things to read up on: Linux conntrack, TCP reordering and retransmission, Stream Control Transmission Protocol (SCTP), Multi-Path TCP (MPTCP), Internet Control Message Protocol (ICMP, also known as "ping").
So is a connection really just the maintained state in both the sender and receiver machines. What is maintained in that state? The ACK flags and the IP of the other machine?
TCP - connection centered (without going into nitty-gritty) it sets up a connection between two communicating computers that has "state"; connection setup, connection ongoing, and connection teardown. The data may arrive out of order, but the "receiver" of a stream will rearrange them within a limited window, so there are in order, and acknowledge reception of the packets as part of the protocol (TCP), higher layers like your software generally can rely on it for that.
UDP: connectionless, just label the packet with the destination and send it off. No acknowledgement at the UDP layer that the packet was ever received. No order guarantees. The upper layer (usually your software) must manage reception of the packet, ordering, AND request the packet be sent again in those cases where they -must- be received (like a file). Some applications don't require that (say you're talking on a phone call and 1 out of 1000 sound packets drop. It doesn't matter, as the packet is useless if it's outside a few milliseconds of being sent. It's simply dropped, and no request is sent back to try and get it again.
I hope I got that helps :)
These refer to 2 different protocols at the same layer: UDP, and TCP. Below these protocols there are layers that route little messages around local networks (like Ethernet) and that route little messages across the Internet (like IP), and UDP and TCP are another layer of abstraction on top of that.
TCP adds information about the ordering of messages, it has a mechanism for acknowledging receipt of a message, it has logic for resending a message, etc. UDP does not include these concepts, so if a message gets lost somewhere in the network, an application using UDP might not even notice.
The "connection" in this case, really refers to the state about these messages that is kept. It's a virtual / logical connection, not a physical connection.
But yes - both sides maintain state about the connection. Senders will re-send packets if they go too long without being acknowledged. They will slow down large batches of sending if lots of messages are being dropped (essentially throttling in case the network is being overwhelmed and they're making it worse). And receivers will acknowledge packets as they're received and assemble messages back in the right order if packets arrive out-of-order, or bits in the middle are missing.
Think of sending a text (connectionless) vs making a phone call (connection-oriented. why it's not called "connectionful" is beyond me).
When you send a text, you hope that the recipient gets it. If they don't, you send it again until they do.
Whereas when you make a phone call, the call is live the minute you dial the number. You're not always talking to the recipient, though; you might be waiting for them to pick up, trying again because the number is busy, in call waiting, etc.
(Packets transmitted via VoIP phone calls are usually UDP and texts _can be_ sent over TCP IIRC, but that's neither here nor there :D)
Connectionless: Drop a postcard in the mail and stop thinking about it. Maybe it gets there, maybe not.
Connection-oriented: Start a correspondence. If you don't get a response after a while, send your letter again. If you get parts 1, 2, and 4 you say "Hey, I missed #3, send that again please!" You keep thinking about the flow of conversation whether you have a message in hand right now or not.
The "connection" is in your memory. On a computer, that may involve persistent memory use on network hardware, by your OS kernel, and in the program sending the messages while the connection exists.
UDP is a stateless protocol. If you listen on port 12345, you will get every packet that comes in on port 12345. Some packets sent won't make it, others might arrive twice, others might arrive out-of-order.
An Engineering Approach to Computer Networking : ATM Networks, The Internet, and The Telephone Network by S.Keshav
To understand a TCP connection, e.g., I imagine a book from the 1980s would be perfectly fine.
Think of a wired network (TCP) vs radio/television broadcast (UDP). There really never was a "connection" it is just a logical concept/abstraction that means an endpoint can be reached, when no data is sent/received there is no connection.
Broadcast or UDP is a "connection" but more like a tuner. UDP datagrams are sent out and don't need to be ACK'd and may never be received, they just broadcast to where you tell them to go. They might even be received out of order where you can discard previous ones if they aren't needed. Note: You can do Reliable UDP and ACK any important messages with a UDP datagram back. Most highly available real-time systems and games use UDP with some sprinkle of RUDP when needed. Example: player positions or actions across the level don't matter to you, can be received and rendered or not. Global state like level starting, level ending, you want to ACK those back to unify the simulation on important states. Any critical message you mark for ACK thus the "reliable" part, it also handles discarding out of order messages which happens with UDP broadcast.
Wired or TCP is more like a stream and has a "connection" and handles all the ordering, verification and ACK backs for you. This has lots of overhead and isn't great for gaming beyond like turn based or simple networking, it works great for sending a web page or file though because all parts are necessary.
Streaming or SCTP-like it really RUDP with more standards around it. It is a combination of the TCP where needed and UDP and can be direct or broadcast.
All types of "connections" are really virtual/logical connections not actual connections.
Gaffer on Games also has some great overviews on these topics and is a must read like Beej's.
https://gafferongames.com/categories/game-networking/
This article on "virtual connections" may help you grok it.
https://gafferongames.com/post/virtual_connection_over_udp/
Reliable UDP started to be popular with enet and RakNet, and is the basis of most good networking systems and netcode today.
https://en.wikipedia.org/wiki/Reliable_User_Datagram_Protoco...
enet
RakNet
http://www.jenkinssoftware.com/
WebRTC is also UDP based and can do RUDP, also uses lots of the NAT/punchthrough techniques from RakNet and others
The other reference that I really like to give students about networks is Michal Zalewski's Silence on the wire [1]. A really great introduction that can be read cover to cover — almost as a novel — despite being really technical.
I recommend you start reading at section 2.