1

I have server application that listens for clients. Let's client lost internet connection and lost connection with server.

Does server automatically check when a client was disconnected? If not how may I implement such thing?

Main.cs http://pastebin.com/fHYpErz7

ServerSocket.cs: http://pastebin.com/erw4tzdp

Client.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace jM2
{
    class Client
    {
        private int clientConnectionID;
        private Socket clientSocket;
        private string clientIP;
        private byte[] clientBuffer = new byte[1024];

        public Client(int connectionID, Socket connectionSocket)
        {
            clientConnectionID = connectionID;
            clientSocket = connectionSocket;
            clientIP = connectionSocket.RemoteEndPoint.ToString();
            clientSocket.BeginReceive(clientBuffer, 0, clientBuffer.Length, SocketFlags.None, new AsyncCallback(dataArrival), null);
        }
        public void Disconnect()
        {
            clientSocket.Close();
        }
        private void dataArrival(IAsyncResult iar)
        {
            int bytesReceived = clientSocket.EndReceive(iar);
            clientSocket.BeginReceive(clientBuffer, 0, clientBuffer.Length, SocketFlags.None, new AsyncCallback(dataArrival), null);
        }
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
jM2.me
  • 3,839
  • 12
  • 44
  • 58

3 Answers3

2

See my answer to this question:

TcpClient.Close doesn't close the connection

Basically no one knows if the connection is closed until you try to send data. If it fails, the connection is closed.

Community
  • 1
  • 1
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • Basically make a ping function on server side to ping clients and if there is no response just close that client socket. Correct? Also what would be the best way to store client sockets? – jM2.me Feb 18 '11 at 20:54
  • 5
    I would recommend that instead of making a "ping" function you just always assume that the client and server are connected. Whenever an error occurs when sending then you can assume that they are disconnected. This sounds stupid but when you think about it, just because they were connected 5 seconds ago when your ping function rang doesn't mean that they are connected now when you send data. – Chris Haas Feb 18 '11 at 20:58
  • I would store your client sockets in a collection (probably `List`) that is accessible to all members or your processing code. Possibly a class-level property but it depends on the rest of your code. – Chris Haas Feb 18 '11 at 21:00
1

Based on what Chris Haas said, I may be wrong, however I have previously written a TCP server and detected closed connections when I received 0 bytes. In other words, in your dataArrival method, if bytesReceived was 0, this would indicate the connection had closed. This seemed to work through fairly extensive testing.

Misko
  • 2,044
  • 12
  • 15
0

I recommend a type of "poll" or "heartbeat" message, as described here.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810