简体   繁体   中英

UDP Client and server using IPV6

I Need to implement a simple C# UDP client server application using IPV6. But i don't have any idea about how to do it. I already did an UDP application which uses IP version 4. following code shows my source code.

UDP Server

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace UDPServer
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Net.Sockets.UdpClient server = new System.Net.Sockets.UdpClient(3478);
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            byte[] data = new byte[1024];
            data = server.Receive(ref sender);
            server.Close();
            string stringData = Encoding.ASCII.GetString(data, 0, data.Length);

            Console.WriteLine("Response from " + sender.Address + Environment.NewLine + "Message: " + stringData);
            Console.ReadLine();

        }
    }
}

UDP Client

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace UDPClient
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Net.Sockets.UdpClient sock = new System.Net.Sockets.UdpClient();
            IPEndPoint iep = new IPEndPoint(IPAddress.Parse("10..10.10.10"), 3478);
            byte[] data = Encoding.ASCII.GetBytes("Hello UDP Server!!!");
            sock.Send(data, data.Length, iep);
            sock.Close();
            Console.WriteLine("Message sent.");
            Console.ReadLine();
        }
    }
}
  1. Please give me an idea or example about "how to convert my application to IPV6" ?
  2. Is it possible to overcome the NAT traversal problems by using IPV 6 ?

technically there is no such thing as UDP server, they are always clients. The client just connects to the network card and then starts sending and receiving datagrams without any low level protocol (that's why datagrams may never arrive). In fact, the only difference between a UDP client and a "server" is that this last one has a public fixed IP (no matters which OS neither).

In order to migrate your UDP clients to IPV6 use:

System.Net.Sockets.UdpClient server = new System.Net.Sockets.UdpClient(3478, System.Net.Sockets.AddressFamily.InterNetworkV6);

I'm currently working in the way to get the same client to send/receive to/from IPv4 and IPv6 at same time, but it's not working. Using this you are forced to use IPv4 or IPv6 always (and that sucks).

Doing this doesn't works (at least for now):

server.Client.DualMode = true;
server.Client.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, 0);

The first option theoretically enables the dual mode: work using IPv4 and IPv6 simultaneously. The second one enables the use of both types of IP addresses internally.

You have to use also IPAddress.IPv6Any instead of IPAddress.Any .

The most interesting part here is that using IPv6 the remote "server" is able to send datagrams to the client (always if client sent a datagram to the "server" first), an impossibility using IPv4.

I'm currently researching and investigating this UDP nightmare, but the true is that I'm trending to migrate all to WebSockets because it is compatible with both IP modes at the same time, just as TCP, but a lot easier.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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