private void AcceptCallback(IAsyncResult result) { var listener = (TcpListener)result.AsyncState; try { var socket = listener.EndAcceptSocket(result); var client = new TextCommandClient(this, socket); Log.Debug("Accepting connection from {RemoteEndPoint}", client.Socket.RemoteEndPoint); lock (sync) { clients.Add(client); } listener.BeginAcceptSocket(AcceptCallback, listener); socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, client); } catch (SocketException ex) { Log.Error("Accept error", ex); } catch (ObjectDisposedException) { } }
public void CloseClient(TextCommandClient client) { Log.Debug("Closing client {RemoteEndPoint}", client.Socket.RemoteEndPoint); lock (sync) { clients.Remove(client); } client.Dispose(); }