private void DoChat() { NetworkStream stream = null; try { string message = string.Empty; while (true) { stream = TcpSocket.GetStream(); byte[] sizeBuf = new byte[TcpSocket.ReceiveBufferSize]; stream.Read(sizeBuf, 0, (int)TcpSocket.ReceiveBufferSize); int size = BitConverter.ToInt32(sizeBuf, 0); MemoryStream memoryStream = new MemoryStream(); while (size > 0) { byte[] buffer; if (size < TcpSocket.ReceiveBufferSize) { buffer = new byte[size]; } else { buffer = new byte[TcpSocket.ReceiveBufferSize]; } int rec = stream.Read(buffer, 0, buffer.Length); size -= rec; memoryStream.Write(buffer, 0, buffer.Length); } memoryStream.Close(); byte[] data = memoryStream.ToArray(); memoryStream.Dispose(); message = Encoding.UTF8.GetString(data); if (message == "") { throw new Exception("공백 출력 == 상대방 소켓 끊어짐"); } serverEvent.ReceiveServerLog(message); OnReceived(message, ClientList[TcpSocket].ToString(), false); } } catch (Exception ex) { serverEvent.ErrorLog("DoChat", ex.Message); if (TcpSocket != null) { OnDisconnected(TcpSocket); TcpSocket.Close(); stream.Close(); if (ThreadHandler.IsAlive == true) { ThreadHandler.Interrupt(); ThreadHandler.Abort(); } } } }
private void DoChat() { NetworkStream stream = null; try { byte[] buffer = new byte[1024]; string message = string.Empty; int bytes = 0; while (true) { stream = TcpSocket.GetStream(); bytes = stream.Read(buffer, 0, buffer.Length); message = Encoding.Unicode.GetString(buffer, 0, bytes); serverEvent.ReceiveServerLog(message); //받은 메시지 원문 기록 if (OnReceived != null) { if (message.Substring(message.LastIndexOf("$") + 1) == "@") { OnReceived(message.Substring(0, message.LastIndexOf("$")), ClientList[TcpSocket].ToString(), true); } else { OnReceived(message.Substring(0, message.LastIndexOf("$")), ClientList[TcpSocket].ToString(), false); } } } } catch (Exception ex) { serverEvent.ErrorLog("DoChat", ex.Message); if (TcpSocket != null) { TcpSocket.Close(); stream.Close(); if (ThreadHandler.IsAlive == true) { ThreadHandler.Interrupt(); ThreadHandler.Abort(); } if (OnDisconnected != null) { OnDisconnected(TcpSocket); } } } }
private void DoChat() { NetworkStream stream = null; try { string message = string.Empty; while (true) { stream = TcpSocket.GetStream(); byte[] sizeBuf = new byte[TcpSocket.ReceiveBufferSize]; stream.Read(sizeBuf, 0, (int)TcpSocket.ReceiveBufferSize); int size = BitConverter.ToInt32(sizeBuf, 0); MemoryStream ms = new MemoryStream(); while (size > 0) { byte[] buffer; if (size < TcpSocket.ReceiveBufferSize) { buffer = new byte[size]; } else { buffer = new byte[TcpSocket.ReceiveBufferSize]; } int rec = stream.Read(buffer, 0, buffer.Length); size -= rec; ms.Write(buffer, 0, buffer.Length); } ms.Close(); byte[] data = ms.ToArray(); ms.Dispose(); message = Encoding.UTF8.GetString(data); //MessageBox.Show(message); serverEvent.ReceiveServerLog(message); //받은 메시지 원문 기록 OnReceived(message, ClientList[TcpSocket].ToString(), false); /* * if (OnReceived != null) * { * if (message.Substring(message.LastIndexOf("$") + 1) == "@") * OnReceived(message.Substring(0, message.LastIndexOf("$")), ClientList[TcpSocket].ToString(), true); * else * ; * } * */ } } catch (Exception ex) { serverEvent.ErrorLog("DoChat", ex.Message); if (TcpSocket != null) { /* * ServerSecurity Encrypted = new ServerSecurity(); * * * string Encrypted = Encrypt.EncryptedMessage(IpEndPoint.Address + ":" + IpEndPoint.Port + "/" + " 연결이 종료되었습니다." * , serverEvent.LocalIPAddress() + ":" + PortNum() + "/" + ClientList[TcpSocket].ToString()); * MessageBox.Show(Encrypted); * * OnReceived(Encrypted, ClientList[TcpSocket].ToString(), true); * */ TcpSocket.Close(); stream.Close(); if (ThreadHandler.IsAlive == true) { ThreadHandler.Interrupt(); ThreadHandler.Abort(); } if (OnDisconnected != null) { OnDisconnected(TcpSocket); } } } }