How do I set a socket to non-blocking?

How do I set a socket to non-blocking?

Post by Dani » Sun, 04 Aug 1996 04:00:00



I am creating a client, and keep coming up with Operation would block when
I try to do a recv. How can I fix this? I have tried things like:
fcntl(sockfd, F_SETFL, O_NONBLOCK)

and

FD_ZERO(&readfds);
FD_SET(sockfd, &readfds);

but theses seem not to do anything. Can anyone tell me what I am doing
wrong, or how I could set the socket to non blocking?

 
 
 

How do I set a socket to non-blocking?

Post by Andrew Gier » Sun, 04 Aug 1996 04:00:00


 Daniel> I am creating a client, and keep coming up with Operation
 Daniel> would block when I try to do a recv. How can I fix this? I
 Daniel> have tried things like: fcntl(sockfd, F_SETFL, O_NONBLOCK)

Sounds like that one worked...

 Daniel> and

 Daniel> FD_ZERO(&readfds);
 Daniel> FD_SET(sockfd, &readfds);

er.. the above does nothing useful, unless in conjuction with a call to
select()

 Daniel> but theses seem not to do anything. Can anyone tell me what I
 Daniel> am doing wrong, or how I could set the socket to non
 Daniel> blocking?

You *did* set it to nonblocking.

Nonblocking means that every function call on the socket will return
immediately, rather than waiting for something to happen. If the call
can complete immediately, e.g. for recv, if there is any data available,
then the usual success status is returned. If the call cannot complete,
then it returns -1 with errno == EWOULDBLOCK or EAGAIN.

If you need to wait for something to happen on a socket that is in
nonblocking mode, use select().

Hope this helps

--


 
 
 

How do I set a socket to non-blocking?

Post by Paul Carlt » Thu, 08 Aug 1996 04:00:00



:  Daniel> I am creating a client, and keep coming up with Operation
:  Daniel> would block when I try to do a recv. How can I fix this? I
:  Daniel> have tried things like: fcntl(sockfd, F_SETFL, O_NONBLOCK)

: Sounds like that one worked...

:  Daniel> and

:  Daniel> FD_ZERO(&readfds);
:  Daniel> FD_SET(sockfd, &readfds);

: er.. the above does nothing useful, unless in conjuction with a call to
: select()

:  Daniel> but theses seem not to do anything. Can anyone tell me what I
:  Daniel> am doing wrong, or how I could set the socket to non
:  Daniel> blocking?

: You *did* set it to nonblocking.

: Nonblocking means that every function call on the socket will return
: immediately, rather than waiting for something to happen. If the call
: can complete immediately, e.g. for recv, if there is any data available,
: then the usual success status is returned. If the call cannot complete,
: then it returns -1 with errno == EWOULDBLOCK or EAGAIN.

: If you need to wait for something to happen on a socket that is in
: nonblocking mode, use select().

: Hope this helps

: --

This works for me:

#include <netinet/tcp.h>

...

      int ret = setsockopt(iDeviceId, SOL_SOCKET, TCP_NODELAY,
                           (char*)&sockOpt, sizeof(sockOpt));
      if (ret < 0) {
         perror("setsockopt");
         printf("Ret = %d\n", ret);
      }

 
 
 

How do I set a socket to non-blocking?

Post by Andrew Gier » Fri, 09 Aug 1996 04:00:00


[discussion of nonblocking sockets]

 Paul> This works for me:

 Paul> #include <netinet/tcp.h>

 Paul> ...

 Paul>       int ret = setsockopt(iDeviceId, SOL_SOCKET, TCP_NODELAY,
 Paul>                          (char*)&sockOpt, sizeof(sockOpt));
 Paul>       if (ret < 0) {
 Paul>          perror("setsockopt");
 Paul>          printf("Ret = %d\n", ret);
 Paul>       }

Well, it may appear to work, but it is a) incorrect and b) has no relevance
to the problem under discussion.

We were talking about *nonblocking* mode, which is set by using
fcntl(O_NONBLOCK) or ioctl(FIONBIO), and causes read() and write() calls to
return EAGAIN or EWOULDBLOCK when no data can be transferred.

What you appear to be doing is trying to disable the Nagle algorithm, which
affects the timing of sent segments (see the socket faq, or a good TCP/IP
book), but has *absolutely no effect* on read() and write().

Your code is incorrect in that TCP_NODELAY is not an option at SOL_SOCKET
level, but at IPPROTO_TCP. Some implementations appear to ignore the level
parameter, however.

The socket faq is regularly posted here and in comp.answers, and may be
found at:

  http://www.auroraonline.com/sock-faq
  http://kipper.york.ac.uk/~vic/sock-faq
  ftp://rtfm.mit.edu/pub/usenet/news.answers/unix-faq/socket

--

 
 
 

1. How do I set a non-blocking socket back to blocking?

I have a socket that is set non-blocking with

        fcntl(fd, F_SETFL, FNDELAY);

and would like to set it back to blocking. What do I do? Also, since this  
is the "BSD" way what is now the portable manner of setting sockets  
blocking/non-blocking?

--
        Peter A. Korp
        Assistant Scientist
        Argonne National Laboratory

2. Beta Testers Needed

3. Blocking and Non-Blocking socket

4. scan with a Mustek 6000 P under linux ?

5. Non-blocking socket reads block! (Bug?)

6. Mircosoft Tax

7. Difference between blocking and non-blocking socket ?

8. Suppressing error redirection codes

9. What is difference of SYNC, ASYNC, BLOCKING, NON-BLOCKING sockets?

10. Non-blocking sockets on SunOS 4 are blocking

11. select() behavior for Blocking and non-blocking sockets.

12. Non blocking socket blocks; says 'read would block' ?

13. Sockets: Non Blocking I/O, How do I know when its done?