服务端代码:
[C#] 纯文本查看 复制代码 public static void xintiao()
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 30000);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Bind(ip);
System.Console.WriteLine("正在监听...");
s.Listen(1);
Socket sk = s.Accept();
System.Console.WriteLine("已经监听到客户端!");
byte[] by = new byte[1024];
sk.Send(Encoding.UTF8.GetBytes("已接受连接!"));
while (true)
{
System.Threading.Thread.Sleep(1000);
by = new byte[1024];
s.Receive(by);
string str = Encoding.UTF8.GetString(by);
System.Console.WriteLine(str);
}
}
客户端代码:
[C#] 纯文本查看 复制代码 public static void xintiao()
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(IPAddress.Parse("127.0.0.1"), 30000);
byte[] by = new byte[1024];
s.Receive(by);
string str = Encoding.UTF8.GetString(by);
str = str.Replace("\0", "");
System.Console.WriteLine(str);
if (str.IndexOf("已接受连接!") != -1)
{
while (true)
{
s.Send(Encoding.UTF8.GetBytes("心跳包"));
System.Threading.Thread.Sleep(700);
}
}
}
服务端运行状况截图:
客户端运行状况截图:
然后到这里就报错了。
以下是报错截图:
这个问题是怎么回事啊?该怎么解决啊?
|