/// <summary> /// 비동기 수신 처리 /// </summary> /// <param name="ar"></param> private void ReceiveCallBack(IAsyncResult ar) { AsyncSocketData asd = (AsyncSocketData)ar.AsyncState; try { // check receive data. size. int recv = asd.AsyncSocket.EndReceive(ar); if (recv == 0) { this.FireClosed(); } //수신된 Data : asd.Buffer this.FireReceived(asd.Buffer, recv); //Console.WriteLine("EchoServer Received {0} Bytes , {1} ", recv, Encoding.Default.GetString(asd.Buffer, 0, recv)); cSocket.BeginReceive(asd.Buffer, 0, 8192, SocketFlags.None, new AsyncCallback(ReceiveCallBack), asd); } catch { // write exception message. //Console.WriteLine(exption.Message); // close socket and dispose byte [] asd.AsyncSocket.Close(); asd.Buffer = null; //Error 이벤트 발생 this.FireClosed(); } }
/// <summary> /// 수신을 시작한다. /// </summary> /// <param name="socket">접속된 소켓</param> private void ReceiveStart() { try { byte[] buffer = new byte[8192]; AsyncSocketData ad = new AsyncSocketData(); ad.AsyncSocket = cSocket; ad.Buffer = buffer; cSocket.BeginReceive(ad.Buffer, 0, 8192, SocketFlags.None, new AsyncCallback(ReceiveCallBack), ad); } catch { // 연결 실패를 알린다. //OnConnectFailed(ee); } }
/// <summary> /// 수신을 시작한다. 이 메소드는, 클라이언트 측에서는 AsyncConnectCallback() 내에서 사용되고, 서버 측에서는 Accepted 이벤트 함수에서 사용한다. /// </summary> /// <param name="socket">접속된 소켓</param> public void Start(Socket sock) { try { mSocket = sock; byte[] buffer = new byte[8192]; AsyncSocketData ad = new AsyncSocketData(); ad.AsyncSocket = mSocket; ad.Buffer = buffer; mSocket.BeginReceive(ad.Buffer, 0, 8192, SocketFlags.None, new AsyncCallback(ReceiveCallBack), ad); } catch { // 연결 실패를 알린다. //OnConnectFailed(ee); } }
/// <summary> /// 비동기 송신 처리 /// </summary> /// <param name="ar"></param> private void SendCallBack(IAsyncResult ar) { AsyncSocketData asd = (AsyncSocketData)ar.AsyncState; try { if (this.Connected) { int send = asd.AsyncSocket.EndSend(ar); if (send == 0) { //Error 이벤트 발생 this.FireClosed(); } } else { } } catch { // write exception message. if (asd != null) { // close socket and dispose byte [] if (asd.AsyncSocket != null) { asd.AsyncSocket.Close(); } if (asd.Buffer != null) { asd.Buffer = null; } asd = null; //Error 이벤트 발생 this.FireClosed(); } } }
/// <summary> /// 비동기 송신 시도 /// </summary> /// <param name="data_buffer"></param> public void Send(byte[] buffer, int lng) { AsyncSocketData ad = new AsyncSocketData(); try { if (this.Connected) { ad.AsyncSocket = cSocket; ad.Buffer = buffer; cSocket.BeginSend(buffer, 0, lng, SocketFlags.None, new AsyncCallback(SendCallBack), ad); //cSocket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(SendCallBack), ad); } else { } } catch { } }