将 UdpClient 与 IPv4 和 IPv6 一起使用?

发布于 2024-08-26 00:17:12 字数 1628 浏览 8 评论 0原文

不久前,我创建了一个类来处理我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我恋#小黄人 2024-09-02 00:17:12

没有端口的UdpClient构造函数不会立即绑定套接字。 UpdClient 还公开其套接字。这种组合允许在绑定之前修改套接字选项。

接收方:

using var receiver = new UdpClient(AddressFamily.InterNetworkV6);
receiver.Client.DualMode = true; // enables both IPv4 and IPv6
receiver.Client.Bind(new IPEndPoint(IPAddress.IPv6Any, 15621));

var incData = await receiver.ReceiveAsync();
Console.WriteLine(Encoding.UTF8.GetString(incData.Buffer));

运行它并检查它是否正在侦听 0.0.0.0[::]

PS C:\> netstat -an | findstr 15621
  UDP    0.0.0.0:15621          *:*
  UDP    [::]:15621             *:*

发送方:

using var sender = new UdpClient(AddressFamily.InterNetworkV6);
sender.Client.DualMode = true;
sender.Connect(new IPEndPoint(IPAddress.IPv6Loopback, 15621));
await sender.SendAsync(Encoding.UTF8.GetBytes("Hello server"));

UdpClient constructors without port don't bind socket immediately. Also UpdClient exposes its socket. This combined allows to modify socket options before binding.

Receiver:

using var receiver = new UdpClient(AddressFamily.InterNetworkV6);
receiver.Client.DualMode = true; // enables both IPv4 and IPv6
receiver.Client.Bind(new IPEndPoint(IPAddress.IPv6Any, 15621));

var incData = await receiver.ReceiveAsync();
Console.WriteLine(Encoding.UTF8.GetString(incData.Buffer));

Run it and check that it is listening both 0.0.0.0 and [::]

PS C:\> netstat -an | findstr 15621
  UDP    0.0.0.0:15621          *:*
  UDP    [::]:15621             *:*

Sender:

using var sender = new UdpClient(AddressFamily.InterNetworkV6);
sender.Client.DualMode = true;
sender.Connect(new IPEndPoint(IPAddress.IPv6Loopback, 15621));
await sender.SendAsync(Encoding.UTF8.GetBytes("Hello server"));
一百个冬季 2024-09-02 00:17:12

通过提供双模式套接字,UdpClient 可以准备在 IPv4 和 IPv6 上接收:

socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
socket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234));
var udpClient = new UdpClient();
udpClient.Client = socket;
... (use udpClient)

发送更容易,我们可以使用指定的目标地址(IPv4 或 IPv6)创建 UdpClient。如果需要,可以在构造函数中提供 AddressFamily。

UdpClient can be prepared to receive on both IPv4 and IPv6 by providing a DualMode socket:

socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
socket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234));
var udpClient = new UdpClient();
udpClient.Client = socket;
... (use udpClient)

Sending is easier, we can create UdpClient with the specified target address (IPv4 or IPv6). AddressFamily can be provided in the constructor, if needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文