<< Prev | Beej's Guide to Network Programming (cloned from beej.us) | Next >> |
Hosted at Teoria dei Segnali.it - Blog - Download - Signal Transmission - Internet Application Layer - Wiki - Newsletter |
Receive data on a socket
#include <sys/types.h> #include <sys/socket.h> ssize_t recv(int s, void *buf, size_t len, int flags); ssize_t recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
Once you have a socket up and connected, you can read incoming data from the remote side using the recv() (for TCP SOCK_STREAM sockets) and recvfrom() (for UDP SOCK_DGRAM sockets).
Both functions take the socket descriptor s, a pointer to the buffer buf, the size (in bytes) of the buffer len, and a set of flags that control how the functions work.
Additionally, the recvfrom() takes a
So what wondrous flags can you pass into this function? Here are some of them, but you should check your local man pages for more information and what is actually supported on your system. You bitwise-or these together, or just set flags to 0 if you want it to be a regular vanilla recv().
When you call recv(), it will block until there is some data to read. If you want to not block, set the socket to non-blocking or check with select() or poll() to see if there is incoming data before calling recv() or recvfrom().
Returns the number of bytes actually received (which might be less than you requested in the len parameter), or -1 on error (and errno will be set accordingly.)
If the remote side has closed the connection, recv() will return 0. This is the normal method for determining if the remote side has closed the connection. Normality is good, rebel!
int s1, s2; int byte_count, fromlen; struct sockaddr_in addr; char buf[512]; // show example with a TCP stream socket first s1 = socket(PF_INET, SOCK_STREAM, 0); // info about the server addr.sin_family = AF_INET; addr.sin_port = htons(3490); inet_aton("10.9.8.7", &addr.sin_addr); connect(s1, &addr, sizeof(addr)); // connect to server // all right! now that we're connected, we can receive some data! byte_count = recv(s1, buf, sizeof(buf), 0); printf("recv()'d %d bytes of data in buf\n", byte_count); // now demo for UDP datagram sockets: s2 = socket(PF_INET, SOCK_DGRAM, 0); fromlen = sizeof(addr); byte_count = recvfrom(s2, buf, sizeof(buf), 0, &addr, &fromlen); printf("recv()'d %d bytes of data in buf\n", byte_count); printf("from IP address %s\n", inet_ntoa(addr.sin_addr));
send(), sendto(), select(), poll(), Blocking
<< Prev | Beej's Guide to Network Programming (cloned from beej.us) | Next >> |