Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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# Winforms服务器套接字应用程序_C#_Multithreading_Winforms_Sockets - Fatal编程技术网

C# Winforms服务器套接字应用程序

C# Winforms服务器套接字应用程序,c#,multithreading,winforms,sockets,C#,Multithreading,Winforms,Sockets,我正在尝试创建一个winforms应用程序,它侦听端口10000上的流量,基本上充当客户端应用程序和远程数据库的中间人。它应该有一个侦听和接受线程,在客户端连接时打开一个单独的客户端线程。然后,该客户机线程将处理与客户机程序的通信。侦听器应用程序有两个列表框,其中包含有关正在连接的用户和正在执行的操作的信息 目前,我正在尝试使用微软提供的示例程序,并根据我的需要对其进行修改,但如果有人对我的其他地方有任何建议,我很乐意听到 当我试图通过这一点时,有一件事我还没有弄清楚,那就是如何在不锁定我的计算

我正在尝试创建一个winforms应用程序,它侦听端口10000上的流量,基本上充当客户端应用程序和远程数据库的中间人。它应该有一个侦听和接受线程,在客户端连接时打开一个单独的客户端线程。然后,该客户机线程将处理与客户机程序的通信。侦听器应用程序有两个列表框,其中包含有关正在连接的用户和正在执行的操作的信息

目前,我正在尝试使用微软提供的示例程序,并根据我的需要对其进行修改,但如果有人对我的其他地方有任何建议,我很乐意听到

当我试图通过这一点时,有一件事我还没有弄清楚,那就是如何在不锁定我的计算机的情况下让这个监听器继续工作。这是我的表单代码(包括退出按钮和清除列表框的按钮):

这是我的侦听器代码,用begin.createListener调用:

int servPort = 10000;
        public void createListener() {
            // Create an instance of the TcpListener class.
            TcpListener tcpListener = null;
            IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
            string output = "";

            try {
                // Set the listener on the local IP address and specify the port.
                // 
                tcpListener = new TcpListener(ipAddress, servPort);
                tcpListener.Start();
                output = "Waiting for a connection...";
            }
            catch (Exception e) {
                output = "Error: " + e.ToString();
                MessageBox.Show(output);
            }
            while (true) {
                // Always use a Sleep call in a while(true) loop 
                // to avoid locking up your CPU.
                Thread.Sleep(10);
                // Create socket
                //Socket socket = tcpListener.AcceptSocket(); 
                TcpClient tcpClient = tcpListener.AcceptTcpClient();
                // Read the data stream from the client. 
                byte[] bytes = new byte[256];
                NetworkStream stream = tcpClient.GetStream();
                stream.Read(bytes, 0, bytes.Length);
                SocketHelper helper = new SocketHelper();
                helper.processMsg(tcpClient, stream, bytes);
            }
        }

现在,这只在tcpListener.AcceptSocket上停止。表单从未加载,而且显然列表框没有被填充。如何让这个侦听器在应用程序启动时自动运行,并且仍然加载表单并更新列表框?我希望此应用程序能够启动并随时准备接受连接,而无需让一个应用程序已经在那里等待。

您正在使用阻止方法,以便
Form1\u加载永不会结束,因为它等待传入的连接

一个简单的解决方法是启动一个处理连接的新线程:

private void Form1_Load(object sender, EventArgs e) {
    new Thread( 
        () =>
        {
           Server begin = new Server();
           begin.createListener();
        } 
    ).Start();
}

如果您确实希望快速入门,请使用开源框架,例如。原始套接字更难学习。你根本不需要睡眠,你必须使用Read的返回值。谢谢大家的提示!那个超级socket看起来可能有用——完全工作了!我认为,看到这一点如何应用到我的课程中,也将有助于我尝试做的其他方面。非常感谢。
private void Form1_Load(object sender, EventArgs e) {
    new Thread( 
        () =>
        {
           Server begin = new Server();
           begin.createListener();
        } 
    ).Start();
}