public void AcceptCallback(IAsyncResult ar) { allDone.Set(); Socket listener = (Socket)ar.AsyncState; Socket handler = listener.EndAccept(ar); StateReader stateReader = new StateReader(); stateReader.workSocket = handler; stateReader.totalReceivedBuffer = handler.Available; //Console.WriteLine(stateReader.totalReceivedBuffer+""); handler.BeginReceive(stateReader.buffer, 0, StateReader.BufferSize - stateReader.totalReceivedBuffer, 0, new AsyncCallback(ReadCallback), stateReader); }
public void ReadCallback(IAsyncResult ar) { string content = String.Empty; StateReader stateReader = (StateReader)ar.AsyncState; Socket handler = stateReader.workSocket; int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { stateReader.sb.Append(Encoding.ASCII.GetString(stateReader.buffer, 0, bytesRead)); content = stateReader.sb.ToString(); if (content.IndexOf("<EOF>") > -1 || content.IndexOf("<IPIF>") > -1) { Console.WriteLine("Read {0} bytes from socket. \n Data : {1}", content.Length, content); //richTextBox.Text=content; richTextBox.Invoke((MethodInvoker) delegate { if (content.IndexOf("<IPIF>") == -1) { richTextBox.Text += content.Replace("<EOF>", "\n"); } else { InformationContainer.PushIPAddressesIntoLoop(content.Replace("<IPIF>", "")); } }); // Echo the data back to the client. Send(handler, content); } else { handler.BeginReceive(stateReader.buffer, 0, StateReader.BufferSize, 0, new AsyncCallback(ReadCallback), stateReader); } } }