我有監聽客戶端的服務器應用程序。 讓我們的客戶端失去了互聯網連接,並失去與服務器的連接。斷開連接時關閉客戶端套接字
服務器自動檢查客戶端何時斷開連接?如果不是我怎麼能實現這樣的事情?
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);
}
}
}
基本上在服務器端進行ping功能來ping客戶端,如果沒有響應就關閉客戶端套接字。正確?另外什麼是存儲客戶端套接字的最佳方式? – 2011-02-18 20:54:32