简体   繁体   English

将UdpClient与IPv4和IPv6一起使用?

[英]Use UdpClient with IPv4 and IPv6?

A little while ago I created a class to deal with my LAN networking programs. 不久前,我创建了一个类来处理我的LAN网络程序。 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. 我最近将一台笔记本电脑升级到Windows 7,并认为Windows 7(或至少是我的设置方式)仅支持IPv6,但我的台式机仍处于Windows xp时代,仅使用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?? 我创建的类使用UdpClient类,并且当前已设置为仅与IPv4一起使用。是否可以修改我的代码以允许发送和接收IPv6和IPv4数据包? 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 感谢您提供的所有帮助,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
    }

我认为这可能对您有所帮助: 在同一端口上支持IPv6和IPv4的C#服务器

UdpClient can be prepared to receive on both IPv4 and IPv6 by providing a DualMode socket: 通过提供DualMode套接字,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)

Sending is easier, we can create UdpClient with the specified target address (IPv4 or IPv6). 发送更容易,我们可以使用指定的目标地址(IPv4或IPv6)创建UdpClient。 AddressFamily can be provided in the constructor, if needed. 如果需要,可以在构造函数中提供AddressFamily。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM