public static void ReadCallback(IAsyncResult ar) { // 내용을 담을 변수 String content = String.Empty; // 비동기상태의 객체에서 상태객체를 연결합니다. MineSocket state = (MineSocket)ar.AsyncState; Socket handler = state.clSocket; // Client소켓에서 데이터를 읽습니다. int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { // 들어온데이터마다 모두 StringBuilder에 저장합니다. state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead)); content = state.sb.ToString(); if (content.IndexOf("<EOF>") > -1) { // All the data has been read from the client. display it on then console. Console.WriteLine("Read {0} bytes from socket. \n Data : {1}", content.Length, content); SendData(handler, content); } else { handler.BeginReceive(state.buffer, 0, MineSocket.BufferSize, 0, new AsyncCallback(ReadCallback), state); } } }
public static void AcceptCallback(IAsyncResult ar) { // 대기쓰레드 재실행 allDone.Set(); // 비동기 Callback의 사용자정의 다시 소켓을 정의함. Socket listener = (Socket)ar.AsyncState; // 들어오는 연결시도를 모두 비동기적으로 받아들이고 원격호스트통신을 진행할 새로운 소켓을 생성 Socket handler = listener.EndAccept(ar); // Client소켓과의 연결을 위한 설정을 연결한다. MineSocket state = new MineSocket(); // Client소켓에 handler소켓을 연결한다. state.clSocket = handler; // 연결된 소켓에서 데이터를 비동기적으로 수신하기 시작합니다. handler.BeginReceive(state.buffer, 0, MineSocket.BufferSize, 0, new AsyncCallback(ReadCallback), state); }