public void ReceiveMessageCallback(IAsyncResult ar) { int received; try { received = workingSocket.EndReceive(ar); if (received > 0) { byte[] recBuf = new byte[received]; Array.Copy(RecvBuffer, recBuf, received); Console.WriteLine("메세지 받음: {0}", Encoding.UTF8.GetString(recBuf)); workingSocket.BeginReceive(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None, ReceiveMessageCallback, null); } else { Console.WriteLine("서버 연결 끊김"); ServerArgs args = new ServerArgs(); args.Message = "서버 연결 끊음"; ServerClosed(this, args); } } catch (Exception ex) { Console.WriteLine("자료 수신 대기 도중 오류 발생! 메세지: {0}", ex.Message); return; } }
public void SendMessage(String message) { try { byte[] buffer = Encoding.UTF8.GetBytes(message); workingSocket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, null, null); } catch (Exception ex) { Console.WriteLine("메세지 전송중 오류 발생!", ex.Message); ServerArgs args = new ServerArgs(); args.Message = "메세지 전송 오류"; ServerClosed(this, args); } }