首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#实现udp通信代码!该如何处理

2012-04-01 
C#实现udp通信代码!领导要求时间紧,请帮忙。[解决办法]http://msdn.microsoft.com/zh-cn/library/system.ne

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);
}
基本的原理核心就这样
涉及到具体的可以继续讨论

热点排行