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 OnReceived(IAsyncResult ar) { SocketInfoHost infoHost = (SocketInfoHost)ar.AsyncState; try { Socket handler = infoHost.SocketHandler; int read = handler.EndReceive(ar); //TODO when sending content is larger than receiving buffer size //if (read > 0) //{ // infoHost.Storage.Append(Encoding.ASCII.GetString(infoHost.buffer, 0, read)); // handler.BeginReceive(infoHost.buffer, 0, SocketInfoHost.BUFFER_SIZE, 0, new AsyncCallback(OnReceived), infoHost); //} //else //{ // if (infoHost.Storage.Length > 0) // { // //All of the data has been read, construct SocketInfoHost // infoHost.PunchTimeStamp(); // _infoRecords.Add(infoHost); // Console.WriteLine("received " + infoHost.Value); // } // handler.Close(); //} infoHost.Storage.Append(Encoding.ASCII.GetString(infoHost.buffer, 0, read)); infoHost.PunchTimeStamp(); _infoRecords.Add(infoHost); var contentReceived = infoHost.Storage.ToString(); Console.WriteLine("received " + contentReceived); if (contentReceived.Length > 10) { _log.Warn(contentReceived); } handler.Shutdown(SocketShutdown.Both); handler.Close(); } catch (Exception ex) { _log.Error("Receive Error " + infoHost.Storage.ToString(), ex); } }