| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2066 人关注过本帖
标题:[求助]关于socket编程乱码的问题
只看楼主 加入收藏
mocelot
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-3-29
收藏
 问题点数:0 回复次数:4 
[求助]关于socket编程乱码的问题
我在网上找到一份tcp聊天室代码,能用英文聊天,但是到了中文就会乱码,我是初学者,请问要怎样才能做到中文也可以聊天啊,请各位指教,谢谢

tcpchat.rar (91.15 KB) 代码

搜索更多相关主题的帖子: socket 乱码 中文 tcp 
2008-04-14 16:55
mocelot
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-3-29
收藏
得分:0 
我觉得最主要都是Data这个类的问题?
会的人,提示下我啦,谢谢
2008-04-14 18:00
mocelot
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-3-29
收藏
得分:0 
这是个学习socket的好例子,大家下来看看(我想)
问题解决.把Data类改了点
class Data
    {
        //Default constructor
        public Data()
        {
            this.cmdCommand = Command.Null;
            this.strMessage = null;
            this.strName = null;
        }

        //Converts the bytes into an object of type Data
        public Data(byte[] data)
        {
            //The first four bytes are for the Command
            this.cmdCommand = (Command)BitConverter.ToInt32(data, 0);

            //The next four store the length of the name
            int nameLen = BitConverter.ToInt32(data, 4);

            //The next four store the length of the message
            int msgLen = BitConverter.ToInt32(data, 8);

            //This check makes sure that strName has been passed in the array of bytes
            if (nameLen > 0)
                this.strName = Encoding.BigEndianUnicode.GetString(data, 12, nameLen*2);
            else
                this.strName = null;

            //This checks for a null message field
            if (msgLen > 0)
                this.strMessage = Encoding.BigEndianUnicode.GetString(data, 12 + nameLen*2, msgLen*2);
            else
                this.strMessage = null;
        }

        //Converts the Data structure into an array of bytes
        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.BigEndianUnicode.GetBytes(strName));

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

            return result.ToArray();
        }

        public string strName;      //Name by which the client logs into the room
        public string strMessage;   //Message text
        public Command cmdCommand;  //Command type (login, logout, send message, etcetera)
    }
}
2008-04-14 20:44
水中行
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-4-25
收藏
得分:0 
我也在改这个,Data类你改了哪里呀?
我电脑上就没有BigEndianUnicode,怎么回事啊!
2008-04-25 10:07
水中行
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-4-25
收藏
得分:0 
我知道了,也不是你说的*2,这样会截错字符串,应该要计算出发送含中文字符串的实际字节数,这样就OK了,本身编码是没问题的,一开始我还总以为是编码问题。
2008-04-25 18:19
快速回复:[求助]关于socket编程乱码的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.043252 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved