Ejemplo n.º 1
0
 public void RemoveSocket(SocketClient socketClient)
 {
     Monitor.Enter(socketClientList);
     try
     {
         foreach (SocketClient socket in socketClientList)
         {
             if (socket == socketClient)
             {
                 socketClientList.Remove(socketClient);
                 break;
             }
         }
     }
     catch
     {
     }
     Monitor.Exit(socketClientList);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Function to process and accept socket connection requests
        /// </summary>
        private void AcceptThread()
        {
            Socket clientSocket = null;

            for (; ;)
            {
                try
                {
                    // If a client connects, accept the connection
                    clientSocket = this.tcpListener.AcceptSocket();
                    if (clientSocket.Connected)
                    {
                        if (socketClientList.Count > maxAccept)
                        {
                            clientSocket.Close();
                            continue;
                        }

                        string Addr  = clientSocket.RemoteEndPoint.ToString();
                        int    index = Addr.IndexOf(':');
                        Addr = Addr.Substring(0, index);

                        // Create a SocketClient object
                        SocketClient socket = AcceptedSocketClient(this,
                                                                   clientSocket,                                         // The socket object for the connection
                                                                   Addr,                                                 // The IpAddress of the client
                                                                   this.Port,                                            // The port the client connected to
                                                                   this.SizeOfRawBuffer,                                 // The size of the byte array for storing messages
                                                                   this.UserArg,                                         // Application developer state
                                                                   new Sockets.MessageEventHandler(this.messageHandler), // Application developer Message Handler
                                                                   new Sockets.CloseEventHandler(this.closeHandler),     // Application developer Close Handler
                                                                   new Sockets.ErrorEventHandler(this.errorHandler));    // Application developer Error Handler

                        socketClientList.Add(socket);
                        // Call the Accept Handler
                        this.acceptHandler(socket);
                    }
                }
                catch (System.Net.Sockets.SocketException e)
                {
                    // Did we stop the TCPListener
                    if (e.ErrorCode != 10004)
                    {
                        // Call the error handler
                        this.errorHandler(null, e);
                        // Close the socket down if it exists
                        if (clientSocket != null)
                        {
                            if (clientSocket.Connected)
                            {
                                clientSocket.Close();
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    // Call the error handler
                    this.errorHandler(null, e);
                    // Close the socket down if it exists
                    if (clientSocket != null)
                    {
                        if (clientSocket.Connected)
                        {
                            clientSocket.Close();
                        }
                    }
                }
            }
        }