.:blog archive:.
- New blogI have decided to move my blogging activities to wordpress!
From ... - Aluminum macbook...As many of you already know, I bought an aluminum macbook (yes the new...
- 4 NovembreC'è da dire che la vittoria di Obama, nonostante abbia dato l...
- VGood evening, London. Allow me first to apologize for this interruptio...
- The reason why I say Italy doesn\'t work...The day msn died... I wonder what happened today, one moment I was chatting away on msn, ...
- What grater picture? Some people do some things. Some people do some other things. Most th...
- Noia.Fumo. Mi rigiro. Mi rivolto. E capisco.
La vita è fatta di... - Good lessons, sorrow, and the Great Unknown It doesn't happen often for me to write a serious blog entry, but I ...
- Cazzatissime!Questo e' quello che succede quando dei mentecatti come noi si trovan...
C Sockets - How to receive split messages
We all know that when we invoke a send() the kernel might decide to split the message we are sending arbitrarily.
But how does the other end behave? How can I receive a message that has been split successfully and easily?
It's relatively simple: you get a blocking recv() in a while loop, inside which you put a loop of non-blocking recv(). This way the first recv() will be waiting for something to be available in the buffer. When something is available, it receives the first part. The subsequent loop retrieves all the following parts, until the buffer is empty. Et voila'! A problem solved easily!
Code snippet:
But how does the other end behave? How can I receive a message that has been split successfully and easily?
It's relatively simple: you get a blocking recv() in a while loop, inside which you put a loop of non-blocking recv(). This way the first recv() will be waiting for something to be available in the buffer. When something is available, it receives the first part. The subsequent loop retrieves all the following parts, until the buffer is empty. Et voila'! A problem solved easily!
Code snippet:
#define MAXDATASIZE 256
int num_bytes,bytes_read,flags,new_fd;
char buf[MAXDATASIZE];
/*
... Open socket, accept connection ...
*/
while ((bytes_read=recv(new_fd, buf, MAXDATASIZE-1, 0)) > 0) {
numbytes = bytes_read;
flags = fcntl(new_fd, F_GETFL, 0);
fcntl(new_fd, F_SETFL, O_NONBLOCK);
while(bytes_read > 0) {
bytes_read=recv(new_fd, &buf[bytes_read], MAXDATASIZE-1-bytes_read, 0);
if (bytes_read>0) numbytes += bytes_read;
}
fcntl(new_fd, F_SETFL, 0);
fcntl(new_fd, F_SETFL, flags & (~O_NONBLOCK));
if (numbytes > 0) {
//DO STUFF
}
}
.:nice links:.
- Morbidicourio - Art and Photography from my friend Charys Palmer
- Levels4You
- Ciaograzie.it
- Hardo.it
- User Friendly
- XKCD
- rm -rf / ??
- Wireless electricity! Goodbye power cord!
- Microsoft sells Linux to Wal-Mart
- Why the Novell Deal is Bad & SCO's Memo in Support of Motion for SJ: We Did Not Breach the GPL
- Apple discontinues UK mail-in repair service
- Google De-indexes Talk.Origins, Won't Say Why
- Self-Recycling Paper
- Anonymizing RFI Attacks Through Google
- LSI Patents the Doubly-Linked List
- RSA crypto attack poses threat to DRM
