UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2002-07-27 12:01 AM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: 6.1 - How would I put my socket in non-blocking mode?

6
From Andrew Gierth (andrew@erlenstar.demon.co.uk):

Technically, fcntl(soc, F_SETFL, O_NONBLOCK) is incorrect since it clobbers all other file flags. Generally one gets away with it since the other flags (O_APPEND for example) don't really apply much to sockets. In a similarly rough vein, you would use fcntl(soc, F_SETFL, 0) to go back to blocking mode.

To do it right, use F_GETFL to get the current flags, set or clear the O_NONBLOCK flag, then use F_SETFL to set the flags.

And yes, the flag can be changed either way at will.

From: jagadeesh

/* set socket to non-blocking i/o */ 
    sts = ioctl(ccp->main_sock, FIONBIO, (char *)&one); 
    if (sts) 
    { 
        setproderr(PE_TCPERROR, GEL_FATAL); 
        sprintf(line,"ioctl (main) failed - %s",strerror(errno)); 
tcpabort(); 
    }

From: Viswaroopan

Hi,
  I tried this and works great as non-blocking socket. I have a different problem is that, when I made it as non-blocking the accept() on the server comes out immediately with non-block error. Instead I want accept() to wait for some time (set a timeout) before giving that error. Is there any way I can set the timeout on accept().

Thanks in advance.
Vish

From: Jonathan Rynd

This is normal for all socket nonblocking operations: if you call them, you should be prepared to handle 2 cases: 1, they succeed right away, 2, they 'fail' with the "EWOULDBLOCK" non-blocking error (it's not a real failure, it just means "we can't satisfy that right now'. You then have to create a FD_SET structure and use it as input to select() with the proper timeout. See the manpage for select. Depending on the call, when select() returns to indicate success, you may need to make the call again.

From:

sorry, but a little more source code would help me more cause i do know nothing about ioctl and need a nonblocking socket...
cause i want to have a read, that doesent stop the whole program
and, what about this stuff with select()
how do i get a buffer that stores a recived package and give it to me when i ask, if there is a new one, or says no, there is no new package, if there is none

From: jesus

aehrm forgot my email...
sorry, but a little more source code would help me more cause i do know nothing about ioctl and need a nonblocking socket...
cause i want to have a read, that doesent stop the whole program
and, what about this stuff with select()
how do i get a buffer that stores a recived package and give it to me when i ask, if there is a new one, or says no, there is no new package, if there is none

From: Michael Lampkin
Added on: 2002-06-01 00:53:57

Since this is a common question... the follow is sample code showing setting and un-setting for non-blocking on a socket.

int flags; 


/* Set socket to non-blocking */ 

if ((flags = fcntl(sock_descriptor, F_GETFL, 0)) < 0) 
{ 
    /* Handle error */ 
} 


if (fcntl(socket_descriptor, F_SETFL, flags | O_NONBLOCK) < 0) 
{ 
    /* Handle error */ 
} 




/* Set socket to blocking */ 

if ((flags = fcntl(sock_descriptor, F_GETFL, 0)) < 0) 
{ 
    /* Handle error */ 
} 


if (fcntl(socket_descriptor, F_SETFL, flags & (~O_NONBLOCK)) < 0) 
{ 
    /* Handle error */ 
}

Note that the example using ioctl to set non-blocking is perfectly valid and can be used in a similar manner but it should be pointed out that POSIX specifies on fcntl for setting / getting these options.

ML

Offline

Board footer

Powered by FluxBB