<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>

[{'answer_id': 48910, 'author': 'Paul Tomblin', 'author_id': 3333, 'author_profile': 'https://Stackoverflow.com/users/3333', 'pm_score': 2, 'selected': False, 'text': "<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.</p>\n"}, {'answer_id': 48912, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 1, 'selected': False, 'text': "<p>Sockets operate at a higher level than raw packets - it's like a file you can read/write from. Also, when you try to read from a socket, the operating system will block (put on hold) your process until it has data to fulfill the request.</p>\n"}, {'answer_id': 48929, 'author': 'dfjacobs', 'author_id': 4848, 'author_profile': 'https://Stackoverflow.com/users/4848', 'pm_score': 5, 'selected': True, 'text': "<p>Short answer is that you have to do all the heavy lifting yourself.  You can be notified that there is data available to be read, but you won't know how many bytes are available.  In most IP protocols that use variable length packets, there will be a header with a known fixed length prepended to the packet.  This header will contain the length of the packet.  You read the header, get the length of the packet, then read the packet.  You repeat this pattern (read header, then read packet) until communication is complete.</p>\n\n<p>When reading data from a socket, you request a certain number of bytes.  The read call may block until the requested number of bytes are read, but it can return fewer bytes than what was requested.  When this happens, you simply retry the read, requesting the remaining bytes.</p>\n\n<p>Here's a typical C function for reading a set number of bytes from a socket:</p>\n\n<pre><code>/* buffer points to memory block that is bigger than the number of bytes to be read */\n/* socket is open socket that is connected to a sender */\n/* bytesToRead is the number of bytes expected from the sender */\n/* bytesRead is a pointer to a integer variable that will hold the number of bytes */\n/*           actually received from the sender. */\n/* The function returns either the number of bytes read, */\n/*                             0 if the socket was closed by the sender, and */\n/*                            -1 if an error occurred while reading from the socket */\nint readBytes(int socket, char *buffer, int bytesToRead, int *bytesRead)\n{\n    *bytesRead = 0;\n    while(*bytesRead &lt; bytesToRead)\n    {\n        int ret = read(socket, buffer + *bytesRead, bytesToRead - *bytesRead);\n        if(ret &lt;= 0)\n        {\n           /* either connection was closed or an error occurred */\n           return ret;\n        }\n        else\n        {\n           *bytesRead += ret;\n        }\n    }\n    return *bytesRead;\n}\n</code></pre>\n"}, {'answer_id': 49131, 'author': 'Dominic Eidson', 'author_id': 5042, 'author_profile': 'https://Stackoverflow.com/users/5042', 'pm_score': 4, 'selected': False, 'text': '<p>So, the answer to your question depends a fair bit on whether you are using UDP or TCP as your transport.</p>\n\n<p>For UDP, life gets a lot simpler, in that you can call recv/recvfrom/recvmsg with the packet size you need (you\'d likely send fixed-length packets from the source anyway), and make the assumption that if data is available, it\'s there in multiples of packet-length sizes. (I.E. You call recv* with the size of your sending side packet, and you\'re set.)</p>\n\n<p>For TCP, life gets a bit more interesting - for the purpose of this explanation, I will assume that you already know how to use socket(), bind(), listen() and accept() - the latter being how you get the file descriptor (FD) of your newly made connection.</p>\n\n<p>There are two ways of doing the I/O for a socket - blocking, in which you call read(fd, buf, N) and the read sits there and waits until you\'ve read N bytes into buf - or non-blocking, in which you have to check (using select() or poll()) whether the FD is readable, and THEN do your read().</p>\n\n<p>When dealing with TCP-based connections, the OS doesn\'t pay attention to the packet sizes, since it\'s considered a continual stream of data, not seperate packet-sized chunks.</p>\n\n<p>If your application uses "packets" (packed or unpacked data structures that you\'re passing around), you ought to be able to call read() with the proper size argument, and read an entire data structure off the socket at a time. The only caveat you have to deal with, is to remember to properly byte-order any data that you\'re sending, in case the source and destination system are of different byte endian-ness. This applies to both UDP and TCP.</p>\n\n<p>As far as *NIX socket programming is concerned, I highly recommend W. Richard Stevens\' "Unix Network Programming, Vol. 1" (UNPv1) and "Advanced Programming in an Unix Environment" (APUE). The first is a tome regarding network-based programming, regardless of the transport, and the latter is a good all-around programming book as it applies to *NIX based programming. Also, look for "TCP/IP Illustrated", Volumes 1 and 2.</p>\n'}]
['https://Stackoverflow.com/questions/48908', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/277/']