write on Non Blocking Socket

How to know whether socket is ready for write.

select(maxfds, (fd_set *)NULL, &writefds, NULL, &timeout); 

By default socket is set for write without checking whether it would block or not? If so how do I know my FD is ready for writing.

  1. select returns > 0 if there are available sockets

  2. FD_ISSET(sock, &writefds) returns true if sock is in the FDSET and the sock descriptor is ready to write. sock is an integer and is one of the sockets you intend to write.

I am connecting to port 7(echo service) in the remote machine.
suppose port 7 is not running in the remote machine I do not want to wait in connect call for long time.. so I am using non blocking connect with timeout of 3 seconds. But I am always getting the return code 1 from select call though the echo service is not running incase of write falgs set.

x=fcntl(S,F_GETFL,0);
fcntl(s,F_SETFL,x | O_NONBLOCK);
rc=connect(s, (struct sockaddr *)&sin, sizeof(sin));
if( errno == EINPROGRESS)
{        
     FD_ZERO(&writeFDs);
     FD_SET(s, &writeFDs);
     rc=select(maxFDs, (fd_set *) NULL, &writeFDs,  (fd_set *)NULL, 
                                                                (struct timeval *)(&timeout));
     if( rc ==0 || rc == -1)
       printf(" Timed out -- Not connected even after 3 secs wait");
    else
    {
	if(FD_ISSET(s,&writeFDs))
       	write(s,"service",7);
    }
}

In the above FD_ISSET is returning true and when it tries to write it retrun EPIPE, because of not connected. How we decide that service is ready for writing?

Closing this thread - looks basically like a double post to me. Continue here:
http://www.unix.com/programming/143701-non-blocking-connect.html\#post302451838