做了个类似于QQ的: 做了个类information然后用到了序列化,之前序列化没办法确定要读到什么地方停止。。我弄了个读入
<EOF>来判别
问题是这样,偶尔服务端会把客户端的两条消息同时接收过来,两条消息如果碰到一起接收了...这样我这个界面也没办法显示出来了。客户端发送请求"02":   //显示个人信息
 information temp = new information();
            temp.command = "02";
            temp.values = _username;           string str = SerializeObject(temp);           _nws.Write(Encoding.Unicode.GetBytes(str), 0, Encoding.Unicode.GetBytes(str).Length);
           _nws.Write(Encoding.Unicode.GetBytes("<EOF>"), 0, Encoding.Unicode.GetBytes("<EOF>").Length);客户端发送请求"03":   //显示好友列表
 information temp = new information();
            temp.command = "03";
            temp.values = _username;           string str = SerializeObject(temp);           _nws.Write(Encoding.Unicode.GetBytes(str), 0, Encoding.Unicode.GetBytes(str).Length);
           _nws.Write(Encoding.Unicode.GetBytes("<EOF>"), 0, Encoding.Unicode.GetBytes("<EOF>").Length);
在服务端做接受客服端发来的请求,做出相应的反馈
客户端接收部分:while (true)
            {                    NetworkStream stream = new NetworkStream(ClientSocket); 
                    if (stream.CanRead)             //如果可以读
                    {
                        if (stream.DataAvailable)       //如果有数据发送过来
                        {
                            string message; //最后要转换的
                            string data = null;
                            while (true)
                            {
                                byte[] buffer = new byte[2000];
                                int bytesRec = stream.Read(buffer, 0, buffer.Length);
                                //int bytesRec = handler.Receive(bytes);
                                data += Encoding.Unicode.GetString(buffer, 0, bytesRec);
                                if (data.IndexOf("<EOF>") > -1)//这个代表接受到指定的消息,也是自己定义即可
                                {
                                    message = data.Substring(0,data.Length-5);
                                    break;
                                }
                            }
                            Console.WriteLine(message);
                            information temp = new information();
                            temp = (information)DeserializeObject(message);
                            
                            
                            
                            switch (temp.command)
                            {
                                case "00":  //接受到消息
                                    {
                                        
                                    }
                                    break;
                                case "01":          //离线
                                    {
                                        
                                    }
                                    break;
                                case "02":          //发送个人信息列表
                                    {                                        //Console.WriteLine(string.Format("select * from UserInformation where UserName = '{0}'", (string)temp.values));
                                        string cmp = (string)temp.values;
                                        mySQL sql = new mySQL();
                                        DataSet dt = sql.Query(string.Format("select UserName,NickName,FaceId from UserInformation where UserName = '{0}'", cmp));    //执行sql语句
                                        //已经彻底的查出了账号信息在表里面是怎么样了...现在把数据集和命令02发给客户端, 让他接受
                                        //Console.WriteLine(string.Format("资料是: {0}, {1}, {2}", dt.Tables[0].Rows[0][0].ToString(), dt.Tables[0].Rows[0][1].ToString(), dt.Tables[0].Rows[0][2].ToString()));
                                        //下面开始做一个类对象,然后把它弄成byte[]的形式,然后发送出去                                        information com = new information();
                                        com.command = "02";
                                        com.values = dt;    //把记录集对象赋给这个object对象,到时候强制类型转换
                                        string strcom = SerializeObject(com);
                                        stream.Write(Encoding.Unicode.GetBytes(strcom), 0, Encoding.Unicode.GetBytes(strcom).Length);   //发送完毕
                                        stream.Write(Encoding.Unicode.GetBytes("<EOF>"), 0, Encoding.Unicode.GetBytes("<EOF>").Length);                                        Console.WriteLine(string.Format("服务器发出{0}个字节",strcom.Length));                                    }
                                    break;
                                case "03":          //发送好友列表
                                    {
                                        string sqlcommand = "select NickName, FaceId, FriendId from Friends join UserInformation on HostId = '" + (string)temp.values + "' and style = 1  and FriendId = UserInformation.UserName";
                                        mySQL sql = new mySQL();
                                        DataSet dt = sql.Query(sqlcommand);                                        information com = new information();
                                        com.command = "03";
                                        com.values = dt;    //把记录集对象赋给这个object对象,到时候强制类型转换
                                        string strcom = SerializeObject(com);
                                        stream.Write(Encoding.Unicode.GetBytes(strcom), 0, Encoding.Unicode.GetBytes(strcom).Length);   //发送完毕
                                        stream.Write(Encoding.Unicode.GetBytes("<EOF>"), 0, Encoding.Unicode.GetBytes("<EOF>").Length);                                        Console.WriteLine(string.Format("服务器显示好友列表发出{0}个字节", strcom.Length));
                                    }
                                    break;
                                case "04":
                                    { 
                                        
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }                    }
              
            }
        }