C# IPv6的Socket编程

C# IPv6的Socket编程

服务端监听代码:

IPAddress dirIP = IPAddress.Parse("fe00:e130:91d4:913b:126c");
IPEndPoint endPoint = new IPEndPoint(dirIP, 55577); 

Socket serverSocket = new Socket(AddressFamily.InterNetworkV6,
                                SocketType.Stream,
                                ProtocolType.Tcp);
try
{
    serverSocket.Bind(endPoint);
    serverSocket.Listen(int.MaxValue);
    StreamReader reader = null;
    StreamWriter writer = null;
    while(true)
    {
        Socket clientSocket = serverSocket.Accpet();
        NetworkStream ntStream = new NetworkStream(clientSocket);
        reader = new StreamReader(ntStream);
        string dataClient = reader.ReadLine();
        Console.WriteLine("Client::"+dataClient);
        if(dataClient == "Quit")
            break;
        writer = new StreamWriter(ntStream);
        string dataServer = "Hello  Clinet,I'm Server!";
        writer.WriteLine(dataServer);
        writer.Flush();
        Console.WriteLine("Server::" + dataServer);
    }
    reader.Close();
    writer.Close();
}
catch(Exception ex)
{
    Console.WriteLine(ex.Message + ex.ToString());
} 

客户端连接代码

IPAddress direction = ("fe00:e130:91d4:913b:126c");
IPEndPoint endPointSrv = new IPEndPoint(direction,55577);
Socket clientSocket = new Socket(AddressFamily.InterNetworkV6,
                                SocketType.Stream,
                                ProtocolType.Tcp);
try
{
    clientSocket.Connect(endPointSrv);
    StreamReader reader = null;
    StreamWriter writer = null;
    NetworkStream ntStream = new NetworkStream(clientSocket);
    writer = new StreamWriter(ntStream);
    string dataClient = "Hello,I'm Client! ";
    writer.WriteLine(dataClient);
    writer.Flush();
    Console.WriteLine("Client 已连接");
    reader = new StreamWriter(ntStream);
    string dataServer = reader.ReadLine();
    Console.WriteLine("Server::" + dataServer);
}
catch(Exception ex)
{
    Console.WriteLine(ex.ToString());
}
reader.Close();
writer.Close();