Socket Programming
How to make a BSD socket non-blocking – EWOULDBLOCK
Oct 5th
Sometimes it is needed to make a socket non-blocking so that I/O requests on the socket don’t block. For a non-blocking socket, when I/O requests like read() and write() return a negative value, the errno should be checked against -EWOULDBLOCK code to find if the I/O request would have blocked.
The following utility function sets non-blocking flag of a socket.
int socketSetNonBlocking(int sockfd)
{
unsigned int flags;
flags = fcntl(sockfd. F_GETFL, 0);
if(flags < 0) return -1;
if(fcntl(sockfd, F_SETFL, flags|O_NONBLOCK) < 0) return -1;
return 0;
}
Similarly the following function makes a non-blocking socket a blocking socket.
int socketSetBlocking(int sockfd)
{
unsigned int More > Why my application can’t open more than 1024 or 4096 sockets in Linux
Dec 5th
Typically socket “open” call doesn’t fail unless you have missed out closing the previous connections using “close” system call. Typically Linux/Unix sets a maximum limit for the number of open FDs. That means you can’t keep FDs in open state more than certain number. These settings can’t be changed as a normal user of the system.
These limits can be seen using “ulimit -a” command.
[neo@techpulp ~]# ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 31729 max locked memory (kbytes, -l) 32 max memory More >


Recent Comments