Пример #1
0
        static private void TcpListenerThreadProc(object data)
        {
            TcpListeners tcpListener   = (TcpListeners)data;
            IPEndPoint   localEndpoint = tcpListener.PopEndpoint();

            TcpListener listener = new TcpListener(localEndpoint);


            try
            {
                listener.Start();

                while (tcpListener.GoonListen)
                {
                    TcpClient cli = listener.AcceptTcpClient();

                    string msg = "(TCP Listener: Accepted) LocalEndPoint {" + cli.Client.LocalEndPoint.ToString() + "}, RemoteEndPoint {"
                                 + cli.Client.RemoteEndPoint.ToString() + "}";
                    Debug.WriteLine(msg);

                    tcpListener.AppendMessage(msg);

                    cli.Close();
                }
            }
            catch (SocketException ex)
            {
                switch (ex.SocketErrorCode)
                {
                case SocketError.AddressAlreadyInUse:
                    string msg = "(TCP Listener: AddressAlreadyInUse) LocalEndPoint {" + localEndpoint.ToString() + "}";
                    Debug.WriteLine(msg);
                    tcpListener.AppendMessage(msg);

                    tcpListener.AddExceptionalPort(localEndpoint.Port);
                    break;

                default:
                    Debug.Assert(false);
                    break;
                }
            }
            finally
            {
                listener.Stop();
            }

            tcpListener.DoDecrease();
        }
Пример #2
0
        private void CheckAsServer(string[] tcpPorts, string[] udpPorts)
        {
            if (startStopButton.Text.Equals("Start"))
            {
                //goto start
                ThreadSafePushNewMessage("Start as Server, Tcp ports " + String.Join(" ", tcpPorts) + " Udp ports " + String.Join(" ", udpPorts));

                startStopButton.Text = "Stop";

                EnableTcpUI(false);
                EnableUdpUI(false);
                checkBoxTcp.Enabled  = false;
                checkBoxUdp.Enabled  = false;
                modeComboBox.Enabled = false;

                if (checkBoxTcp.Checked)
                {
                    Debug.Assert(null == m_tcpListeners);
                    m_tcpListeners = new TcpListeners();
                    m_tcpListeners.Attach(this);
                    foreach (string item in tcpPorts)
                    {
                        m_tcpListeners.AddPort(Convert.ToInt32(item));
                    }
                    m_tcpListeners.StartListen();
                }

                if (checkBoxUdp.Checked)
                {
                    Debug.Assert(null == m_udpServers);
                    m_udpServers = new UdpServers();
                    m_udpServers.Attach(this);
                    foreach (string item in udpPorts)
                    {
                        m_udpServers.AddPort(Convert.ToInt32(item));
                    }
                    m_udpServers.StartListen();
                }

                m_udpServersExceptionPortList      = null;
                timer2CheckServerException.Enabled = true;
            }
            else
            {
                //goto stop
                ThreadSafePushNewMessage("Stop Server.");

                timer2CheckServerException.Enabled = false;
                m_udpServersExceptionPortList      = null;

                if (checkBoxTcp.Checked)
                {
                    m_tcpListeners.StopListen();

                    TcpClients tclis   = new TcpClients(IPAddress.Loopback);
                    TcpClients tclisv6 = new TcpClients(IPAddress.IPv6Loopback);
                    foreach (string item in tcpPorts)
                    {
                        tclis.AddPort(Convert.ToInt32(item));
                        tclisv6.AddPort(Convert.ToInt32(item));
                    }
                    tclis.StartConnect();
                    tclisv6.StartConnect();

                    List <int> exceptionalTcpPorts, exceptionalTcpPortsv6;
                    while (!tclis.IsConnectFinished(out exceptionalTcpPorts) || !tclisv6.IsConnectFinished(out exceptionalTcpPortsv6))
                    {
                        Thread.Sleep(500);
                    }

                    m_tcpListeners.WaitListensStop();
                    m_tcpListeners = null;
                }

                if (checkBoxUdp.Checked)
                {
                    m_udpServers.StopListen();

                    UdpClients uclis   = new UdpClients(IPAddress.Loopback);
                    UdpClients uclisv6 = new UdpClients(IPAddress.IPv6Loopback);
                    foreach (string item in udpPorts)
                    {
                        uclis.AddPort(Convert.ToInt32(item));
                        uclisv6.AddPort(Convert.ToInt32(item));
                    }
                    uclis.StartConnect();
                    uclisv6.StartConnect();

                    List <int> exceptionalUdpPorts, exceptionalUdpPortsv6;
                    while (!uclis.IsConnectFinished(out exceptionalUdpPorts) || !uclisv6.IsConnectFinished(out exceptionalUdpPortsv6))
                    {
                        Thread.Sleep(500);
                    }

                    m_udpServers.WaitListensStop();
                    m_udpServers = null;
                }


                EnableTcpUI(true);
                EnableUdpUI(true);
                checkBoxTcp.Enabled  = true;
                checkBoxUdp.Enabled  = true;
                modeComboBox.Enabled = true;
                startStopButton.Text = "Start";
            }
        }