日期:2014-05-18 浏览次数:20947 次
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);
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 } }
// 编码发送 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 编码