UDP 汉字传输问题

ya19891117 2010-10-28 10:07:07
客户端发送汉字时候 传给服务端的字符不能获取完整的字符,比如(你好吗)传给服务器 服务器只收到(你)一个字
大家帮忙看看

发送端
Data msgToSend = new Data();

msgToSend.strName = strName;
msgToSend.strMessage = lstChatters.SelectedItem.ToString() + "|" + txtMessage.Text;
msgToSend.cmdCommand = Command.Message;

byte[] byteData = msgToSend.ToByte();

//Send it to the server
clientSocket.BeginSendTo (byteData, 0, byteData.Length, SocketFlags.None, epServer, new AsyncCallback(OnSend), null);


public byte[] ToByte()
{
List<byte> result = new List<byte>();

//First four are for the Command
result.AddRange(BitConverter.GetBytes((int)cmdCommand));

//Add the length of the name
if (strName != null)
result.AddRange(BitConverter.GetBytes(strName.Length));
else
result.AddRange(BitConverter.GetBytes(0));

//Length of the message
if (strMessage != null)
result.AddRange(BitConverter.GetBytes(strMessage.Length));
else
result.AddRange(BitConverter.GetBytes(0));

//Add the name
if (strName != null)
result.AddRange(Encoding.GetEncoding("gb2312").GetBytes(strName));

//And, lastly we add the message text to our array of bytes
if (strMessage != null)
result.AddRange(Encoding.GetEncoding("gb2312").GetBytes(strMessage));

return result.ToArray();
}

服务端
serverSocket.EndReceiveFrom (ar, ref epSender);

//Transform the array of bytes received from the user into an
//intelligent form of object Data
Data msgReceived = new Data(byteData);
string strmessage = msgReceived.strMessage;(strMessage 显示(你?))

大家帮帮忙 谢谢啦
...全文
146 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ya19891117 2010-10-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 qldsrx 的回复:]
问题可能在Data msgReceived = new Data(byteData);
不清楚你Data类是如何处理byteData二进制数据的。
[/Quote]
byte[] byteData = new byte[1024];

请问该怎么修改?
liuzhiyyyy 2010-10-28
  • 打赏
  • 举报
回复
发的时候是 Unicode,收的时候可能被当做 ASCII 了。
ya19891117 2010-10-28
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 cgabriel 的回复:]
发的时候是 Unicode,收的时候可能被当做 ASCII 了。
[/Quote]
那请问怎么修改?
qldsrx 2010-10-28
  • 打赏
  • 举报
回复
问题可能在Data msgReceived = new Data(byteData);
不清楚你Data类是如何处理byteData二进制数据的。
CGabriel 2010-10-28
  • 打赏
  • 举报
回复
发的时候是 Unicode,收的时候可能被当做 ASCII 了。
ya19891117 2010-10-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 maco_wang 的回复:]
应该是被截掉了,调试一下,看看是在哪开始变成“你”字的。
[/Quote]
到服务端被截掉了
叶子 2010-10-28
  • 打赏
  • 举报
回复
应该是被截掉了,调试一下,看看是在哪开始变成“你”字的。
ya19891117 2010-10-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 abcdef1111111 的回复:]
楼主单步执行看看,传给服务器时是不是"你好吗",是不是长度有限制
[/Quote]
不是你好, 是你?
abcdef1111111 2010-10-28
  • 打赏
  • 举报
回复
楼主单步执行看看,传给服务器时是不是"你好吗",是不是长度有限制
ya19891117 2010-10-28
  • 打赏
  • 举报
回复
哎!老爸不是李刚都要靠自己啊!!!

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧