Write on a closed TCPConn

827 views
Skip to first unread message

Monnand

unread,
Dec 7, 2012, 1:41:36 AM12/7/12
to golan...@googlegroups.com
Hi all,

I am writing a client which connects the server through TCP and sends
some data periodically. The server may drop the connection and the
client should re-establish the connection before next Write().

This is a very typical program and I thought I could detect the closed
connection by checking the returned error from Write(). I wrote two
simple programs to see what error will be returned from Write() if the
connection is closed by the remote end. (It should be EPIPE according to
write(2))

However, the Write() operation on the closed connection returns no
error. I would like to know how can I detect the closed connection in
this case.

Here are the test programs I wrote:

- client program:
http://play.golang.org/p/cSs-AHkM2U

The logic is very simple: connect to server, wait for a second, then
write some data and print the return values from Write().

- server program:
http://play.golang.org/p/oTRtTCrKAt

Still very simple: listen on a port, if there is a connection, accept it
and close it immediately.

Regards,
-Monnand

mjarco

unread,
Dec 7, 2012, 5:29:11 AM12/7/12
to golan...@googlegroups.com
Hi,
This topic returns to the group from time to time. Short answer is
to Read() from connection and if Read() returns EOF it means 
connection is closed. For more detailed explanaitions please 
browse group archives

Monnand

unread,
Dec 10, 2012, 12:18:38 AM12/10/12
to golan...@googlegroups.com
On 12/07/2012 05:29 AM, mjarco wrote:
> Hi,
> This topic returns to the group from time to time. Short answer is
> to Read() from connection and if Read() returns EOF it means
> connection is closed. For more detailed explanaitions please
> browse group archives
>

Hi Mjarco,

Thanks for your reply!

I did searched around the Internet and I did found some thread in this
mailing list related to this problem. However, none of them proposed a
solution suitable for me. Most of them, suggest to use Read() first to
test if the connectivity of a socket.

In my code, there are two goroutines, one for read from the socket and
one for write to the socket. If there are some data not been processed
by the read goroutine, the write goroutine may read the data from the
connection when test the connectivity using Read(). It may lead to data
corruption for the read goroutine.

I would like to know if this (write to closed net.TCPConn without
returning error) is a feature of net package or it should be considered
as a bug.

Regards,
-Monnand

Kyle Lemons

unread,
Dec 10, 2012, 5:00:19 PM12/10/12
to Monnand, golang-nuts
Your program needs to be able to handle this case.  Because you are communicating over a network, there can always be circumstances in which the remote side terminates the connection before it has received some data that you sent.  Under TCP, your operating system should be able to determine that this happened, but whether this is reported to you at the time of the Write or at the time of the Close (or not at all) will vary from situation to situation.




--



Kyle Lemons

unread,
Dec 10, 2012, 5:03:28 PM12/10/12
to Monnand, golang-nuts
Whoops, that carriage return apparently sent the email.

On Mon, Dec 10, 2012 at 5:00 PM, Kyle Lemons <kev...@google.com> wrote:
Your program needs to be able to handle this case.  Because you are communicating over a network, there can always be circumstances in which the remote side terminates the connection before it has received some data that you sent.  Under TCP, your operating system should be able to determine that this happened, but whether this is reported to you at the time of the Write or at the time of the Close (or not at all) will vary from situation to situation.

You already hava a goroutine that is handling reads, so use that to signal when the connection has been closed, at which point you can close the connection and re-establish it along with the two goroutines handling reads and writes.

Monnand

unread,
Dec 15, 2012, 2:39:55 AM12/15/12
to golan...@googlegroups.com, Monnand
Hi Kyle,

Thanks for your reply. I have changed my code according to your advice.

Thank you very much!

Regards,
-Monnand
Reply all
Reply to author
Forward
0 new messages