Results 1 to 6 of 6

Thread: detect server disconnected?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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!

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.
    I don't live here any more.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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);
    }
    Last edited by Techno; May 8th, 2006 at 09:54 AM.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  5. #5
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    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!
    Last edited by drattansingh; May 8th, 2006 at 10:19 AM.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: detect server disconnected?

    actually that wouldnt work but the solution i posted before you works well

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width