private void Connect_Completed(object sender, SocketAsyncEventArgs e) { m_Client = (Socket)sender; if (m_Client.Connected) { Telegram _telegram = new Telegram(); SocketAsyncEventArgs _receiveArgs = new SocketAsyncEventArgs(); _receiveArgs.UserToken = _telegram; _receiveArgs.SetBuffer(_telegram.GetBuffer(), 0, 4); _receiveArgs.Completed += new EventHandler<SocketAsyncEventArgs>(Recieve_Completed); m_Client.ReceiveAsync(_receiveArgs); SendDisplay("Server Connection Success", ChatType.System); } else { m_Client = null; SendDisplay("Connection Failed!", ChatType.System); SendDisplay("Press Any Key...", ChatType.System); } }
private void Recieve_Completed(object sender, SocketAsyncEventArgs e) { Socket _client = (Socket)sender; Telegram _telegram = new Telegram(); _telegram.SetLength(e.Buffer); _telegram.InitData(); if (_client.Connected) { //서버에서 Send()함수를 통해 전송된 내용을 Receive()함수로 수신합니다. _client.Receive(_telegram.Data, _telegram.DataLength, SocketFlags.None); //수신된 Data중 맨 처음 글자를 분석합니다. "M"인경우 Message, "F"인경우 File string strMessageType = Encoding.Unicode.GetString(_telegram.Data).Substring(0, 1); int nLength = Encoding.Unicode.GetString(_telegram.Data).Length; //일반 Message이므로 Console에 뿌려줍니다. if (strMessageType == "M") { SendDisplay(Encoding.Unicode.GetString(_telegram.Data).Substring(1, nLength - 1), ChatType.Receive); } //File인 경우 else if (strMessageType == "F") { try { //수신된 맨 앞 2바이트는 File,Message구분에 사용되었으므로 건너뜁니다. //다음 4byte를 분석해 file이름의 크기를 구합니다. byte[] byteArrayFileNameLength = new byte[4]; System.Buffer.BlockCopy(_telegram.Data, 2, byteArrayFileNameLength, 0, 4); //file이름의 크기만큼 분석해 file이름을 얻어옵니다. int nFileNameLength = BitConverter.ToInt32(byteArrayFileNameLength, 0); byte[] byteArrayFileName = new byte[nFileNameLength]; System.Buffer.BlockCopy(_telegram.Data, 2 + 4, byteArrayFileName, 0, nFileNameLength); //나머지 데이터는 실제 File을 구성하는 Data입니다. int nFileDataLength = _telegram.Data.Length - 2 - 4 - nFileNameLength; byte[] byteArrayFileData = new byte[nFileDataLength]; System.Buffer.BlockCopy(_telegram.Data, 2 + 4 + nFileNameLength, byteArrayFileData, 0, nFileDataLength); //클라이언트가 실행중인 경로에 file을 저장합니다. string strRootPath = System.IO.Directory.GetCurrentDirectory(); string strFileName = Encoding.Unicode.GetString(byteArrayFileName); strFileName = strRootPath + "\\"+ strFileName; File.WriteAllBytes(strFileName, byteArrayFileData); SendDisplay(string.Format("File is recieved : {0}", strFileName), ChatType.Receive); } catch { //... return; } } _client.ReceiveAsync(e); } else { SendDisplay("Connection Failed!", ChatType.System); m_Client = null; } }
public void DataInput() { String sData; Telegram _telegram = new Telegram(); SendDisplay("ChattingProgram ClientStart", ChatType.System); while (true) { sData = Console.ReadLine(); if (sData.CompareTo("exit") == 0) { break; } else { if (m_Client != null) { if (!m_Client.Connected) { m_Client = null; SendDisplay("Connection Failed!", ChatType.System); SendDisplay("Press Any Key...", ChatType.System); } else { _telegram.SetData(sData); SocketAsyncEventArgs _sendArgs = new SocketAsyncEventArgs(); _sendArgs.SetBuffer(BitConverter.GetBytes(_telegram.DataLength), 0, 4); _sendArgs.Completed += new EventHandler<SocketAsyncEventArgs>(Send_Completed); _sendArgs.UserToken = _telegram; m_Client.SendAsync(_sendArgs); } } else { break; } } } }