【问题标题】:Winforms server socket applicationWinforms 服务器套接字应用程序
【发布时间】:2014-07-04 06:04:58
【问题描述】:

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

目前,我正在尝试使用 Microsoft 提供的示例程序 here 并根据我的需要对其进行修改,但如果有人对我可能在哪里寻找其他建议有任何建议,我很乐意听到。

当我尝试解决这个问题时,我还无法弄清楚的一件事是如何在不锁定我的计算机的情况下让这个听众继续工作。这是我的表单代码(包括一个退出按钮和一个清除我的列表框的按钮):

public partial class Form1 : Form {

    public Form1() {
        InitializeComponent();
    }

    private void btnExit_Click(object sender, EventArgs e) {
        this.Close();
    }

    private void btnClearList_Click(object sender, EventArgs e) {
        this.lbActionLog.Items.Clear();
        this.lbUserLog.Items.Clear();
        count = 0;
        this.txtCount.Text = count.ToString();
    }

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

这是我用 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 上停止。表单永远不会加载,显然列表框没有被填充。我怎样才能让这个监听器在应用程序启动时自动运行,并且仍然加载表单并更新列表框?我希望此应用程序启动并随时准备好接受连接,而无需让一个已经坐在那里等待。

【问题讨论】:

  • 如果您确实想要快速入门,请使用开源框架,例如 SuperSocket。原始套接字更难学习。
  • 你根本不需要sleep,必须使用Read的返回值。
  • 感谢您的提示!那个超级套接字看起来可能有用

标签: c# multithreading winforms sockets


【解决方案1】:

您正在使用阻塞方法,因此 Form1_Load 永远不会结束,因为它等待传入的连接。

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

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

【讨论】:

  • 完全成功!我认为看看这如何适用于我的程序也将有助于我正在尝试做的其他方面。谢谢!
猜你喜欢
  • 2011-12-20
  • 1970-01-01
  • 2015-06-24
  • 2015-03-15
  • 2015-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多