日期:2014-05-18  浏览次数:20930 次

【求助】客户端发送数据 服务端如何提取
客户端 发送的数据
C# code
 netstream.Write(new byte[] { 0, 4 }, 0, 2);
                    netstream.Write(Encoding.Unicode.GetBytes(user_tb.Text), 0, Encoding.Unicode.GetBytes(user_tb.Text).Length);
                    netstream. Write(Encoding.Unicode.GetBytes(mima_t.Text), 0, Encoding.Unicode.GetBytes(mima_t.Text).Length);


服务端如何提取?分别提取就可以了
我写的是这样,但是不对
C# code
 
        private string DecodingBytes(byte[] s)
        {
            return string.Concat(s[0].ToString(), s[1].ToString());

//接受第一个数据包。
                    byte[] _cmdBuff = new byte[128];
                    clientSkt.Receive(_cmdBuff);
                    string _cmd = DecodingBytes(_cmdBuff);
  switch (_cmd)
                    {
case "04":
                            {
                               
                                    
                                  
                               
                                string nam = Encoding.Unicode.GetString(_cmdBuff).TrimEnd('\0');//想提取客户端发来的user_tb.Text

 string _mima = Encoding.Unicode.GetString(_cmdBuff).TrimEnd('\0');//想提取mima_t.Text  
}
}








后面的提取有些问题,正确的格式应该是什么?

------解决方案--------------------
C# code
// 编码发送
netstream.Write(new byte[] { 0, 4 }, 0, 2);
byte[] usrarr = Encoding.UTF8.GetBytes(user_tb.Text);
netstream.Write(usrarr, 0, usrarr.Length);
netstream.Write(new byte[] { 0, 1 }, 0, 1);
byte[] mimaarr = Encoding.UTF8.GetBytes(mima_t.Text);
netstream.Write(mimaarr, 0, mimaarr.Length);

// 接收解码
byte[] _cmdBuff = new byte[128];
clientSkt.Receive(_cmdBuff);

string _cmd = Encoding.UTF8.GetString(_cmdBuff, 0, 2);
switch (_cmd)
{
    case "04":
        {
            string[] arr = Encoding.UTF8.GetString(_cmdBuff, 2, _cmdBuff.Length - 2).TrimEnd('\0').Split('\0');
            string user = arr[0];//想提取客户端发来的user_tb.Text
            string mima = arr[1];//想提取mima_t.Text 

            break;
        }
    default:
        break;
}

Unicode 不好处理,用 UTF8 编码