?? chatclient.cs
字號:
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.cbPrivate);
this.Controls.Add(this.lstUsers);
this.Controls.Add(this.btnSend);
this.Controls.Add(this.tbSendContent);
this.Name = "ChatClientForm";
this.Text = "ChatClient";
this.Closing += new System.ComponentModel.CancelEventHandler(this.ChatClientForm_Closing);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new ChatClientForm());
}
//ServerResponse()方法用于接收從服務器發回的信息,
//根據不同的命令,執行相應的操作
private void ServerResponse()
{
//定義一個byte數組,用于接收從服務器端發送來的數據,
//每次所能接收的數據包的最大長度為1024個字節
byte[] buff=new byte[1024];
string msg;
int len;
try
{
if(!Stream.CanRead)
{
return;
}
stopFlag = false;
while(!stopFlag)
{
//從流中得到數據,并存入到buff字符數組中
len=Stream.Read(buff,0,buff.Length);
if (len < 1)
{
Thread.Sleep(200);
continue;
}
//將字符數組轉化為字符串
msg=System.Text.Encoding.Default.GetString(buff,0,len);
msg.Trim();
string[] tokens=msg.Split(new Char[]{'|'});
//tokens[0]中保存了命令標志符(LIST或JOIN或QUIT)
if (tokens[0].ToUpper()== "OK")
{
//處理響應
add("命令執行成功");
}
else if (tokens[0].ToUpper()== "ERR")
{
//命令執行錯誤
add("命令執行錯誤:" + tokens[1]);
}
else if(tokens[0]== "LIST")
{
//此時從服務器返回的消息格式:
//命令標志符(LIST)|用戶名1|用戶名|2...(所有在線用戶名)|
add("獲得用戶列表");
//更新在線用戶列表
lstUsers.Items.Clear();
for(int i=1;i<tokens.Length-1;i++)
{
lstUsers.Items.Add(tokens[i].Trim());
}
}
else if(tokens[0]== "JOIN")
{
//此時從服務器返回的消息格式:
//命令標志符(JOIN)|剛剛登入的用戶名|
add(tokens[1]+" "+"已經進入了聊天室");
this.lstUsers.Items.Add(tokens[1]);
if (this.tbUserName.Text ==tokens[1])
{
this.state = CONNECTED;
}
}
else if(tokens[0]== "QUIT")
{
if (this.lstUsers.Items.IndexOf(tokens[1])>-1)
{
this.lstUsers.Items.Remove(tokens[1]);
}
add("用戶:" + tokens[1] + " 已經離開");
}
else
{
//如果從服務器返回的其他消息格式,
//則在ListBox控件中直接顯示
add(msg);
}
}
//關閉連接
tcpClient.Close();
}
catch
{
add("網絡發生錯誤");
}
}
private void add(string msg)
{
if (!color.IsEmpty)
{
this.rtbMsg.SelectionColor = color;
}
this.rtbMsg.SelectedText = msg + "\n";
}
//當點擊“發送”按鈕時,便會進入btnSend_Click處理程序。
//在btnSend_Click處理程序中,如果不是私聊,
//將“CHAT”命令發送給服務器,
//否則(為私聊),將“PRIV”命令發送給服務器,
//注意命令格式一定要與服務器端的命令格式一致
private void btnSend_Click(object sender, System.EventArgs e)
{
try
{
if(!this.cbPrivate.Checked)
{
//此時命令的格式是:
//命令標志符(CHAT)|發送者的用戶名:發送內容|
string message="CHAT|"+ this.tbUserName.Text +":"+
tbSendContent.Text+"|";
tbSendContent.Text="";
tbSendContent.Focus();
//將字符串轉化為字符數組
Byte[]outbytes=System.Text.Encoding.Default.GetBytes(
message.ToCharArray());
Stream.Write(outbytes,0,outbytes.Length);
}
else
{
if(lstUsers.SelectedIndex==-1)
{
MessageBox.Show("請在列表中選擇一個用戶","提示信息",
MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
return;
}
string receiver=lstUsers.SelectedItem.ToString();
//消息的格式是:
//命令標志符(PRIV)|發送者的用戶名|接收者的用戶名|發送內容|
string message="PRIV|"+this.tbUserName.Text+"|"+receiver+"|"+
tbSendContent.Text+"|";
tbSendContent.Text="";
tbSendContent.Focus();
//將字符串轉化為字符數組
byte[] outbytes=System.Text.Encoding.ASCII.GetBytes(
message.ToCharArray());
Stream.Write(outbytes,0,outbytes.Length);
}
}
catch
{
this.rtbMsg.AppendText("網絡發生錯誤");
}
}
//連接聊天服務器
private void btnLogin_Click(object sender, System.EventArgs e)
{
if (state == CONNECTED)
{
return;
}
if(this.tbUserName.Text.Length==0)
{
MessageBox.Show("請輸入您的呢稱!","提示信息",
MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
this.tbUserName.Focus();
return;
}
try
{
//創建一個客戶端套接字,它是Login的一個公共屬性,
//將被傳遞給ChatClient窗體
tcpClient=new TcpClient();
//向指定的IP地址的服務器發出連接請求
tcpClient.Connect(IPAddress.Parse(txtHost.Text),
Int32.Parse(txtPort.Text));
//獲得與服務器數據交互的流通道(NetworkStream)
Stream=tcpClient.GetStream();
//啟動一個新的線程,執行方法this.ServerResponse(),
//以便來響應從服務器發回的信息
Thread thread=new Thread(new ThreadStart(this.ServerResponse));
thread.Start();
//向服務器發送“CONN”請求命令,
//此命令的格式與服務器端的定義的格式一致,
//命令格式為:命令標志符(CONN)|發送者的用戶名|
string cmd="CONN|"+this.tbUserName.Text+"|";
//將字符串轉化為字符數組
Byte[] outbytes=System.Text.Encoding.Default.GetBytes(
cmd.ToCharArray());
Stream.Write(outbytes,0,outbytes.Length);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//設置字體顏色
private void btnColor_Click(object sender, System.EventArgs e)
{
ColorDialog colorDialog1 = new ColorDialog();
colorDialog1.Color = this.rtbMsg.SelectionColor;
if(colorDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
colorDialog1.Color !=this.rtbMsg.SelectionColor)
{
this.rtbMsg.SelectionColor = colorDialog1.Color;
color = colorDialog1.Color;
}
}
//當單擊“離開”按鈕時,便進入了btnExit_Click 處理程序。
//在btnExit_Click 處理程序中,
//將“EXIT”命令發送給服務器,此命令格式要與服務器端的命令格式一致
private void btnExit_Click_1(object sender, System.EventArgs e)
{
if (state == CONNECTED)
{
string message="EXIT|"+this.tbUserName.Text+"|";
//將字符串轉化為字符數組
Byte[]outbytes=System.Text.Encoding.Default.GetBytes(
message.ToCharArray());
Stream.Write(outbytes,0,outbytes.Length);
this.state = CLOSED;
this.stopFlag = true;
this.lstUsers.Items.Clear();
}
}
private void ChatClientForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
btnExit_Click_1(sender, e);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -