private void ServerResponse()//响应服务器的命令 { //定义一个byte数组,用于接收从服务器端发来的数据 //每次所能接受的数据包的最大长度为1024个字节 byte[] buff = new byte[1024]; string msg; int len; try { if (Stream.CanRead == false) { return; } stopFlag = false; while (!stopFlag) { //从流中得到数据,并存入到buff字符数组中 len = Stream.Read(buff, 0, buff.Length); if (len < 1) { Thread.Sleep(200); continue; } msg = Encoding.GetEncoding("gb2312").GetString(buff, 0, len); msg.Trim(); string[] tokens = msg.Split(new char[] { '|' }); //把命令分解开 tokens[0]为命令类型 //下面为处理各种命令 if (tokens[0].ToUpper() == "PRIV") { string sender = tokens[1]; string content = tokens[3]; string time = tokens[4]; int i; for (i = 0; FriendInfo[i].id != sender; i++) ; if (FriendInfo[i].chat==null || FriendInfo[i].chat.Visible == false) { //窗口未打开的情况 hashtable.Add((count++), sender + "(" + time + ")" + "\t" + content + "\n"); MessageBox.Show("您收到一条新消息,请进入消息中心"); } else { string addmsg = (sender + "(" + time + ")" + "\n" + content + "\n"); FriendInfo[i].chat.MsgReceived.AppendText(addmsg); } } else if(tokens[0].ToUpper()=="OKPRIV") { string receiver = tokens[1]; string content = tokens[2]; int i; for (i = 0; FriendInfo[i].id != receiver; i++) ; if (FriendInfo[i].chat == null || FriendInfo[i].chat.Visible == false) { //如果窗口没有打开 待处理 } else { string addmsg = "\""+content+"\""+"(已成功发送)\n"; FriendInfo[i].chat.MsgReceived.AppendText(addmsg); } } else if (tokens[0].ToUpper() == "DOWNLOAD") { hashtable.Add(hashtable.Count + 1, tokens[1]); } else if (tokens[0].ToUpper() == "VIBRATE") { string sender = tokens[1]; int i; for (i = 0; FriendInfo[i].id != sender; i++) ; if (FriendInfo[i].chat == null) { FriendInfo[i].chat = new Chat(FriendInfo[i]); } FriendInfo[i].chat.Show(); FriendInfo[i].chat.MsgReceived.AppendText("Receive窗口震动\n"); FriendInfo[i].chat.vibrate(); } else if (tokens[0].ToUpper() == "LIST") { //暂时未定义 //此时从服务器返回的消息格式:命令标志符LIST|用户名1|用户名2|。。(所有在线用户名) /*lstUsers.Items.Clear(); for (int i = 1; i < tokens.Length - 1; i++) { lstUsers.Items.Add(tokens[i].Trim()); }*/ } else if (tokens[0] == "FRIREQ") { string id = tokens[1]; string content = tokens[3]; ReceiveRequest rr = new ReceiveRequest(id, content); rr.ShowDialog(); if (rr.DialogResult == DialogResult.OK) { MessageBox.Show("接受" + id + "为好友"); } } else if (tokens[0] == "REFREQ") { MessageBox.Show("您的好友请求被拒绝:" + tokens[1]); } else if (tokens[0] == "ACCREQ") { MessageBox.Show("您的好友已通过\n" + tokens[1] + "已成为您的好友"); } else if (tokens[0].ToUpper() == "OKONLINE") { OnlineState.Text = "在线"; this.state = CONNECTED; } else if (tokens[0].ToUpper() == "ONLINE") { if (tokens[1] != myInfo.id) { MessageBox.Show(tokens[1] + "上线"); } else { this.state = CONNECTED; } } else if (tokens[0].ToUpper() == "OFFLINE") { if (tokens[1] != myInfo.id) { MessageBox.Show(tokens[1] + "下线"); } else { this.state = CONNECTED; } } else if (tokens[0].ToUpper() == "FRESH") { FriList.Clear(); imageList.Images.Clear(); imageList_small.Images.Clear(); LoadProgram(); } else if (tokens[0].ToUpper() == "ERROR") { if (tokens[1] == "ISONLINE") { MessageBox.Show("该账户已经登陆,此窗口会自动关闭"); this.state = CLOSED; this.Close(); } else if (tokens[1] == "SERVERERR") { MessageBox.Show("服务器连接错误,信息并未成功发送"); } } else { //如果从服务器返回的其他消息格式,则直接显示 MessageBox.Show(msg, "客户端收到其他种类的消息"); } } //关闭连接 tcpClient.Close(); } catch(Exception ex) { MessageBox.Show(ex.Message,"与服务器的连接断开"); this.state = CLOSED; } }
private void ServerResponse()//响应服务器的命令 { //定义一个byte数组,用于接收从服务器端发来的数据 //每次所能接受的数据包的最大长度为1024个字节 byte[] buff = new byte[1024]; string msg; int len; try { if (Stream.CanRead == false) { return; } stopFlag = false; while (!stopFlag) { //从流中得到数据,并存入到buff字符数组中 len = Stream.Read(buff, 0, buff.Length); if (len < 1) { Thread.Sleep(200); continue; } msg = Encoding.GetEncoding("gb2312").GetString(buff, 0, len); msg.Trim(); string[] tokens = msg.Split(new char[] { '|' }); //把命令分解开 tokens[0]为命令类型 //下面为处理各种命令 if (tokens[0].ToUpper() == "PRIV") { string sender = tokens[1]; string content = tokens[3]; string time = tokens[4]; int i; for (i = 0; FriendInfo[i].id != sender; i++) { ; } if (FriendInfo[i].chat == null || FriendInfo[i].chat.Visible == false) { //窗口未打开的情况 hashtable.Add((count++), sender + "(" + time + ")" + "\t" + content + "\n"); MessageBox.Show("您收到一条新消息,请进入消息中心"); } else { string addmsg = (sender + "(" + time + ")" + "\n" + content + "\n"); FriendInfo[i].chat.MsgReceived.AppendText(addmsg); } } else if (tokens[0].ToUpper() == "OKPRIV") { string receiver = tokens[1]; string content = tokens[2]; int i; for (i = 0; FriendInfo[i].id != receiver; i++) { ; } if (FriendInfo[i].chat == null || FriendInfo[i].chat.Visible == false) { //如果窗口没有打开 待处理 } else { string addmsg = "\"" + content + "\"" + "(已成功发送)\n"; FriendInfo[i].chat.MsgReceived.AppendText(addmsg); } } else if (tokens[0].ToUpper() == "DOWNLOAD") { hashtable.Add(hashtable.Count + 1, tokens[1]); } else if (tokens[0].ToUpper() == "VIBRATE") { string sender = tokens[1]; int i; for (i = 0; FriendInfo[i].id != sender; i++) { ; } if (FriendInfo[i].chat == null) { FriendInfo[i].chat = new Chat(FriendInfo[i]); } FriendInfo[i].chat.Show(); FriendInfo[i].chat.MsgReceived.AppendText("Receive窗口震动\n"); FriendInfo[i].chat.vibrate(); } else if (tokens[0].ToUpper() == "LIST") { //暂时未定义 //此时从服务器返回的消息格式:命令标志符LIST|用户名1|用户名2|。。(所有在线用户名) /*lstUsers.Items.Clear(); * for (int i = 1; i < tokens.Length - 1; i++) * { * lstUsers.Items.Add(tokens[i].Trim()); * }*/ } else if (tokens[0] == "FRIREQ") { string id = tokens[1]; string content = tokens[3]; ReceiveRequest rr = new ReceiveRequest(id, content); rr.ShowDialog(); if (rr.DialogResult == DialogResult.OK) { MessageBox.Show("接受" + id + "为好友"); } } else if (tokens[0] == "REFREQ") { MessageBox.Show("您的好友请求被拒绝:" + tokens[1]); } else if (tokens[0] == "ACCREQ") { MessageBox.Show("您的好友已通过\n" + tokens[1] + "已成为您的好友"); } else if (tokens[0].ToUpper() == "OKONLINE") { OnlineState.Text = "在线"; this.state = CONNECTED; } else if (tokens[0].ToUpper() == "ONLINE") { if (tokens[1] != myInfo.id) { MessageBox.Show(tokens[1] + "上线"); } else { this.state = CONNECTED; } } else if (tokens[0].ToUpper() == "OFFLINE") { if (tokens[1] != myInfo.id) { MessageBox.Show(tokens[1] + "下线"); } else { this.state = CONNECTED; } } else if (tokens[0].ToUpper() == "FRESH") { FriList.Clear(); imageList.Images.Clear(); imageList_small.Images.Clear(); LoadProgram(); } else if (tokens[0].ToUpper() == "ERROR") { if (tokens[1] == "ISONLINE") { MessageBox.Show("该账户已经登陆,此窗口会自动关闭"); this.state = CLOSED; this.Close(); } else if (tokens[1] == "SERVERERR") { MessageBox.Show("服务器连接错误,信息并未成功发送"); } } else { //如果从服务器返回的其他消息格式,则直接显示 MessageBox.Show(msg, "客户端收到其他种类的消息"); } } //关闭连接 tcpClient.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "与服务器的连接断开"); this.state = CLOSED; } }