C#同时收听两个tcp端口

时间:2018-09-17 05:28:47

标签: c# .net multithreading sockets tcp

我打开了两个TCP端口,以监听服务器的客户端。下面是我打开端口的代码。我使用了一个线程:

            clientThreadTS = new Thread(ClientListenerTS);
            clientThreadTS.IsBackground = true;
            clientThreadTS.Name = "client listener TS";
            clientThreadTS.Start();

            clientThreadDis = new Thread(ClientListenerDis);
            clientThreadDis.IsBackground = true;
            clientThreadDis.Name = "client listener Dis";
            clientThreadDis.Start();

客户端监听器功能:

        private void ClientListenerTS()
    {
        try
        {
            if (bRestartListener)
            {
                Debug.WriteImportant("Restart QS listener");
                bRestartListener = false;
                htTCPClientTS.Clear();

                if (theClientListener != null)
                {
                    try
                    {
                        theClientListener.Close();
                    }
                    catch (Exception ex2)
                    {
                    }
                }
                theClientListener = null;

            }

            if (theClientListener == null)
            {
                try
                {
                    Debug.WriteImportant("Creating listener for client TS at any local IPs - port " + nConstClientPortTS);

                    IPEndPoint localEP = new IPEndPoint(IPAddress.Any, nConstClientPortTS);
                    theClientListener = new Socket(localEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                    theClientListener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                    theClientListener.Bind(localEP);
                    theClientListener.Listen(100);
                    theClientListener.BeginAccept(new AsyncCallback(AcceptConnectBackTS), theClientListener);

                }
                catch (Exception ex2)
                {
                    Debug.WriteLine(ex2.ToString());
                    System.Threading.Thread.Sleep(500);
                    theClientListener = null;
                }
            }

        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.ToString());
        }
    }

    private void ClientListenerDis()
    {
        try
        {
            if (bRestartListener)
            {
                Debug.WriteImportant("Restart QS listener");
                bRestartListener = false;
                htTCPClientDis.Clear();

                if (theClientListener != null)
                {
                    try
                    {
                        theClientListener.Close();
                    }
                    catch (Exception ex2)
                    {
                    }
                }
                theClientListener = null;

            }

            if (theClientListener == null)
            {
                try
                {
                    Debug.WriteImportant("Creating listener for client display at any local IPs - port " + nConstClientPortDis);


                    IPEndPoint localEP = new IPEndPoint(IPAddress.Any, nConstClientPortDis);
                    theClientListener = new Socket(localEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                    theClientListener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                    theClientListener.Bind(localEP);
                    theClientListener.Listen(100);
                    theClientListener.BeginAccept(new AsyncCallback(AcceptConnectBackDis), theClientListener);

                }
                catch (Exception ex2)
                {
                    Debug.WriteLine(ex2.ToString());
                    System.Threading.Thread.Sleep(500);
                    theClientListener = null;
                }
            }

        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.ToString());
        }
    }

问题总是总是第一个端口成功打开。第二个端口没有打开。

代码是否正确编写?打开第一个端口后,我们需要打开另一个端口吗?

知道为什么第一个端口只打开吗?

0 个答案:

没有答案