How can I set the timeout for the connect() system call?
Submitted by: AdministratorNormally you cannot change this. Solaris does let you do this, on a per-kernel basis with the ndd tcp_ip_abort_cinterval parameter.
The easiest way to shorten the connect time is with an alarm() around the call to connect(). A harder way is to use select(), after setting the socket nonblocking. Also notice that you can only shorten the connect time, there's normally no way to lengthen it.
First, create the socket and put it into non-blocking mode, then call connect(). There are three possibilities:
o connect succeeds: the connection has been successfully made (this usually only happens when connecting to the same machine)
o connect fails: obvious
o connect returns -1/EINPROGRESS. The connection attempt has begun, but not yet completed.
If the connection succeeds:
o the socket will select() as writable (and will also select as readable if data arrives)
If the connection fails:
o the socket will select as readable *and* writable, but either a read or write will return the error code from the connection attempt. Also, you can use getsockopt(SO_ERROR) to get the error status - but be careful; some systems return the error code in the result parameter of getsockopt, but others (incorrectly) cause the getsockopt call *itself* to fail with the stored value as the error.
Submitted by: Administrator
The easiest way to shorten the connect time is with an alarm() around the call to connect(). A harder way is to use select(), after setting the socket nonblocking. Also notice that you can only shorten the connect time, there's normally no way to lengthen it.
First, create the socket and put it into non-blocking mode, then call connect(). There are three possibilities:
o connect succeeds: the connection has been successfully made (this usually only happens when connecting to the same machine)
o connect fails: obvious
o connect returns -1/EINPROGRESS. The connection attempt has begun, but not yet completed.
If the connection succeeds:
o the socket will select() as writable (and will also select as readable if data arrives)
If the connection fails:
o the socket will select as readable *and* writable, but either a read or write will return the error code from the connection attempt. Also, you can use getsockopt(SO_ERROR) to get the error status - but be careful; some systems return the error code in the result parameter of getsockopt, but others (incorrectly) cause the getsockopt call *itself* to fail with the stored value as the error.
Submitted by: Administrator
Read Online Unix Socket Programming Job Interview Questions And Answers
Top Unix Socket Programming Questions
☺ | How do Sockets Work? |
☺ | What is the difference between SO_REUSEADDR and SO_REUSEPORT? |
☺ | How would I put my socket in non-blocking mode? |
☺ | What exactly does SO_LINGER do? |
☺ | How can I be sure that UDP messages are received in order? |
Top Operating System (OS) Categories
☺ | RTOS Interview Questions. |
☺ | Windows 7 Interview Questions. |
☺ | MAC Operating System Interview Questions. |
☺ | Disk Operating System (DOS) Interview Questions. |
☺ | Shell Scripting Interview Questions. |