読者です 読者をやめる 読者になる 読者になる

IPv6とIPv4を使って通信待ちうけをしてみる

とりあえず接続テストまで行ったので、メモ書き。
IPv4IPv6で同じポートで待ちうけ出来ることに少し驚き。
というわけで、ソースは以下

ソース

using System;
using System.Net;
using System.Net.Sockets;

class Program
{
    private static bool ListenFlag = true;
    static void Main(string[] args)
    {
        string ipaddr_v6 = "fe80::xxx:yyyy:zzzz:7b17%5"; // 送信先のIP(IPv6)
        string ipaddr_v4 = "192.168.x.10";               // 送信先のIP(IPv4)

        Console.WriteLine("0    :Client");
        Console.WriteLine("other:Server");
        int num = 0;
        try
        {
            num = int.Parse(Console.ReadLine());
        }
        catch (Exception)
        {
            num = int.MaxValue;
        }

        switch (num)
        {
            case 0:
                Console.WriteLine("Start Client");
                Client(ipaddr_v6, ipaddr_v4, 9999);
                Console.WriteLine("End Client");
                break;
            default:
                Console.WriteLine("Start Server");
                Server(9999);
                Console.WriteLine("End Server");
                break;
        }
    }

    static void Client(string ipAddr_v6, string ipAddr_v4, int port)
    {
        Console.WriteLine("IPv6 で送ります。");
        Socket socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
        socket.Connect(new IPEndPoint(IPAddress.Parse(ipAddr_v6), port));
        socket.Disconnect(false);
        socket.Dispose();

        Console.WriteLine("3秒ほどWaitします");
        System.Threading.Thread.Sleep(3000);

        Console.WriteLine("IPv4 で送ります。");
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        socket.Connect(new IPEndPoint(IPAddress.Parse(ipAddr_v4), port));
        socket.Disconnect(false);
        socket.Dispose();
    }

    static void Server(int port)
    {
        Console.WriteLine("Start IPv6 Listen");
        Listen(IPAddress.IPv6Any, port);
        Console.WriteLine("Start IPv4 Listen");
        Listen(IPAddress.Any, port);
        while (ListenFlag)
        {
            System.Threading.Thread.Sleep(10000);
            Console.WriteLine("Listen now ...");
        }
        Console.WriteLine("End Listen");
    }

    private static void Listen(IPAddress ipaddr, int port)
    {
        TcpListener listen = new TcpListener(ipaddr, port);
        listen.Start();
        listen.BeginAcceptSocket(DoAcceptSocketCallback, listen);
    }

    private static void DoAcceptSocketCallback(IAsyncResult ar)
    {
        TcpListener listen = ar.AsyncState as TcpListener;

        Socket socket = listen.EndAcceptSocket(ar);

        Console.WriteLine("Connect Success.");
        if (socket.AddressFamily == AddressFamily.InterNetworkV6)
        {
            Console.WriteLine("IPv6");
            Console.WriteLine("送信元IP");
            Console.WriteLine(socket.LocalEndPoint.ToString());
        }
        else if (socket.AddressFamily == AddressFamily.InterNetwork)
        {
            Console.WriteLine("IPv4");
            Console.WriteLine("送信元IP");
            Console.WriteLine(socket.LocalEndPoint.ToString());
        }

        listen.BeginAcceptSocket(DoAcceptSocketCallback, listen);
    }
}

実行結果

  • Client
0    :Client
other:Server
0
Start Client
IPv6 で送ります。
3秒ほどWaitします
IPv4 で送ります。
End Client
  • Server
0    :Client
other:Server
1
Start Server
Start IPv6 Listen
Start IPv4 Listen
Connect Success.
IPv6
送信元IP
[fe80::xxx:yyyy:zzzz:7b17%5]:9999
Connect Success.
IPv4
送信元IP
192.168.x.10:9999
Listen now ...