Hi,

I seem to have a problem with non-blocking writes to TCP sockets and 
unix-domain sockets - they block.

The attached program illustrates it - it creates a socketpair(), sets one end 
to non-blocking and writes to it - expecting to get an EWOULDBLOCK. On my 
system it hangs.

With pipe() it works fine.

Please advise.
Thanks,
Uri

#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>

#define BASIC_UNIT      1024*1024
//#define BASIC_UNIT    1
char large_buf[BASIC_UNIT];

void fd_pair(int *fd)
{
/*      if(pipe(fd)<0)
                perror("pipe"); */
        if(socketpair(AF_UNIX,SOCK_STREAM,0,fd) <0)
                perror("socketpair");
}

void set_nonblocking(int fd)
{
        int val=fcntl(fd,F_GETFL);
        if(val==-1)
                perror("fcntl");
        val|=O_NONBLOCK;
        fcntl(fd,F_SETFL,val);
}

int main(int argc,char *argv[])
{
        int fd[2],rc;
        fd_pair(fd);
          
        set_nonblocking(fd[1]);

        while(1){
                rc=write(fd[1],large_buf,sizeof(large_buf));
                if(rc<0){
                        if(errno==EWOULDBLOCK)
                                printf("blocking, thank you, goodbye.\n");
                        else
                                perror("write");

                        break;
                }
        }

        return 0;
}

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

Reply via email to