Exemple #1
0
        /// <summary>
        /// Accepts all incoming requests and creates a handler thread
        /// to deal with the communication.
        /// </summary>
        private void ListenForClients()
        {
            Log.Debug("SocketServer: Listener Thread");
            try
            {
                this.tcpListener.Start();
            }
            catch (System.Net.Sockets.SocketException se)
            {
                //Log.Error(se.StackTrace);
                Log.Error(se.StackTrace);
                // se.ErrorCode == 10048, this condition means that more than one process is attempting to bind to same port, disallowed.
                //                Log.Write(String.Format("SocketException: NativeError:{0} ErrorCode:{1}, Msg:{2}", se.NativeErrorCode, se.ErrorCode, se.Message));
                //                Log.Debug("Socket Exception Listening for clients", se);
                listenThread.Abort();
                return;
            }

            while (true)
            {
                //blocks until a client has connected to the server
                try
                {
                    TcpClient client = tcpListener.AcceptTcpClient();

                    IPEndPoint ipe     = (IPEndPoint)client.Client.LocalEndPoint;
                    IPAddress  addr    = IPAddress.Parse(ipe.Address.ToString());
                    String     strAddr = addr.ToString();

                    Log.Debug("Client " + strAddr + " has connected");

                    // Create a thread to handle communication with a connected client.
                    // The ClientConnHandler is an object that allows a way to pass data to a thread.  The TcpClient is passed to the
                    // object constructor and a thread is started with the objects worker method... allowing the thread access to the client.
                    var connHandler = new ClientConnHandler(client);
                    connHandler.OnPacketReceived += connHandler_OnPacketReceived;
                    if (!String.IsNullOrEmpty(connHandler.ID))
                    {
                        connHandler.OnClientConnStatusChanged += connHandler_OnClientConnStatusChanged;
                        connHandler.WorkerThread = new Thread(connHandler.WorkerThreadMethod)
                        {
                            IsBackground = true
                        };
                        connHandler.WorkerThread.Start();
                    }
                }
                catch (SocketException se)
                {
                    Log.Error(se.StackTrace);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Client connection status changed
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="status">status of the connection</param>
        private void connHandler_OnClientConnStatusChanged(object sender, ClientConnHandler.ConnectionStatus status)
        {
            ClientConnHandler clientConn = (ClientConnHandler)sender;

            if (status == ClientConnHandler.ConnectionStatus.Disconnected)
            {
                OnClientDisconnected(this, new WinsockClientConnectEventArgs(clientConn.ID, clientConn.ClientIPAddress));
                clientConn.OnClientConnStatusChanged -= connHandler_OnClientConnStatusChanged;
                clientList.Remove(clientConn.ID);
            }
            else if (status == ClientConnHandler.ConnectionStatus.Connected)
            {
                OnClientConnected(this, new WinsockClientConnectEventArgs(clientConn.ID, clientConn.ClientIPAddress));
                clientList.Add(clientConn.ID, clientConn);
            }
        }
Exemple #3
0
        /// <summary>
        /// Accepts all incoming requests and creates a handler thread 
        /// to deal with the communication.
        /// </summary>
        private void ListenForClients()
        {
            Log.Debug("SocketServer: Listener Thread");
            try
            {
                this.tcpListener.Start();
            }
            catch (System.Net.Sockets.SocketException se)
            {
                //Log.Error(se.StackTrace);
                Log.Error(se.StackTrace);
                // se.ErrorCode == 10048, this condition means that more than one process is attempting to bind to same port, disallowed.
                //                Log.Write(String.Format("SocketException: NativeError:{0} ErrorCode:{1}, Msg:{2}", se.NativeErrorCode, se.ErrorCode, se.Message));
                //                Log.Debug("Socket Exception Listening for clients", se);
                listenThread.Abort();
                return;
            }

            while (true)
            {
                //blocks until a client has connected to the server
                try
                {
                    TcpClient client = tcpListener.AcceptTcpClient();

                    IPEndPoint ipe = (IPEndPoint)client.Client.LocalEndPoint;
                    IPAddress addr = IPAddress.Parse(ipe.Address.ToString());
                    String strAddr = addr.ToString();

                    Log.Debug("Client " + strAddr + " has connected");

                    // Create a thread to handle communication with a connected client.
                    // The ClientConnHandler is an object that allows a way to pass data to a thread.  The TcpClient is passed to the
                    // object constructor and a thread is started with the objects worker method... allowing the thread access to the client.
                    var connHandler = new ClientConnHandler(client);
                    connHandler.OnPacketReceived += connHandler_OnPacketReceived;
                    if (!String.IsNullOrEmpty(connHandler.ID))
                    {
                        connHandler.OnClientConnStatusChanged += connHandler_OnClientConnStatusChanged;
                        connHandler.WorkerThread = new Thread(connHandler.WorkerThreadMethod) { IsBackground = true };
                        connHandler.WorkerThread.Start();
                    }
                }
                catch (SocketException se)
                {
                    Log.Error(se.StackTrace);
                }
            }
        }
Exemple #4
0
 /// <summary>
 /// Client connection status changed
 /// </summary>
 /// <param name="sender">event sender</param>
 /// <param name="status">status of the connection</param>
 private void connHandler_OnClientConnStatusChanged(object sender, ClientConnHandler.ConnectionStatus status)
 {
     ClientConnHandler clientConn = (ClientConnHandler)sender;
     if (status == ClientConnHandler.ConnectionStatus.Disconnected)
     {
         OnClientDisconnected(this, new WinsockClientConnectEventArgs(clientConn.ID, clientConn.ClientIPAddress));
         clientConn.OnClientConnStatusChanged -= connHandler_OnClientConnStatusChanged;
         clientList.Remove(clientConn.ID);
     }
     else if (status == ClientConnHandler.ConnectionStatus.Connected)
     {
         OnClientConnected(this, new WinsockClientConnectEventArgs(clientConn.ID, clientConn.ClientIPAddress));
         clientList.Add(clientConn.ID, clientConn);
     }
 }