客户端连接服务器,说服务器拒绝连接,这是则么回事?
这个是服务器端代码有问题还是客户端代码有问题??以下是服务器端代码:
static void Main(string[] args)
{
IPAddress IP = IPAddress.Parse("127.0.0.1");
int port = 10001;
TcpListener server = new TcpListener(IP, port);
server.Start();
Console.WriteLine("等待客户机连接");
Socket socket = server.AcceptSocket();
Console.WriteLine("服务器接受连接");
while (true)
{
try
{
Console.WriteLine("联机成功!");
byte[] buff = new byte[1024];
int length = socket.Receive(buff);
string data = Encoding.ASCII.GetString(buff, 0, length);
}
catch (Exception e)
{
Console.WriteLine("客户机离开");
socket.Close();
}
}
Console.ReadLine();
}