Advertisement
Delta

Rum

May 15th, 2011
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1.         public void Read(IAsyncResult ar)
  2.         {
  3.             AsyncState State = (AsyncState)ar.AsyncState;
  4.  
  5.             int BytesReceived = State.Connection.EndReceive(ar);
  6.  
  7.             String Data = Encoding.Default.GetString(State.Buffer, 0, BytesReceived);
  8.             State.DataReceived += Data;
  9.             Array.Clear(State.Buffer, 0, State.Buffer.Length);
  10.  
  11.             Console.WriteLine("\r\nDados recebidos de {0} ({1} bytes):\r\n{2}", State.Connection.RemoteEndPoint, BytesReceived, Data);
  12.  
  13.             State.Connection.BeginReceive(State.Buffer, 0, State.Buffer.Length, SocketFlags.None, out State.Error, new AsyncCallback(Read), State);
  14.  
  15.             SocketError Error;
  16.             State.Connection.Send(new byte[] {0x00}, 0, 1, SocketFlags.None, out Error);
  17.             Console.WriteLine("Verificando conexão: {0}", Error);
  18.  
  19.             if(SocketError.ConnectionAborted.Equals(Error))
  20.             {
  21.                 Console.WriteLine("\r\nConexão perdida com {0}\r\nTodos os dados recebidos:\r\n{1}", State.Connection.RemoteEndPoint, State.DataReceived);
  22.                 State.Connection.Close();
  23.                 return;
  24.             }
  25.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement