日期:2014-05-17 浏览次数:21200 次
contactWithServer = new ContactWithServer(this, user);
Thread clientcontactserverThread = new Thread(new ThreadStart(contactWithServer.ReceiveMsg));
clientcontactserverThread.Start();
public void ReceiveMsg()
{
while (true)
{
lock (this)
{
// 调用接收回调函数
IAsyncResult iar = udpReceiveClient.BeginReceive(new AsyncCallback(ReceiveCallback), udpReceiveState);
receiveDone.WaitOne();
Thread.Sleep(100);
}
}
}
private void ReceiveCallback(IAsyncResult iar)
{
UdpState udpReceiveState = iar.AsyncState as UdpState;
if (iar.IsCompleted)
{
data = udpReceiveState.udpClient.EndReceive(iar, ref udpReceiveState.ipEndPoint);
if (data.Length < 20) //只有命令
commandStr = Encoding.ASCII.GetString(data);