C#用tcpclient怎么实现服务器与客户端的互相发送接收信息

2024-12-16 12:28:09
推荐回答(2个)
回答1:

服务器端做个触发 当收到信息后,自动回复客户端

回答2:

while ((length = s.Receive(buffer)) > 0)
{
string receiveStr = System.Text.Encoding.UTF8.GetString(buffer, 0, length);
if (receiveStr.StartsWith("head:") && receiveStr.EndsWith(":end"))
{
receiveStr = receiveStr.Remove(receiveStr.IndexOf(":end"));
string[] arrs = receiveStr.Replace("head:", "").Split('@');
byte[] sendMsg = System.Text.Encoding.UTF8.GetBytes("ok");
s.Send(sendMsg);
}
}