EOF notification on TCP sockets

2,684 views
Skip to first unread message

Bill Roome

unread,
Jun 22, 2009, 10:45:06 AM6/22/09
to ns-3-users
Folks,

When reading from a TCP socket, how can I get notified that the other
end has closed the socket? I understand the basic mechanism:
Socket::Recv() returns a packet with no bytes.

But as far as I can tell, Socket only calls the Recv callback when
data is actually available; it doesn't notify me when it has EOF
without data. So if I only call Recv() when triggered by the read
callback, I'll never know to call Recv() to get that empty packet.

I tried the following experiment: my sender wrote data, optionally
paused a few seconds, and then closed the socket. My receiver called
Recv() from the read callback, and the callback looped over Recv()
until the function returned 0 instead of a packet.

If the sender closed the socket immediately after sending the data,
the reader got a packet with the data and then an empty packet
indicating EOF. Great!

However, if the sender paused before closing, Socket called the Recv
callback when the data arrived, but it never called the callback when
the EOF arrived, so the reader hung.

I think the solution is to call the Recv callback when EOF is
available, even if there's no data. Otherwise it seems like the only
way to detect EOF is for the reader to poll the socket.

Or is there some other trick that I missed?

Tom Henderson

unread,
Jun 25, 2009, 1:42:06 AM6/25/09
to ns-3-...@googlegroups.com

I think you are probably right-- I'll be trying to close out some
related bugs in the next couple of days:
http://www.nsnam.org/bugzilla/show_bug.cgi?id=424

so I will look at what you raised when I do that.

Tom

Tom

unread,
Jun 29, 2009, 1:53:32 AM6/29/09
to ns-3-users


On Jun 22, 7:45 am, Bill Roome <w...@alcatel-lucent.com> wrote:
> Folks,
>
> When reading from a TCP socket, how can I get notified that the other
> end has closed the socket? I understand the basic mechanism:
> Socket::Recv() returns a packet with no bytes.
>
> But as far as I can tell, Socket only calls the Recv callback when
> data is actually available; it doesn't notify me when it has EOF
> without data. So if I only call Recv() when triggered by the read
> callback, I'll never know to call Recv() to get that empty packet.

Bill, do you mind reviewing the patch here and see whether it works
for you?

http://www.nsnam.org/bugzilla/show_bug.cgi?id=613

Tom
Reply all
Reply to author
Forward
0 new messages