Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我的服务器/客户端项目不工作_C#_Sockets - Fatal编程技术网

C# 我的服务器/客户端项目不工作

C# 我的服务器/客户端项目不工作,c#,sockets,C#,Sockets,我有一个带socket的服务器项目和一个c#中带socket的客户端项目,当我启动服务器项目时,我的表单不工作,就像锁一样。 请帮帮我 如何解决我的表单正常工作而文本框显示消息的问题 private void button1_Click(object sender, EventArgs e) { try { port = 11000; ip = IPAddress.Any; I

我有一个带socket的服务器项目和一个c#中带socket的客户端项目,当我启动服务器项目时,我的表单不工作,就像锁一样。 请帮帮我

如何解决我的表单正常工作而文本框显示消息的问题

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            port = 11000;
            ip = IPAddress.Any;
            IPEndPoint ipLocal = new IPEndPoint(ip, port);
            socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socketServer.Bind(ipLocal);
            socketServer.Listen(10);
            buffersize = 1024;//از این سایزه همه چی از گووره این در می آد
            newClient = socketServer.Accept();
            while (true)
            {
                buffer = new byte[buffersize];
                recv = newClient.Receive(buffer, 0, buffer.Length, SocketFlags.None);
                if (recv != 0)
                {
                    message = Encoding.ASCII.GetString(buffer, 0, recv);
                    textBox1.Text += message;
                    //socketServer.Close();
                    //newClient.Close();
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
在客户端:

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            bufferSize = 1024;
            ip = IPAddress.Parse("192.168.1.10");
            port = 11000;
            socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint ipEnd = new IPEndPoint(ip, port);
            socketClient.Connect(ipEnd);
            while (true)
            {
                buffer = new byte[bufferSize];
                message = textBox1.Text;
                buffer = Encoding.ASCII.GetBytes(message);
                snd = socketClient.Send(buffer, 0, buffer.Length, SocketFlags.None);
                //socketClient.Close();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
在您的服务器中:

您的问题是由于Socket.Recieve()的阻塞性质造成的

它说:

如果没有可读取的数据,接收方法将阻塞,直到数据可用

你在同一个UI线程中运行它,所以你觉得你的UI被冻结了

解决方案:将套接字函数转移到另一个线程。要更新文本框,请使用


同样在服务器和客户机的实现中,您可以无限地运行while循环。因为它们是在UI线程中运行的,所以您会感到冻结。

您的应用程序会被锁定,因为您的click事件处理程序中有一个无限循环。您必须启动一个线程来执行读写操作,而不是阻塞主可视化线程