如何服务器发送和接收Unicode(UTF8)? [英] how to server send and receive Unicode (UTF8)?

查看:103
本文介绍了如何服务器发送和接收Unicode(UTF8)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用套接字和C#2005在聊天室中,它成功了.10个客户端可以同时连接.我尝试了 Encoding.UTF8 发送和接收Unicode.我无法发送和接收越南文.我发送越南文,例如:chào.我得到cho.发生了什么事?
那是服务器代码.但是客户端代码发送相同的服务器

I''m doing a chat room with the socket and C # 2005.It succeeded.10 clients can connect at the same time.I tried Encoding.UTF8 to send and receive Unicode.But I can not send and receive Vietnamese.I send Vietnamese eg:chào.I get cho.What happend?
that is the server code.But the client code to send the same server

private void btnConnect_Click(object sender, EventArgs e)
{
            try
            {
                mainSoc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint ip = new IPEndPoint(IPAddress.Any, int.Parse(txtPort.Text));
                mainSoc.Bind(ip);
                mainSoc.Listen(4);
                mainSoc.BeginAccept(new AsyncCallback(OnClient), null);
                UpdateControl(true);
            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }
}





void OnClient(IAsyncResult asyn)
        {
            try
            {
                SocClient[count] = mainSoc.EndAccept(asyn);
                Wait(SocClient[count]);
                ++count;
                mainSoc.BeginAccept(new AsyncCallback(OnClient), null);
            }
            catch (ObjectDisposedException)
            {
                System.Diagnostics.Debugger.Log(0, "1", "lỗi");
            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }
        }



void Wait(Socket soc)
       {
           try
           {
               if (asynCB == null)
               {
                   asynCB = new AsyncCallback(OnRecv);
               }
               //SocPack class contains public Socket CurSoc
               //and public byte[] data=new byte[3];
               SockPack SocP = new SockPack();
               SocP.CurSoc = soc;
               soc.BeginReceive(SocP.data, 0, SocP.data.Length, SocketFlags.None, asynCB, SocP);
           }
           catch (SocketException se)
           {
               MessageBox.Show(se.Message);
           }
      }


接收:


receive:

void OnRecv(IAsyncResult asyn)
{
    try
    {
        SockPack scop = (SockPack)asyn.AsyncState;
        int r = scop.CurSoc.EndReceive(asyn);
        char[] ch = new char[r + 1];
        System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
        int chLen = d.GetChars(scop.data, 0, r, ch, 0);
        string s = new string(ch);
        txtRecv.AppendText(s);
        SendAllClient(scop.data);
        Wait(scop.CurSoc);
    }
    catch (ObjectDisposedException)
    {
        System.Diagnostics.Debugger.Log(0, "1", "lỗi");
    }
    catch (SocketException se)
    {
        MessageBox.Show(se.Message);
    }
}



//send data to client
        void SendClient(byte[] by)
        {
            for (int i = 0; i < count; i++)
            {
                if (SocClient[i] != null)
                {
                    if (SocClient[i].Connected)
                    {
                        SocClient[i].Send(by);
                    }
                }
            }
        }

推荐答案

UTF8与unicode不同. UTF8仅允许将8位用于字符.前127个与ASCII相同,后126个根据所使用的代码页而不同.我建议您摆脱GetDecoder行,而只需使用
UTF8 Isn''t the same as unicode. UTF8 only allows for 8 bits to be used for characters. The first 127 are the same as ASCII the last 126 are Different depending on the codepage used. I would recomend you get rid of the GetDecoder line and just use
Encoding.Unicode.GetBytes


这篇关于如何服务器发送和接收Unicode(UTF8)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆