在这里EndReceive前后端都需要做判断,不然前端会报错
static void ReceiveCallback(IAsyncResult ar)
{
try
{
Socket pocket = (Socket)ar.AsyncState;
int num = pocket.EndReceive(ar);
if (num <= 0)
{
Console.WriteLine("接收到的数据数量:" + num );
}
else {
string msg = Encoding.UTF8.GetString(msgBuff);
Console.WriteLine("接收到的数据:" + msg);
Receive(pocket, ReceiveCallback);
}
}
catch (Exception ex)
{
Console.WriteLine("接收数据失败! 异常 :" + ex.ToString());
}
}