将 UdpClient 与 IPv4 和 IPv6 一起使用?
不久前,我创建了一个类来处理我的 LAN 网络程序。我最近将一台笔记本电脑升级到 Windows 7,并意识到 Windows 7(或者至少是我设置它的方式)仅支持 IPv6,但我的台式机仍回到 Windows XP 时代,并且仅使用 IPv4。我创建的类使用 UdpClient 类,并且当前设置为仅适用于 IPv4。有没有办法修改我的代码以允许发送和接收 IPv6 和 IPv4 数据包? 很难废弃类代码,我的很多程序都依赖于这个类。我想让类保持接近其原始状态,因此我不需要修改旧程序,只需将旧类切换为更新的程序即可。
感谢您的任何和所有帮助, 最大
发送:
using System.Net.Sockets;UdpClient tub = new UdpClient ();
tub.Connect ( new IPEndPoint ( ToIP, ToPort ) );
UdpState s = new UdpState ();
s.client = tub;
s.endpoint = new IPEndPoint ( ToIP, ToPort );
tub.BeginSend ( data, data.Length, new AsyncCallback ( SendCallBack ),s);
private void SendCallBack ( IAsyncResult result )
{
UdpClient client = (UdpClient)( (UdpState)( result.AsyncState ) ).client;
IPEndPoint endpoint = (IPEndPoint)( (UdpState)( result.AsyncState ) ).endpoint;
client.EndSend ( result );
}
接收:
UdpClient tub = new UdpClient (ReceivePort);
UdpState s = new UdpState ();
s.client = tub;
s.endpoint = new IPEndPoint ( ReceiveIP, ReceivePort );
s.callback = cb;
tub.BeginReceive ( new AsyncCallback ( receivedPacket ), s );
public void receivedPacket (IAsyncResult result)
{
UdpClient client = (UdpClient)( (UdpState)( result.AsyncState ) ).client;
IPEndPoint endpoint = (IPEndPoint)( (UdpState)( result.AsyncState ) ).endpoint;
Byte[] receiveBytes = client.EndReceive ( result, ref endpoint );
Packet ThePacket = new Packet ( receiveBytes );
client.Close();
//Do what ever with the 'ThePacket' now
}
A little while ago I created a class to deal with my LAN networking programs. I recently upgraded one of my laptops to windows 7 and relized that windows 7 (or at least the way I have it set up) only supports IPv6, but my desktop is still back in the Windows xp days, and only uses IPv4. The class I created uses the UdpClient class, and is currently setup to only work with IPv4.. Is there a way to modify my code to allow sending and receiving of IPv6 and IPv4 packets??
It would be hard to scrap the classes code, a lot of my programs rely on this class. I would like to keep the class as close to its original state, so I don't need to modify my older programs, only switch out the old class for the updated one.
Thanks for any and all help,
Max
Send:
using System.Net.Sockets;UdpClient tub = new UdpClient ();
tub.Connect ( new IPEndPoint ( ToIP, ToPort ) );
UdpState s = new UdpState ();
s.client = tub;
s.endpoint = new IPEndPoint ( ToIP, ToPort );
tub.BeginSend ( data, data.Length, new AsyncCallback ( SendCallBack ),s);
private void SendCallBack ( IAsyncResult result )
{
UdpClient client = (UdpClient)( (UdpState)( result.AsyncState ) ).client;
IPEndPoint endpoint = (IPEndPoint)( (UdpState)( result.AsyncState ) ).endpoint;
client.EndSend ( result );
}
Receive:
UdpClient tub = new UdpClient (ReceivePort);
UdpState s = new UdpState ();
s.client = tub;
s.endpoint = new IPEndPoint ( ReceiveIP, ReceivePort );
s.callback = cb;
tub.BeginReceive ( new AsyncCallback ( receivedPacket ), s );
public void receivedPacket (IAsyncResult result)
{
UdpClient client = (UdpClient)( (UdpState)( result.AsyncState ) ).client;
IPEndPoint endpoint = (IPEndPoint)( (UdpState)( result.AsyncState ) ).endpoint;
Byte[] receiveBytes = client.EndReceive ( result, ref endpoint );
Packet ThePacket = new Packet ( receiveBytes );
client.Close();
//Do what ever with the 'ThePacket' now
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有端口的UdpClient构造函数不会立即绑定套接字。 UpdClient 还公开其套接字。这种组合允许在绑定之前修改套接字选项。
接收方:
运行它并检查它是否正在侦听
0.0.0.0
和[::]
发送方:
UdpClient constructors without port don't bind socket immediately. Also UpdClient exposes its socket. This combined allows to modify socket options before binding.
Receiver:
Run it and check that it is listening both
0.0.0.0
and[::]
Sender:
我认为这可能对您有帮助: C# server that在同一端口上支持 IPv6 和 IPv4
I think this might help you: C# server that supports IPv6 and IPv4 on the same port
通过提供双模式套接字,UdpClient 可以准备在 IPv4 和 IPv6 上接收:
发送更容易,我们可以使用指定的目标地址(IPv4 或 IPv6)创建 UdpClient。如果需要,可以在构造函数中提供 AddressFamily。
UdpClient can be prepared to receive on both IPv4 and IPv6 by providing a DualMode socket:
Sending is easier, we can create UdpClient with the specified target address (IPv4 or IPv6). AddressFamily can be provided in the constructor, if needed.