Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void Read(IAsyncResult ar)
- {
- AsyncState State = (AsyncState)ar.AsyncState;
- int BytesReceived = State.Connection.EndReceive(ar);
- String Data = Encoding.Default.GetString(State.Buffer, 0, BytesReceived);
- State.DataReceived += Data;
- Array.Clear(State.Buffer, 0, State.Buffer.Length);
- Console.WriteLine("\r\nDados recebidos de {0} ({1} bytes):\r\n{2}", State.Connection.RemoteEndPoint, BytesReceived, Data);
- State.Connection.BeginReceive(State.Buffer, 0, State.Buffer.Length, SocketFlags.None, out State.Error, new AsyncCallback(Read), State);
- SocketError Error;
- State.Connection.Send(new byte[] {0x00}, 0, 1, SocketFlags.None, out Error);
- Console.WriteLine("Verificando conexão: {0}", Error);
- if(SocketError.ConnectionAborted.Equals(Error))
- {
- Console.WriteLine("\r\nConexão perdida com {0}\r\nTodos os dados recebidos:\r\n{1}", State.Connection.RemoteEndPoint, State.DataReceived);
- State.Connection.Close();
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement