/******************添加收到的消息******************/ public void AddMsg(string chatMsg) { Chatbox.SelectionAlignment = HorizontalAlignment.Left; Font font = new Font(FontFamily.GenericMonospace, 10, FontStyle.Regular); Chatbox.SelectionFont = font; Chatbox.SelectionColor = Color.Blue; string msg = System.DateTime.Now.ToLocalTime().ToString(); msg = msg + "\n" + ">>> " + chatMsg + "\n"; Chatbox.AppendText(msg); Chatbox.ScrollToCaret(); }
/******************发送消息******************/ private void Sendbutton_Click(object sender, EventArgs e) { if (Sendchatbox.Text == "") { MessageBox.Show("请先填写消息"); } else { try { //向服务器确认好友是否在线,得到IP NetworkStream stream = toServer.GetStream(); string sendMsg = "q" + friendID; byte[] sendByt = Encoding.ASCII.GetBytes(sendMsg); stream.Write(sendByt, 0, sendByt.Length); byte[] rcvByt = new byte[1024]; int rcvLength = stream.Read(rcvByt, 0, rcvByt.Length); string rcvMsg = Encoding.ASCII.GetString(rcvByt, 0, rcvLength); if (rcvMsg == "n") { MessageBox.Show("好友已离线"); } else { //发送该消息 sendMsg = "msg" + userID + Sendchatbox.Text; sendByt = Encoding.Unicode.GetBytes(sendMsg); if (sendByt.Length > 1024) { MessageBox.Show("一次输入请不要超过500个字"); return; } else { TcpClient tcptoFriend = new TcpClient(); tcptoFriend.Connect(rcvMsg, listenPort); NetworkStream streamtoFriend = tcptoFriend.GetStream(); streamtoFriend.Write(sendByt, 0, sendByt.Length); streamtoFriend.Close(); tcptoFriend.Close(); //stream.Close(); 会报错 //向chatbox添加该消息 Chatbox.SelectionAlignment = HorizontalAlignment.Right; Font font = new Font(FontFamily.GenericMonospace, 10, FontStyle.Regular); Chatbox.SelectionFont = font; Chatbox.SelectionColor = Color.Green; string msg = System.DateTime.Now.ToLocalTime().ToString(); msg = msg + "\n" + ">>> " + Sendchatbox.Text + "\n"; Chatbox.AppendText(msg); Chatbox.ScrollToCaret(); Sendchatbox.Clear(); Sendchatbox.Focus(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }