SOCKET通讯错误问题
自己照书写了一个SOCKET通讯SERVER端示例,发现当客户端关闭连接的时候,服务器端会一直做循环,请问如何在服务器端判断客户端已经关闭?以下是服务器端类代码.
class SocketFunction
{
private static TcpListener Listener;
private static Socket ClientSocket;
private static IPEndPoint ListenPort;
private static Thread ClientService;
public static void StartListening()
{
ListenPort = new IPEndPoint(IPAddress.Any, 2048);
Listener = new TcpListener(ListenPort);
Listener.Start();
while (true)
{
Socket s = Listener.AcceptSocket();
ClientSocket = s;
ClientService = new Thread(new ThreadStart(ServiceClient));
ClientService.Start();
}
}
private static void ServiceClient()
{
Socket client = ClientSocket;
bool KeepAlive = true;
while (KeepAlive)
{
byte[] buffer = new byte[1024];
client.Receive(buffer, buffer.Length, 0);
string ClientCommand = System.Text.Encoding.UTF8.GetString(buffer);