Ejemplo n.º 1
0
        static private void UdpReceiveThreadProc(object data)
        {
            UdpServers udpServer     = (UdpServers)data;
            IPEndPoint localEndpoint = udpServer.PopEndpoint();

            //Receive
            //Creates an IPEndPoint to record the IP Address and port number of the sender.
            // The IPEndPoint will allow you to read datagrams sent from any source.
            IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

            try
            {
                UdpClient ucli = new UdpClient(localEndpoint);
                while (udpServer.GoonListen)
                {
                    // Blocks until a message returns on this socket from a remote host.
                    Byte[] receiveBytes = ucli.Receive(ref RemoteIpEndPoint);
                    string returnData   = Encoding.ASCII.GetString(receiveBytes);

                    string msg = "(UDP Server: Received) LocalEndPoint {" + localEndpoint.ToString() + "}, RemoteEndPoint {"
                                 + RemoteIpEndPoint.ToString() + "}, msg-->{" + returnData.ToString() + "}";
                    Debug.WriteLine(msg);
                    udpServer.AppendMessage(msg);

                    if (returnData.ToString().Contains(UdpServers.kAskQuestion))
                    {
                        Byte[] sendBytes = Encoding.ASCII.GetBytes(UdpServers.kAnswer);
                        ucli.Send(sendBytes, sendBytes.Length, RemoteIpEndPoint);

                        string sendMsg = "(UDP Server: AckSent) LocalEndPoint {" + localEndpoint.ToString() + "}, RemoteEndPoint {"
                                         + RemoteIpEndPoint.ToString() + "}, msg-->{" + UdpServers.kAnswer + "}";
                        Debug.WriteLine(sendMsg);
                        udpServer.AppendMessage(sendMsg);
                    }
                }
                ucli.Close();
            }
            catch (SocketException ex)
            {
                Debug.WriteLine("SocketErrorcode " + ex.ErrorCode + ", " + ex.ToString());
                switch (ex.SocketErrorCode)
                {
                case SocketError.AddressAlreadyInUse:
                {
                    string msg = "(UDP Server: AddressAlreadyInUse) LocalEndPoint {" + localEndpoint.ToString() + "}";
                    Debug.WriteLine(msg);
                    udpServer.AppendMessage(msg);

                    udpServer.AddExceptionalPort(localEndpoint.Port);
                }
                break;

                default:
                {
                    Debug.WriteLine(ex.ToString());
                    Debug.Assert(false);
                    //string msg = "(UDP Server: Error) LocalEndPoint {" + localEndpoint.ToString() + "}, Error Code "
                    //    + ex.ErrorCode.ToString() + " Error Msg: " + ex.Message;
                    //Debug.WriteLine(msg);
                    //udpServer.AppendMessage(msg);
                }
                break;
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                Debug.Assert(false);
            }


            udpServer.DoDecrease();
        }
Ejemplo n.º 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";
            }
        }