C#socket利用线程实现服务端向客户端连续发送数据,为什么客户端只接收到一次的数据?

服务端代码如图,客户端我用的tcp测试工具
2024-12-01 13:56:27
推荐回答(1个)
回答1:

void receives()
{
TcpListener tcplisten = new TcpListener(new IPEndPoint(IPAddress.Any, 5000));
tcplisten.Start();
byte[] by = new byte[1024];
int ii;
while (true)
{
Socket s = tcplisten.AcceptSocket();
s.Receive(by);
string t = System.Text.Encoding.Default.GetString(by, 0, ii);
this.richTextBox1.AppendText(ip.ToString() + " 发来信息:" + t + "\r\n");
s.Close();
}
tcplisten.Stop();
}
试试这样吧