void OnReceiveConnection(IAsyncResult _Asunc) { var _NewId = Guid.NewGuid().ToString(); try { lock (_WorkerSockets) { UserSock _US = new UserSock(_NewId, _MainSocket.EndAccept(_Asunc)); _WorkerSockets.Add(_NewId, _US); } NewClientConnected(_NewId); WaitForData(_NewId); _MainSocket.BeginAccept(new AsyncCallback(OnReceiveConnection), null); } catch (ObjectDisposedException) { // ToDo } catch (SocketException ex) { // ToDo if (_WorkerSockets.ContainsKey(_NewId)) { //Console.WriteLine("RemoteEndPoint: " + _WorkerSockets[_NewId].UserSocket.RemoteEndPoint.ToString()); //Console.WriteLine("LocalEndPoint: " + _WorkerSockets[_NewId].UserSocket.LocalEndPoint.ToString()); //Console.WriteLine("Closing socket from OnReceiveConnection"); // ToDo } ClientDisconnect(_NewId); } }
public void Connect(IPAddress address, int port) { var _NewId = Guid.NewGuid().ToString(); try { var _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _clientSocket.Connect(new IPEndPoint(address, port)); lock (_WorkerSockets) { UserSock _US = new UserSock(_NewId, _clientSocket); _WorkerSockets.Add(_NewId, _US); } NewClientConnected(_NewId); WaitForData(_NewId); } catch (Exception ex) { _Logger.Error(ex, "Something bad happened"); } }