日期:2014-05-18 浏览次数:20880 次
            发送消息
            information temp = new information();
            temp.command = "02";
            temp.values = _username;
  
            byte[] bt = SerializeObject(temp);
             
            int len = bt.Length;
            byte[] m = System.BitConverter.GetBytes(len);
            _nws.Write(m, 0, 4);
            _nws.Write(bt, 0, bt.Length);
                            接收消息:
                            byte[] buffer = new byte[4096];
                            int offset = 0;
                            int count = 4;
                            int datalen;
                            while (true)
                            {
                                int len = _nws.Read(buffer, offset, count);    //读入4个字节,一个int表示要接受包的长度
                                if (len != count)
                                {
                                    offset += len;
                                    count -= len;
                                    continue;
                                }
                                datalen = BitConverter.ToInt32(buffer, 0);
                                offset = 0;
                                count = 4;
                                break;
                            }
                            int lent = datalen;
                            
                            if (datalen > 0)
                            {
                                int offsett = 0;
                                while (datalen > 0)
                                {
                                    int len = _nws.Read(buffer, offset, datalen);
                                    if (len != datalen)
                                    {
                                        offsett += len;
                                        datalen -= len;
                                        continue;
                                    }
                                    break;
                                }
                            }
                            for (int itm = 0; itm < 50000000; ++itm)     //加了for循环就可以了!
                            { 
                                
                            }
                            //MessageBox.Show(string.Format("接受了{0}", lent));
                            information temp = new information();
                            temp = (information)DeserializeObject(buffer);      //反序列化