C# socket ipv6初体验

 

Server:

  serverSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                        serverSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                        if (!string.IsNullOrEmpty(SocketBindingIP))
                        {
                            serverSocket.Bind(new IPEndPoint(IPAddress.IPv6Any, int.Parse(Program.config.AppSettings.Settings["Middleware_PORT"].Value)));
                            lbWebApiBaseAddress.Text = ip;
                            LogMessage(DateTime.Now + "->Socket开启IPV6监听成功,监听IP:" + IPAddress.IPv6Any.ToString() + ":" + Program.config.AppSettings.Settings["Middleware_PORT"].Value);
                        }
                        else
                        { 
                            serverSocket.Bind(new IPEndPoint(IPAddress.Parse(ip), int.Parse(Program.config.AppSettings.Settings["Middleware_PORT"].Value)));
                            lbWebApiBaseAddress.Text = IPAddress.Parse(ip).ToString();
                            LogMessage(DateTime.Now + "->Socket开启IPV6监听成功,监听IP:" + IPAddress.Parse(ip) + ":" + Program.config.AppSettings.Settings["Middleware_PORT"].Value);
                        }

 

client:

m_clientSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                            m_clientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                            EndPoint ipeh_Server = new IPEndPoint(IPAddress.Parse(textBox1.Text), Convert.ToInt32(textBox2.Text));
                            if (!m_clientSocket.IsBound)
                            {
                                if (comboBox1.Text == "自动分配")
                                {
                                    m_clientSocket.Bind(new IPEndPoint(IPAddress.IPv6Any, 0));
                                }
                                else
                                {
                                    m_clientSocket.Bind(new IPEndPoint(IPAddress.IPv6Any, Convert.ToInt32(comboBox1.Text)));
                                }
                            }

 

posted @ 2019-07-29 16:35  数据酷软件  阅读(2909)  评论(0编辑  收藏  举报