TCP通信无法接收数据
代码如下: /// <summary>
/// 声明获取事件的委托
/// </summary>
private delegate void SynReceive();
/// <summary>
/// 线程接收函数
/// </summary>
public void Receive()
{
try
{
string strRcv = "";
// 声明字节数组,一次接收数据的长度为1024字节
byte[] RcvBuffer = new byte[1024];
//循环读取,直到接收完所有数据
while (true)
{
int bytes = socket.Receive(RcvBuffer, RcvBuffer.Length, 0);
//读取完成后退出循环
if (bytes <= 0)
break;
//将读取的字节数转换为字符串
strRcv += Encoding.ASCII.GetString(RcvBuffer,bytes,0);
if (!txtRcv.InvokeRequired)
{
txtRcv.AppendText(strRcv);
}
else
{
txtRcv.Invoke(new SynReceive(Receive));
}
}
}