我正在寻找一种处理断开连接的方法,因为每次关闭客户端时,服务器都会停止工作.我收到一条错误消息,在此行中"无法读取超出流的末尾":
string message = reader.ReadString();
Run Code Online (Sandbox Code Playgroud)
此外,我需要一种方法来从客户端列表中删除断开连接的客户端.这是我的代码:服务器
using System;
using System.Threading;
using System.Net.Sockets;
using System.IO;
using System.Net;
using System.Collections.Generic;
namespace Server
{
class Server
{
public static List<TcpClient> clients = new List<TcpClient>();
static void Main(string[] args)
{
IPAddress ip = IPAddress.Parse("127.0.0.1");
TcpListener ServerSocket = new TcpListener(ip, 14000);
ServerSocket.Start();
Console.WriteLine("Server started.");
while (true)
{
TcpClient clientSocket = ServerSocket.AcceptTcpClient();
clients.Add(clientSocket);
handleClient client = new handleClient();
client.startClient(clientSocket);
}
}
}
public class handleClient
{
TcpClient clientSocket;
public void startClient(TcpClient inClientSocket)
{
this.clientSocket = inClientSocket; …Run Code Online (Sandbox Code Playgroud)