I have a situation where a client which receives data from a server at irregular intervals. The server only sends data and cannot receive anything. The client however needs to know when the connection with the server has been lost. In .NET C# is there an event that can be trapped for this purpose. I have searched for a while now and found anything.

disconnect event needed
Wee Bubba
"if you post an async read on the Socket, the callback is called when the socket connection is closed - sort of an event."
True in this case the EndRead will return a 0 byte count.
Thanks ;-)
Aamir Iqbal
I'm sure about the Asynch procedure.
The synch calls I have at present also use factory pattern to construnct a number of these clients if needed (it can happen that this 'client' is monitoring a number of 'servers'.
Any suggestions As you probably have already gathered I'm new to .NET and C#.
Chris Lively
"There is no such event in C# but you can build your own like this. While reading the Nework Stream whenever you get data with length '0' it means that other party is disconnected."
Thanks for this tip! My test shows this works. Don't you think this is contradictory to the following quote from the .Net online book regarding the method NetworkStream.Read():
"This method reads data into the buffer parameter and returns the number of bytes successfully read. If no data is available for reading, the Read method returns 0. "
rod_r
DavidR100
This client need to receive and save data coming in from 1 more devices. These devices send their data at irregular intervals often with long time periods been them. Communications is purely uni-directional.
In the event that the network connection to one of the devices is lost for any reason at all, e.g. physical line break, device power lose, I need to be able to detect this if possible and notify the user.
Cheers
qzrlsd
"This method reads data into the buffer parameter and returns the number of bytes successfully read. If no data is available for reading, the Read method returns 0."
Wrong, If no data is available Read() method simply waits untill some data is received or connection is colsed. Both Situations can be checked using int read = ns.Read() if read > 0 (Data Received) else disconnected!
This point has already been raised by someone that documentation says this. But some System.Net team member agreed that it was a mistake in that document and we'll setlle it up. So they may fix it in next review...
Best Regards,
Ventuspilot
You dont have to send 0 as a byte yourself, I meant to say when you close socket on either end you'll get a 0 legth byte means nothing or the length of the data received will be 0 length. I hope you understand now.
See My earlier posts!!! This solves this problem and try to think how to implement them.
Cheers ;-)
Paul Diston
Thanks for the suggestion. I had to send 1 byte as 0 bytes did nothing.
No I have a new problem. Because the receive method was synchronous I had to add a timeout so it would return in order to do the next send. Data from the server could have long time intervals between them so a timeout will happen. This then throws a socket timeout exception as expected. It also however throws a second type of socket exception "WouldBlock". The "WouldBlock" exception however does not only come once for each timeout, which would be ok, but continuously.
Is the there anyway of "cancelling" it when it has been caught the first time
mbutcher
There is no such event in C# but you can build your own like this. While reading the Nework Stream whenever you get data with length '0' it means that other party is disconnected.
When you are readin data from the network, always check the returned int value of Read method or equallent in asynchronous mode.
int readData = networkStream.Read(buffer, 0, buffer.length)
if(readData <= 0)
{
// Disconnected
}
Cheers ;-)
Vista2007new
" I had to send 1 byte as 0 bytes did nothing."
You dont have to send 0 as a byte yourself, I meant to say when you close socket on either end you'll get a 0 legth byte means nothing or the length of the data received will be 0 length. I hope you understand now.
"No I have a new problem. Because the receive method was synchronous I had to add a timeout so it would return in order to do the next send. Data from the server could have long time intervals between them so a timeout will happen. This then throws a socket timeout exception as expected. It also however throws a second type of socket exception "WouldBlock". The "WouldBlock" exception however does not only come once for each timeout, which would be ok, but continuously."
What actually you want to do Do you want to implement Time out feature in your code
For sending please explain!
Best Regards,