How can I tell when a socket is closed on the other end?
Submitted by: AdministratorIf the peer calls close() or exits, without
having messed with SO_LINGER, then our
calls to read() should return 0. It is
less clear what happens to write() calls
in this case; I would expect EPIPE, not
on the next call, but the one after.
If the peer reboots, or sets l_onoff = 1,
l_linger = 0 and then closes, then we
should get ECONNRESET (eventually) from
read(), or EPIPE from write().
When write() returns EPIPE, it also
raises the SIGPIPE signal - you never
see the EPIPE error unless you
handle or ignore the signal.
If the peer remains unreachable,
we should get some other error.
Don't think that write() can legitimately
return 0. read() should return 0 on
receipt of a FIN from the peer, and on
all following calls.
So yes, you must expect read() to return 0.
As an example, suppose you are receiving
a file down a TCP link; you
might handle the return from read() like this:
rc = read(sock,buf,sizeof(buf));
if (rc > 0)
{
write(file,buf,rc);
/* error checking on file omitted */
}
else if (rc == 0)
{
close(file);
close(sock);
/* file received successfully */
}
else /* rc < 0 */
{
/* close file and delete it, since data
is not complete report error, or whatever */
}
Submitted by: Administrator
having messed with SO_LINGER, then our
calls to read() should return 0. It is
less clear what happens to write() calls
in this case; I would expect EPIPE, not
on the next call, but the one after.
If the peer reboots, or sets l_onoff = 1,
l_linger = 0 and then closes, then we
should get ECONNRESET (eventually) from
read(), or EPIPE from write().
When write() returns EPIPE, it also
raises the SIGPIPE signal - you never
see the EPIPE error unless you
handle or ignore the signal.
If the peer remains unreachable,
we should get some other error.
Don't think that write() can legitimately
return 0. read() should return 0 on
receipt of a FIN from the peer, and on
all following calls.
So yes, you must expect read() to return 0.
As an example, suppose you are receiving
a file down a TCP link; you
might handle the return from read() like this:
rc = read(sock,buf,sizeof(buf));
if (rc > 0)
{
write(file,buf,rc);
/* error checking on file omitted */
}
else if (rc == 0)
{
close(file);
close(sock);
/* file received successfully */
}
else /* rc < 0 */
{
/* close file and delete it, since data
is not complete report error, or whatever */
}
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. |