<p>I am a bit confused about socket programming in C.</p>

<p>You create a socket, bind it to an interface and an IP address and get it to listen. I found a couple of web resources on that, and understood it fine. In particular, I found an article <s><a href="http://users.actcom.co.il/~choo/lupg/tutorials/internetworking/internet-theory.html" rel="nofollow noreferrer">Network programming under Unix systems</a></s> to be very informative.</p>

<p>What confuses me is the timing of data arriving on the socket.</p>

<p>How can you tell when packets arrive, and how big the packet is, do you have to do all the heavy lifting yourself? </p>

<p>My basic assumption here is that packets can be of variable length, so once binary data starts appearing down the socket, how do you begin to construct packets from that?</p>

When you do a read on the socket, you tell it how many maximum bytes to read, but if it doesn't have that many, it gives you however many it's got.  It's up to you to design the protocol so you know whether you've got a partial packet or not.  For instance, in the past when sending variable length binary data, I would put an int at the beginning that said how many bytes to expect.  I'd do a read requesting a number of bytes greater than the largest possible packet in my protocol, and then I'd compare the first int against however many bytes I'd received, and either process it or try more reads until I'd gotten the full packet, depending.