Exemplo n.º 1
0
        }/*HiawathaSocketServer()*/

        /// <summary>
        /// Raises a customer event for when a Client has connected to the server.
        /// Used mostly for debugging.
        /// </summary>
        /// <remarks>
        /// NAME: OnRaiseClientConnectedEvent
        /// AUTHOR: Ryan Osgood
        /// DATE: 8/17/2019
        /// </remarks>
        /// <param name="e">The custom ClientConnectEventArgs associated with this event.</param>
        protected virtual void OnRaiseClientConnectedEvent(ClientConnectedEventArgs e)
        {
            EventHandler <ClientConnectedEventArgs> handler = m_raiseClientConnectedEvent;

            if (handler != null)
            {
                handler(this, e);
            }
        }/*OnRaiseClientConnectedEvent(ClientConnectedEventArgs e)*/
Exemplo n.º 2
0
        }/*OnRaiseClientConnectedEvent(ClientConnectedEventArgs e)*/

        /// <summary>
        /// An asynchronous method to handle and allow the connection of any Clients
        /// that ping the server.
        /// </summary>
        /// <remarks>
        /// NAME: StartListeningForIncomingConnection
        /// AUTHOR: Ryan Osgood
        /// DATE: 8/17/2019
        /// </remarks>
        /// <param name="a_ipaddr">The IP Address to listen on.</param>
        /// <param name="a_port"> The port number to listen on.</param>
        public async void StartListeningForIncomingConnection(IPAddress a_ipaddr = null, int a_port = 23000)
        {
            if (a_ipaddr == null)
            {
                a_ipaddr = IPAddress.Any;
            }

            if (a_port <= 0)
            {
                a_port = 23000;
            }

            m_ip   = a_ipaddr;
            m_port = a_port;

            Debug.WriteLine("IP Address: {0} - Port: {1}", m_ip, m_port);

            m_tcpListener = new TcpListener(m_ip, m_port);

            try
            {
                m_tcpListener.Start();

                KeepRunning = true;
                while (KeepRunning)
                {
                    var returnedByAccept = await m_tcpListener.AcceptTcpClientAsync();

                    m_clients.Add(returnedByAccept);

                    Debug.WriteLine("Client connected successfully, count: {0} - {1}", m_clients.Count, returnedByAccept.Client.RemoteEndPoint);

                    TakeCareOfTCPClient(returnedByAccept);

                    ClientConnectedEventArgs eaClientConnected = new ClientConnectedEventArgs(returnedByAccept.Client.RemoteEndPoint.ToString());
                    OnRaiseClientConnectedEvent(eaClientConnected);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        } /*StartListeningForIncomingConnection(IPAddress a_ipaddr = null, int a_port = 23000)*/