/// <summary> /// Accepts clients in the separate thread. /// </summary> private void AcceptConnections() { try { _listener.Start(); while (true) { Socket socket = _listener.AcceptSocket(); //Waiting var client = new SocketClient(socket); client.Context = this.Context; client.Connected += (s, e) => { OnConnected(e); }; client.Disconnected += SocketClientDisconnected; client.ReceivedData += (s, e) => { OnReceivedData(e); }; client.SentData += (s, e) => { OnSentData(e); }; client.Error += (s, e) => { OnError(e); }; client.SocketError += (s, e) => { OnSocketError(e); }; SocketClients.Add(client); client.BeginReceive(); } } catch (SocketException e) { if (e.ErrorCode != 10004) { OnSocketError(new SocketErrorEventArgs(LocalEndPoint, e)); } } catch (Exception e) { OnError(new ErrorEventArgs(LocalEndPoint, e)); } }
/// <summary> /// Ends the pending of asynchronous accept of new clients. /// </summary> /// <param name="ar"></param> private void AcceptCallBack(IAsyncResult ar) { try { var listener = (TcpListener)ar.AsyncState; if (ServerStatus != SocketServerStatus.Start || !listener.Server.IsBound) { return; } Socket socket = listener.EndAcceptSocket(ar); var client = new SocketClient(socket); client.Context = this.Context; client.Connected += (s, e) => { OnConnected(e); }; client.Disconnected += SocketClientDisconnected; client.ReceivedData += (s, e) => { OnReceivedData(e); }; client.SentData += (s, e) => { OnSentData(e); }; client.Error += (s, e) => { OnError(e); }; client.SocketError += (s, e) => { OnSocketError(e); }; SocketClients.Add(client); client.BeginReceive(); ContinueAcceptConnections(); } catch (SocketException e) { if (e.ErrorCode != 10004) { OnSocketError(new SocketErrorEventArgs(LocalEndPoint, e)); } } catch (Exception e) { OnError(new ErrorEventArgs(LocalEndPoint, e)); } }
/// <summary> /// Ends the pending of asynchronous accept of new clients. /// </summary> /// <param name="ar"></param> private void AcceptCallBack(IAsyncResult ar) { try { var listener = (TcpListener)ar.AsyncState; if (ServerStatus != SocketServerStatus.Start || !listener.Server.IsBound) { return; } Socket socket = listener.EndAcceptSocket(ar); var client = new SocketClientSsl(socket); client.Context = this.Context; client.Connected += (s, e) => { OnConnected(e); }; client.Disconnected += SocketClientDisconnected; client.ReceivedData += (s, e) => { OnReceivedData(e); }; client.SentData += (s, e) => { OnSentData(e); }; client.Error += (s, e) => { OnError(e); }; client.SocketError += (s, e) => { OnSocketError(e); }; client.SocketSecurity += (s, e) => { OnSocketSecurity(e); }; SocketClients.Add(client); client.BeginAuthenticateAsServer(ClientCertificateRequired, CheckCertificateRevocation, ServerCertificate, CertificateValidationCallback, SslProtocol); ContinueAcceptConnections(); } catch (SocketException e) { if (e.ErrorCode != 10004) { OnSocketError(new SocketErrorEventArgs(LocalEndPoint, e)); } } catch (Exception e) { OnError(new ErrorEventArgs(LocalEndPoint, e)); } }