实现用C#做一个聊天室客户端的代码:using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Text;using ;using .Sockets;using System.Windows.Forms;namespace EasyChat{public partial class login_frm : Form{///<summary>/// IP地址///</summary>private IPAddress _ipAddr;#region登录窗体构造函数///<summary>///构造函数,自动生成///</summary>public login_frm(){InitializeComponent();}#endregion#region登录窗体的私有方法///<summary>///验证登录信息///</summary>///<returns>验证结果</returns>private bool ValidateInfo(){if (user_tb.Text.Trim() == string.Empty){MessageBox.Show("请填写用户名!","提示",MessageBoxButtons.OK,rmation);return false;}if (!IPAddress.TryParse(svrip_tb.Text, out _ipAddr)) {MessageBox.Show("IP地址不合法!","提示",MessageBoxButtons.OK,rmation);return false;}int _port;if (!int.TryParse(svrport_tb.Text, out _port)){MessageBox.Show("端口号不合法!","提示",MessageBoxButtons.OK,rmation);return false;}else{if (_port < 1024 || _port > 65535){MessageBox.Show("端口号不合法!","提示",MessageBoxButtons.OK,rmation);return false;}}return true;}///<summary>///取消,关闭窗体///</summary>///<param name="sender"></param>///<param name="e"></param>private void cancel_btn_Click(object sender, EventArgs e){this.Close();}///<summary>///登录///</summary>///<param name="sender"></param>///<param name="e"></param>private void login_btn_Click(object sender, EventArgs e){//验证数据合法性if (!ValidateInfo()){return;}int port = int.Parse(svrport_tb.Text);//向服务器发出连接请求TCPConnection conn = new TCPConnection(_ipAddr, port);TcpClient _tcpc = conn.Connect();if (_tcpc == null){MessageBox.Show("无法连接到服务器,请重试!","错误",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);}else{NetworkStream netstream = _tcpc.GetStream();//提供用于访问网络的基本数据流//向服务器发送用户名以确认身份netstream.Write(Encoding.Unicode.GetBytes(user_tb.Text), 0, Encoding.Unicode.GetBytes(user_tb.Text).Length);//得到登录结果byte[] buffer = new byte[50];netstream.Read(buffer, 0, buffer.Length);//该方法将数据读入 buffer 参数并返回成功读取的字节数。
如果没有可以读取的数据,则 Read 方法返回 0。
string connResult = Encoding.Unicode.GetString(buffer).TrimEnd('\0');if (connResult.Equals("cmd::Failed")){MessageBox.Show("您的用户名已经被使用,请尝试其他用户名!","提示",MessageBoxButtons.OK,rmation);return;}else{string svrskt = svrip_tb.Text + ":" + svrport_tb.Text;chat_frm chatFrm = new chat_frm(user_tb.Text, netstream, svrskt);//一个巧妙的参数传递方法chatFrm.Owner = this;this.Hide();chatFrm.Show();}}}///<summary>///初始化登录信息///</summary>///<param name="sender"></param>///<param name="e"></param>private void login_frm_Load(object sender, EventArgs e){svrip_tb.Text = "192.168.1.164";svrport_tb.Text = "8888";user_tb.Focus();}#endregion}}//===================================================================== using System;using System.Collections;using System.Collections.Specialized;using ponentModel;using System.Data;using System.Drawing;using System.Text;using .Sockets;//++++using System.Windows.Forms;using System.Threading;//++++using System.Runtime.Serialization;//++++using System.Runtime.InteropServices;//++++using System.IO;using System.Runtime.Serialization.Formatters.Binary;//++++using System.Media;//+++++namespace EasyChat{public partial class chat_frm : Form{#region私有字段///<summary>///当前用户名///</summary>private string _username = null;///<summary>///数据缓冲区大小///</summary>private int _maxPacket = 2048;//2K的缓冲区///<summary>///用于接受消息的线程///</summary>private Thread _receiveThread = null;///<summary>///用于接受和发送的网络流,从登录窗体得到///</summary>private NetworkStream _nws = null;///<summary>///服务器套接字的字符串形式,从登录窗体得到///</summary>private string _svrskt = null;///<summary>///播放消息提示的播放器///</summary>private SoundPlayer _sp1 = new SoundPlayer(Properties.Resources.msg); private SoundPlayer _sp2 = new SoundPlayer(Properties.Resources.nudge); ///<summary>///指示是否最小化到托盘private bool _hideFlag = false;#endregion#region聊天窗体构造函数///<summary>///构造函数,得到登录窗体的一些信息///</summary>///<param name="userName">当前用户名</param>///<param name="nws">接受和发送消息的网络流</param>///<param name="svrskt">服务器套接字的字符串形式</param>public chat_frm(string userName, NetworkStream nws, string svrskt){InitializeComponent();_username = userName;_nws = nws;_svrskt = svrskt;}#endregion#region聊天窗体的私有方法///<summary>///保存聊天记录///</summary>///<param name="sender"></param>///<param name="e"></param>private void save_btn_Click(object sender, EventArgs e){DialogResult ret;SaveFileDialog sfd = new SaveFileDialog();sfd.Filter = "文本文件(*.txt)|*.txt";sfd.AddExtension = true;if ((ret = sfd.ShowDialog()) == DialogResult.OK){chatrcd_rtb.SaveFile(sfd.FileName, RichTextBoxStreamType.PlainText); }}///清除聊天记录///</summary>///<param name="sender"></param>///<param name="e"></param>private void clear_btn_Click(object sender, EventArgs e){DialogResult ret;ret = MessageBox.Show("确定清除吗?清除后不可恢复。