日期:2014-05-18  浏览次数:20948 次

C#实现udp通信代码!
领导要求时间紧,请帮忙。

------解决方案--------------------
http://msdn.microsoft.com/zh-cn/library/system.net.sockets.socket.aspx
http://msdn.microsoft.com/zh-cn/library/system.net.sockets.udpclient.aspx
------解决方案--------------------
form实现异步UDP网络聊天通讯
winform实现UDP局域网聊天
------解决方案--------------------
发送消息:
UdpClient udpclient = new UdpClient();
IPAddress remoteIPAddress = Dns.GetHostAddress(Dns.GetHostName())[0]
IPEndPoint romoteIpEndPoint = new IPEndPoint(remoteIpAddress,端口号);
byte[] sendBytes = Encoding.Unicode.GetBytes("message");
udpClient.Send(sendBytes,sendBytes.length,remoteIPAddress);
接受数据:
UdpClient udpClient = new UdpClient(Dns.GetHostAddress(Dns.GetHostName())[0]);
IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any,0);
try
{
Byte[] receiveBytes = udpClient.Receive(ref remoteIpEndPoint);
string receiveDate = Encoding.Unicode.GetString(receiveByte);
Console.WriteLine("receive message:"+receiveData);
}
catch(Exception e)
{
MessageBox.Show(e.ToString);
}
基本的原理核心就这样
涉及到具体的可以继续讨论