473,385 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Detecting Disconnect on TCP Socket


I am using the asynchronous send/receive methods of the Socket class.

When the remote end closes the socket, the callback for receive is
called and EndReceive returns 0.

Socket.Connected still returns true at this point, which I know is
expected behavior, so when I reach this state, I begin sending on this
socket every few seconds until an error is generated.

After the exception is raised, Socket.Connected returns false.
However, I cannot connect again to this socket. Calls to BeginConnect
fail with a message "A connect request was made on an already
connected socket."

How do I work around this, short of recreating the socket?

--Bruce
Nov 16 '05 #1
6 16599
"Bruce Vander Werf" <br*****@hotmail.com> wrote in message
news:5f********************************@4ax.com...

I am using the asynchronous send/receive methods of the Socket class.

When the remote end closes the socket, the callback for receive is
called and EndReceive returns 0.


Did you immediately call Close() against the socket at this point? If
not, this might be your problem. Don't bother trying to send stuff across
this socket after EndReceive() has returned zero. It's like beating a dead
horse (that's just an expression).

If I were you, I'd then call Dispose() on the socket, stop referencing
it, and let the GC get rid of it. Next time you need to initiate/accept a
new connection, just use a new Socket object. I know you said you were
against the idea of creating a new socket, but hey, why not?
Nov 16 '05 #2
Hi, Bruce

This is normal. After exception you usually can't use same socket. You have
to close/destroy old one and create new. I mean of course network-related
exception, not division by zero :-)

HTH
Alex

"Bruce Vander Werf" <br*****@hotmail.com> wrote in message
news:5f********************************@4ax.com...

I am using the asynchronous send/receive methods of the Socket class.

When the remote end closes the socket, the callback for receive is
called and EndReceive returns 0.

Socket.Connected still returns true at this point, which I know is
expected behavior, so when I reach this state, I begin sending on this
socket every few seconds until an error is generated.

After the exception is raised, Socket.Connected returns false.
However, I cannot connect again to this socket. Calls to BeginConnect
fail with a message "A connect request was made on an already
connected socket."

How do I work around this, short of recreating the socket?

--Bruce

Nov 16 '05 #3
David,

it's not always like this. I've seen situations when endreceive delivered 0
bytes, however beginreceive issued at that point was getting more data. It
really depends on how remote end behaves. If remote server is slow - you can
get 0 bytes while there are data available.
If you know what you should get from remote server you can check if you got
everything, for example using end-of-file byte or some tag line. I've seen
situations when I was receiving 5-10MB in chunks of 100-200K with delays of
several seconds between chunks. Until exception happens or
socket.Connected!=true socket could be considered as working and connected.

HTH
Alex

"David Sworder" <ds******@cts.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
"Bruce Vander Werf" <br*****@hotmail.com> wrote in message
news:5f********************************@4ax.com...

I am using the asynchronous send/receive methods of the Socket class.

When the remote end closes the socket, the callback for receive is
called and EndReceive returns 0.


Did you immediately call Close() against the socket at this point? If
not, this might be your problem. Don't bother trying to send stuff across
this socket after EndReceive() has returned zero. It's like beating a dead
horse (that's just an expression).

If I were you, I'd then call Dispose() on the socket, stop referencing
it, and let the GC get rid of it. Next time you need to initiate/accept a
new connection, just use a new Socket object. I know you said you were
against the idea of creating a new socket, but hey, why not?

Nov 16 '05 #4
Hi Alex,
it's not always like this. I've seen situations when endreceive delivered 0 bytes, however beginreceive issued at that point was getting more data. It
really depends on how remote end behaves. If remote server is slow - you can get 0 bytes while there are data available.
Are you sure about this? I just double-checked the documentation for
EndReceive() and it looks like a return value of 0 indicates that the remote
system has Shutdown the connection.

Consider an example where a client connects to a server and doesn't send
any data for five minutes. The server calls BeginReceive() immediately after
the connection is accepted. The first callback won't be even be made for 5
minutes. When the callback is finally made, there will be at least 1 byte of
data to receive. In other words, EndReceive() should return a value greater
than zero. If for some reason the client terminates early (say after two
minutes), the callback will be made and the server's EndReceive() call will
return zero. At that point, are you saying that it's possible for further
data to be in the pipeline? I wouldn't have thought that this is the case
since the socket is already shutdown. I would think that the socket should
immediately be closed and disposed at this point.
If you know what you should get from remote server you can check if you got everything, for example using end-of-file byte or some tag line. I've seen
situations when I was receiving 5-10MB in chunks of 100-200K with delays of several seconds between chunks.


...but your EndReceive() should never have returned zero between chunks,
right? If it did, wouldn't this imply that the client had dropped the
connection?

David
Nov 16 '05 #5
David,

MSDN says only:

-quote-
"If the remote host shuts down the Socket connection with the Shutdown
method, and all available data has been received, the EndReceive method will
complete immediately and return zero bytes"
-unquote-

It says nothing about 0 bytes meaning always shutdown with complete receipt.
It really depends on server on remote end. 0 could mean shutdown, lack of
data, not available data and other things, but not always shutdown.

HTH
Alex
"David Sworder" <ds******@cts.com> wrote in message
news:uH**************@TK2MSFTNGP09.phx.gbl...
Hi Alex,
it's not always like this. I've seen situations when endreceive delivered
0
bytes, however beginreceive issued at that point was getting more data.
It really depends on how remote end behaves. If remote server is slow - you

can
get 0 bytes while there are data available.


Are you sure about this? I just double-checked the documentation for
EndReceive() and it looks like a return value of 0 indicates that the

remote system has Shutdown the connection.

Consider an example where a client connects to a server and doesn't send any data for five minutes. The server calls BeginReceive() immediately after the connection is accepted. The first callback won't be even be made for 5
minutes. When the callback is finally made, there will be at least 1 byte of data to receive. In other words, EndReceive() should return a value greater than zero. If for some reason the client terminates early (say after two
minutes), the callback will be made and the server's EndReceive() call will return zero. At that point, are you saying that it's possible for further
data to be in the pipeline? I wouldn't have thought that this is the case
since the socket is already shutdown. I would think that the socket should
immediately be closed and disposed at this point.
If you know what you should get from remote server you can check if you got
everything, for example using end-of-file byte or some tag line. I've seen situations when I was receiving 5-10MB in chunks of 100-200K with delays

of
several seconds between chunks.


...but your EndReceive() should never have returned zero between

chunks, right? If it did, wouldn't this imply that the client had dropped the
connection?

David

Nov 16 '05 #6
aha! Thanks for the clarification.

"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:uV**************@TK2MSFTNGP10.phx.gbl...
David,

MSDN says only:

-quote-
"If the remote host shuts down the Socket connection with the Shutdown
method, and all available data has been received, the EndReceive method will complete immediately and return zero bytes"
-unquote-

It says nothing about 0 bytes meaning always shutdown with complete receipt. It really depends on server on remote end. 0 could mean shutdown, lack of
data, not available data and other things, but not always shutdown.

HTH
Alex
"David Sworder" <ds******@cts.com> wrote in message
news:uH**************@TK2MSFTNGP09.phx.gbl...
Hi Alex,
it's not always like this. I've seen situations when endreceive delivered
0
bytes, however beginreceive issued at that point was getting more
data.
It really depends on how remote end behaves. If remote server is slow -
you can
get 0 bytes while there are data available.
Are you sure about this? I just double-checked the documentation for
EndReceive() and it looks like a return value of 0 indicates that the

remote
system has Shutdown the connection.

Consider an example where a client connects to a server and doesn't

send
any data for five minutes. The server calls BeginReceive() immediately

after
the connection is accepted. The first callback won't be even be made for 5 minutes. When the callback is finally made, there will be at least 1 byte of
data to receive. In other words, EndReceive() should return a value

greater
than zero. If for some reason the client terminates early (say after two
minutes), the callback will be made and the server's EndReceive() call

will
return zero. At that point, are you saying that it's possible for

further data to be in the pipeline? I wouldn't have thought that this is the case since the socket is already shutdown. I would think that the socket should immediately be closed and disposed at this point.
If you know what you should get from remote server you can check if
you got
everything, for example using end-of-file byte or some tag line. I've seen situations when I was receiving 5-10MB in chunks of 100-200K with

delays of
several seconds between chunks.


...but your EndReceive() should never have returned zero between

chunks,
right? If it did, wouldn't this imply that the client had dropped the
connection?

David


Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: James Yang | last post by:
Hi, I am using Sockets to connect to a remote computer and send data using Socket.Send() and receive using Socket.Receive() (block mode) . for somereason tho, when the client disconnects the...
2
by: yaron | last post by:
i get an SocketException exception when using socket select method. how do i know what is the socket that cause the exception ? can i get this info from the exception instance ? Thanks.
1
by: rs | last post by:
how I the client tell the server that the socket is closed? or this there an even that informs the server that the clients socket is close? Oh, I am using vb.net 2003 Thanks
3
by: xincmu | last post by:
I built a very basic server-client in C#. When my server disconnects from the client, the socket at the server is already disconnected, but the socket the client remains open. I used the sample...
3
by: Cheryl | last post by:
Hi. I am having a problem on handling asynchronous sockets in C#. I implemented a pair of client and server sockets. The connection is ok when first connected. However, when I turned off the...
0
by: =?Utf-8?B?RGllZ28=?= | last post by:
The Connected property of the Socket class does not return false when the Socket no longer is connected to the host connection. Is there another way to verify that the Socket is disconnected? tnx
4
by: Adam Clauss | last post by:
A while back I posted regarding a problem we were having with one of our applications which was randomly crashing. Monitoring memory usage revealed a spike in nonpaged pool memory just prior to...
5
by: tichi | last post by:
I'm writing a program that in its most simplest form acts as a proxy between a local client and a remote server. But I cannot figure out how to detect when either the connection to the client is...
1
by: s3raph | last post by:
Hi, I'm having trouble detecting the client machine connection lost when the client physically pushes the power or restart button on their computer. Using the code from Plater in the following...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.