关于用UDP协议通讯的问题
我想用UDP协议来通讯,可是在使用过程中遇到了这样的问题:
如果我是先发送一个udp数据包再接收一下时,没有问题,好好的;
可是如果我先试着接收,它就会程序没有响应,不知道怎么回事?
在C#中,有没有象DELPHI中那样,有数据包来就通知你的消息函数(OnDataReceived)?
下面是我的接收数据包的函数,请各位大虾帮忙看看,万分感谢!
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 1888);
try{
Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
MessageBox.Show("This is the message you received " +
returnData.ToString());
MessageBox.Show("This message was sent from " +
RemoteIpEndPoint.Address.ToString() +
" on their port number " +
RemoteIpEndPoint.Port.ToString());
}
catch ( Exception e1 ){
MessageBox.Show(e1.ToString());
}