コード例 #1
0
        static private void TcpConnectThreadProc(object data)
        {
            TcpClients tcpClients   = (TcpClients)data;
            IPEndPoint peerEndpoint = tcpClients.PopEndpoint();

            if (peerEndpoint.AddressFamily != tcpClients.TargetIP.AddressFamily)
            {
                //ip stack not match
                tcpClients.DoDecrease();
                return;
            }

            TcpClient tcli = new TcpClient(tcpClients.TargetIP.AddressFamily);

            tcli.SendTimeout = 1000;

            try
            {
                tcli.Connect(new IPEndPoint(tcpClients.TargetIP, peerEndpoint.Port));
            }
            catch (SocketException ex)
            {
                switch (ex.ErrorCode)
                {
                case 10061:
                    tcpClients.AddExceptionalPort(peerEndpoint.Port);
                    break;

                default:
                    tcpClients.AddExceptionalPort(peerEndpoint.Port);
                    break;
                }
            }
            finally
            {
                tcli.Close();
            }

            tcpClients.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";
            }
        }
コード例 #3
0
        private void CheckAsClient(IPAddress ip, string[] tcpPorts, string[] udpPorts)
        {
            EnableTcpUI(false);
            EnableUdpUI(false);
            checkBoxTcp.Enabled  = false;
            checkBoxUdp.Enabled  = false;
            ipTextBox.Enabled    = false;
            modeComboBox.Enabled = false;

            string showText = null;

            if (checkBoxTcp.Checked)
            {
                ThreadSafePushNewMessage("Start as Tcp Client, Peer IP " + ip.ToString() + " Tcp ports " + String.Join(" ", tcpPorts));

                TcpClients tclis = new TcpClients(ip);
                foreach (string item in tcpPorts)
                {
                    tclis.AddPort(Convert.ToInt32(item));
                }
                tclis.StartConnect();

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

                showText += "TCP Ports ";
                if (null != exceptionalTcpPorts && exceptionalTcpPorts.Count > 0)
                {
                    foreach (int item in exceptionalTcpPorts)
                    {
                        showText += item.ToString();
                        showText += " ";
                    }
                    showText += " invalid!";
                    //MessageBox.Show(showText);
                }
                else
                {
                    //MessageBox.Show(" valid.");
                    showText += "ALL valid.";
                }
            }

            if (checkBoxUdp.Checked)
            {
                ThreadSafePushNewMessage("Start as Udp Client, Peer IP " + ip.ToString() + " Udp ports " + String.Join(" ", udpPorts));

                showText += "\nUDP Ports ";

                UdpClients tclis = new UdpClients(ip);
                foreach (string item in udpPorts)
                {
                    tclis.AddPort(Convert.ToInt32(item));
                }
                tclis.StartConnect();

                List <int> exceptionalPorts;
                while (!tclis.IsConnectFinished(out exceptionalPorts))
                {
                    Thread.Sleep(500);
                }

                if (null != exceptionalPorts && exceptionalPorts.Count > 0)
                {
                    foreach (int item in exceptionalPorts)
                    {
                        showText += item.ToString();
                        showText += " ";
                    }
                    showText += " invalid!";
                    //MessageBox.Show(showText);
                }
                else
                {
                    //MessageBox.Show("Ports valid.");
                    showText += "ALL valid.";
                }
            }

            MessageBox.Show(showText);

            EnableTcpUI(true);
            EnableUdpUI(true);
            checkBoxTcp.Enabled  = true;
            checkBoxUdp.Enabled  = true;
            ipTextBox.Enabled    = true;
            modeComboBox.Enabled = true;
        }