private void OnReceived(IAsyncResult ar) { SocketInfoHost infoHost = (SocketInfoHost)ar.AsyncState; Socket handler = infoHost.SocketHandler; int read = handler.EndReceive(ar); var content = Encoding.ASCII.GetString(infoHost.buffer, 0, read); if (content.Length > 30) { _log.Warn(content); } //content = content.Trim('\0'); //if (!string.IsNullOrEmpty(content)) infoHost.Storage.Append(content); //All of the data has been read, construct SocketInfoHost infoHost.PunchTimeStamp(); _infoRecords.Add(infoHost); Console.WriteLine("received " + infoHost.Value); SocketInfoHost newInfoHost = SocketInfoHost.Create(handler); handler.BeginReceive(newInfoHost.buffer, 0, SocketInfoHost.BUFFER_SIZE, 0, new AsyncCallback(OnReceived), newInfoHost); // CloseHandle(handler); }
private void OnConnected(IAsyncResult AsyncResult) { Socket socketHandler = null; try { socketHandler = this._listener.EndAccept(AsyncResult); _connected.Set(); IPEndPoint iPEndPoint = (IPEndPoint)socketHandler.RemoteEndPoint; Console.WriteLine("socket connected from {0}!", iPEndPoint.Address.ToString()); ConfigureHandlerSocket(socketHandler); var infoHost = SocketInfoHost.Create(socketHandler); socketHandler.BeginReceive(infoHost.buffer, 0, SocketInfoHost.BUFFER_SIZE, 0, new AsyncCallback(OnReceived), infoHost); } catch (Exception e) { _connected.Set(); if (socketHandler != null) { socketHandler.Close(); } _log.Error(e.Message); } //finally //{ // ThreadPool.QueueUserWorkItem(new WaitCallback(this.BeginAccept), null); //} }
private void OnConnectionAccepted1(IAsyncResult AsyncResult) { Socket socketHandler = null; try { socketHandler = this._listener.EndAccept(AsyncResult); //this._listener.BeginAccept(new AsyncCallback(this.OnConnectionAccepted1), this._listener); _connected.Set(); IPEndPoint iPEndPoint = (IPEndPoint)socketHandler.RemoteEndPoint; Console.WriteLine("socket connected from {0}!", iPEndPoint.ToString()); InitializeHandlerSocker(socketHandler); SocketInfoHost hostInfo = SocketInfoHost.Create(socketHandler); socketHandler.BeginReceive(hostInfo.buffer, 0, SocketInfoHost.BUFFER_SIZE, 0, new AsyncCallback(OnReceived), hostInfo); } catch (Exception e) { _connected.Set(); if (socketHandler != null) { socketHandler.Close(); } _log.Error(e.Message); } }