通过TCP发送简单消息

通过TCP发送简单消息

本文介绍了通过TCP发送简单消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





以下代码在使用loopback ip的同一台计算机中完美运行,但如果我们在两台计算机之间尝试会导致问题。有什么建议吗?



谢谢。

NAS



客户端:

Hi ,

The following code work perfectly in in the same computer using the loopback ip ,but cause a problem if we try between two computers . Any suggestions ??

Thanks.
N.A.S

Client side:

private void button1_Click(object sender, EventArgs e)
       {
           client = new TcpClient();
           client.Connect("127.0.0.1", 8080);
       }
       TcpClient client;

       private void button2_Click(object sender, EventArgs e)
       {
           byte[] b = Encoding.Unicode.GetBytes(textBox1.Text);
           client.GetStream().Write(b, 0, Encoding.Unicode.GetByteCount(textBox1.Text));}









服务器端:





Server side:

private void rcv()

                    {
                        Int32 port = 8080;
                        IPAddress localAddr = IPAddress.Parse("127.0.0.1");
            TcpListener ls = new TcpListener(localAddr,port);
            ls.Start();
            TcpClient clientOne = ls.AcceptTcpClient();
           byte[] b = new byte[1024];
            while (true)
            {
                clientOne.GetStream().Read(b, 0, 1024);
                //MessageBox.Show(Encoding.Unicode.GetString(b));
                textBox1.Text = Encoding.Unicode.GetString(b);
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
            new Thread(new ThreadStart(rcv)).Start();}

推荐答案



这篇关于通过TCP发送简单消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-27 18:17