c#异步通讯 tcp连接任意ip怎么修改

c#异步通讯 tcp连接任意ip怎么修改,本机可以工作,但是把Dns.GetHostName()改成指定的ipv6地址连接不上

private int maxPlayingTables;
        private CheckBox[,] checkBoxGameTables;
        private TcpClient client = null;
        private Service service;
        private StreamWriter sw;
        private StreamReader sr;
        private BinaryReader br;
        private BinaryWriter bw;
        bool exitWhile = false;

        private FormPlaying formPlaying;

        private bool isConnected = false; //是否连接成功

        BackgroundWorker connectWork = new BackgroundWorker();
        //是否正常退出接收线程
        private bool normalExit = false;
        //命令是否来自服务器
        private bool isReceiveCommand = false;
        //所坐的游戏桌座位号,-1表示未入座,0表示坐到黑方,1表示坐到白方
        private int side = -1;
        public FormRoom()
        {
            InitializeComponent();
            connectWork.DoWork += new DoWorkEventHandler(connectWork_DoWork);
            connectWork.RunWorkerCompleted +=
                new RunWorkerCompletedEventHandler(connectWork_RunWorkerCompleted);
        }
        private void FormRoom_Load(object sender, EventArgs e)
        {
            Random r = new Random((int)DateTime.Now.Ticks);
            textBoxName.Text = "Player" + r.Next(1, 100);
            maxPlayingTables = 0;
            textBoxLocal.ReadOnly = true;
            textBoxServer.ReadOnly = true;
        }

        /// <summary>异步方式与服务器进行连接</summary>
        void connectWork_DoWork(object sender, DoWorkEventArgs e)
        {
            client = new TcpClient();
            //此处为方便演示,实际使用时要将Dns.GetHostName()改为服务器域名
            //IAsyncResult result = client.BeginConnect(Dns.GetHostName(), 51888,null,null);
            //while (result.IsCompleted == false)
            //{
            //    Thread.Sleep(100);
            ////    
            //}
            try
            {
                // client.EndConnect(result);
                client = new TcpClient(Dns.GetHostName(), 51888);
                e.Result = "login";
            }
            catch (Exception ex)
            {
                e.Result = ex.Message;
                return;
            }
        }
        /// <summary>异步方式与服务器完成连接操作后的处理</summary>
        void connectWork_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Result.ToString() == "login")
            {

                isConnected = true;

                groupBox1.Visible = true;
                textBoxLocal.Text = client.Client.LocalEndPoint.ToString();
                textBoxServer.Text = client.Client.RemoteEndPoint.ToString();
                buttonConnect.Enabled = false;
                //获取网络流
                NetworkStream netStream = client.GetStream();
                //sr = new StreamReader(netStream, System.Text.Encoding.UTF8);
                //sw = new StreamWriter(netStream, System.Text.Encoding.UTF8);
                br = new BinaryReader(netStream);
                bw = new BinaryWriter(netStream);
                service = new Service(listBox1, bw);
                service.SendToServer("Login," + textBoxName.Text.Trim());
                Thread threadReceive = new Thread(new ThreadStart(ReceiveData));
                threadReceive.IsBackground = true;
                threadReceive.Start();
            }
            else
            {
                //AddStatus("连接失败:" + e.Result);
                MessageBox.Show("与服务器连接失败", "",
                   MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
                //buttonConnect.Enabled = true;
            }
        }

        /// <summary>【登录】按钮的Click事件</summary>
        private void buttonConnect_Click(object sender, EventArgs e)
        {
            buttonConnect.Enabled = false;
            connectWork.RunWorkerAsync();
            //try
            //{
            //    //仅作本机测试,实际使用时要将Dns.GetHostName()改为服务器域名
            //    client = new TcpClient(Dns.GetHostName(), 51888);

            //}
            //catch
            //{
            //    MessageBox.Show("与服务器连接失败", "",
            //        MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            //groupBox1.Visible = true;
            //textBoxLocal.Text = client.Client.LocalEndPoint.ToString();
            //textBoxServer.Text = client.Client.RemoteEndPoint.ToString();
            //buttonConnect.Enabled = false;
            ////获取网络流
            //NetworkStream netStream = client.GetStream();
            //sr = new StreamReader(netStream, System.Text.Encoding.UTF8);
            //sw = new StreamWriter(netStream, System.Text.Encoding.UTF8);
            //service = new Service(listBox1, sw);
            ////登录服务器,获取服务器各桌信息
            ////格式:Login,昵称
            //service.AsyncSendToServer("Login," + textBoxName.Text.Trim());
            //Thread threadReceive = new Thread(new ThreadStart(ReceiveData));
            //threadReceive.Start();
        }

后面处理接受数据的就不贴了,

1个回答

TcpClient client = new TcpClient(AddressFamily.InterNetworkV6);
client.Connect("fe80::14d8:a209:89e6:c162%14", 9900);

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问