Blocking and Non-Blocking socket

Blocking and Non-Blocking socket

Post by Chris » Wed, 22 Jan 2003 09:14:47



Hi,

I have a socket that I use for read/write. When created, the socket is
blocking by default. I want it to be blocking sometimes and
non-blocking other times. I have succeeded in switching from Blocking
to Non-blocking using

      fcntl(socket, F_SETFL, O_NONBLOCK);

but now I want to set it back to blocking. Is there something like

      fcntl(socket, F_SETFL, O_BLOCK);

around? I cannot find a way to set it back to blocking. Thanks for any
suggestions!

-Chris

 
 
 

Blocking and Non-Blocking socket

Post by Michael Fu » Wed, 22 Jan 2003 10:29:02



> I have a socket that I use for read/write. When created, the socket is
> blocking by default. I want it to be blocking sometimes and
> non-blocking other times. I have succeeded in switching from Blocking
> to Non-blocking using

>       fcntl(socket, F_SETFL, O_NONBLOCK);

To be safe, it's better to call fcntl() with F_GETFL to get the
current flags, then add O_NONBLOCK, then call fcntl() with F_SETFL.
There could be other flags set that you might want to keep.

Quote:> but now I want to set it back to blocking. Is there something like

>       fcntl(socket, F_SETFL, O_BLOCK);

> around? I cannot find a way to set it back to blocking. Thanks for any
> suggestions!

Call fcntl() with F_GETFL to get the flags, then do something like
this:

flags &= ~O_NONBLOCK;

Then call fcntl() with F_SETFL to set the flags.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

 
 
 

Blocking and Non-Blocking socket

Post by David Schwart » Wed, 22 Jan 2003 10:30:48



> Hi,

> I have a socket that I use for read/write. When created, the socket is
> blocking by default. I want it to be blocking sometimes and
> non-blocking other times. I have succeeded in switching from Blocking
> to Non-blocking using

>       fcntl(socket, F_SETFL, O_NONBLOCK);

> but now I want to set it back to blocking. Is there something like

>       fcntl(socket, F_SETFL, O_BLOCK);

> around? I cannot find a way to set it back to blocking. Thanks for any
> suggestions!

> -Chris

        The F_SETFL subfunction sets the flags *to* the specified value. It
does not set the specified flags in addition to any current flags. So

        fcntl(socket, F_SETFL, 0);

        should do what you want. It's good practice to do an F_GETFL to
retrieve the flags, set or clear the flags of interest, and then do an
F_SETFL with the result.

        DS

 
 
 

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

Hi all,

I have three sockets in server which I am reading in a continuous loop,
so don't want to wait on any one. I have designated them as 'non blocking'
but when run the server exits on the very first read() and says 'would
block'. (Is it the same as 'EWOULDBLOCK' mentioned in R. Steven's book ?)
I don't want that I should get any error msg and exit, in fact the
whole purpose of nonblocking is that if something is available, read it
else proceed in the loop and come back to read next time.
The client (sender) is sending msgs on all three sockets. The sockets
are conn_oriented. I am using SunOS 4.1. The small code part is below:

int emer_s, env_s, sens_s;            /* socket fds */
//----------- making sockets nonblocking ******
if ((res=fcntl(emer_s,F_SETFL,FNDELAY)) < 0)
        {
          perror("fcntl res = -1");
          exit(1);
        }
if ((res=fcntl(env_s,F_SETFL,FNDELAY)) < 0)
        {
          perror("fcntl res = -1");
          exit(1);
        }
if ((res=fcntl(sens_s,F_SETFL,FNDELAY)) < 0)
        {
          perror("fcntl res = -1");
          exit(1);
        }

for(; ;)
   {

      if ((cc=read(emer_s,(char*)&gen_struct,size)) < size)
        {
        perror("read error");
          exit(1);
        }
......... do something
      if ((cc=read(env_s,(char*)&gen_struct,size)) < size)
        {
         perror("read error");
          exit(1);
        }
        ......... do something
      if ((cc=read(sens_s,(char*)&gen_struct,size)) < size)
        {
          perror("read error");
          exit(1);
        }
        ......... do something

   }

Any help is appreciated.

hashmi

--
-----
Atiqullah Hashmi                    
UTA (Univ. of Texas at Arlington)  

2. login console question...

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

4. Andrew word processor

5. Difference between blocking and non-blocking socket ?

6. mysql-3.23.33 problem

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

8. Who Else Can't mount VFAT in RH 6.0

9. Non-blocking sockets on SunOS 4 are blocking

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

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

12. changing socket to blocking after non blocking connect

13. Blocking connect() fails, non-blocking succeeds?