detect server disconnected?
I swear I had this before but now i cant get it back.
If I have a server client setup, how can I detect from client if the server no longer exists, example the server exists (could be because the server did not respond and we ended task) or perhaps there was a power failure and the PC switched off - in this sort of situation the client should know if there still is a connection to listen to..... how can I make it do this on the client side when listening to communication?
I swear I had it before but after modifying code, and changing it back, i cannot seem to reproduce this behaviour
thanks!
Re: detect server disconnected?
You are presumeably talking about Sockets here. You can either Ping the server (using the VS2005 Ping class) or by doing something like a GET request. If the server is gone then the socket will throw an exdception that you can catch.
Re: detect server disconnected?
ping wouldnt be a good idea in this case. I want to check the socket connection. I almost found a way, infact I did reproduce the code I had earlier and works great, except that if I get a long stream of data, the code wont work well as it would only read part of the incoming data.
the code which I would like, which does exactly what I want:
Code:
int theData = 0;
byte[] theIncomingDataBuffer = new byte[this.theNumberOfBytesToRead];
theData = this.theNetworkStream.Read(theIncomingDataBuffer, 0, theIncomingDataBuffer.Length);
//the line above is the code which will throw an IOException if the server has prematurely closed for whatever reason (power failure for example) - this is what I want, to keep reading
while (this.theNetworkStream.DataAvailable)
{
theData += this.theNetworkStream.Read(theIncomingDataBuffer, 0, theIncomingDataBuffer.Length);
}
if (theData > 0)
{
//handle data
}
problem with that is that if there is data available, it will overwrite what was in the buffer byte[] variable, so i would only get part of the data :( I want to be able to continuing writing to that byte[] variable from where it left off
Re: detect server disconnected?
ok this seems to work.... is this the correct way of reading from the offset?
Code:
int theData = this.theNetworkStream.Read(someBuffer, theData, someBuffer.Length);
if (this.theNetworkStream.DataAvailable)
{
theData += this.theNetworkStream.Read(someBuffer, theData, someBuffer.Length - theData);
}
Re: detect server disconnected?
You have to loop to read all the bytes of data from the stream. At the point at which the number of bytes is zero, then that is an indication that there are no more bytes to be read from the input stream. e.g.:
Code:
IPEndPoint remoteEP = new IPEndPoint(ipAddress, Server_Port_AsInt);
Socket sender = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp );
sender.Connect(remoteEP);
int theData = sender.Receive(someBuffer, theData.length, 0);
// Some processing with theData
while(theData > 0)
{
theData = sender.Receive(someBuffer, theData.length, 0);
// Some processing with theData
}
About the server shutting down, when you send the message from the client to the server, do it in a try / catch clause and catch the SocketException which will be thrown if the server is down. e.g.
Code:
try
{
// send the message
}
catch(SocketException) { Print --> Server is down }
Jennifer - Women Rules!
Re: detect server disconnected?
actually that wouldnt work but the solution i posted before you works well ;)