字符串长度Winsock + UTF8 +“ç”

时间:2015-02-06 19:27:28

标签: c# ruby utf-8 winsock

实际上我在服务器中遇到“接收数据”的问题。

一切正常,这是服务器对收到的数据的处理方式:

    string data = null;
    byte[] buffer = new byte[40000];
    client.TCPClient.GetStream().Read(buffer, 0, 40000);
    data = Encoding.UTF8.GetString(buffer);
    Server.Network.ReceiveData.SelectPacket(client.Index, data);

空隙:

public static void SelectPacket(int Index, string data)
{
    string[] packets = data.Split('\\');
    for (int i = 0; i < packets.Length; i++)
    {
        if (packets[i] == String.Empty) { break; }
        if (packets[i].StartsWith("<0>")) { ReceivedAuth(Index); }
        else if (packets[i].StartsWith("<1>")) { ReceivedDisconnect(Index); }
        else if (packets[i].StartsWith("<3>")) { ReceivedMotd(Index); }
        else if (packets[i].StartsWith("<4>")) { ReceivedLogin(Index, packets[i]); }
        else if (packets[i].StartsWith("<5>")) { ReceivedRegister(Index, packets[i]); }
        else if (packets[i].StartsWith("<6>")) { ReceivedNewChar(Index, packets[i]); }
        else if (packets[i].StartsWith("<7>")) { ReceivedLoadChar(Index, packets[i]); }
        else if (packets[i].StartsWith("<8>")) { ReceivedIngame(Index, packets[i]); }
        else if (packets[i].StartsWith("<10>")) { ReceivedUpdatePlayer(Index); }
        else if (packets[i].StartsWith("<11>")) { ReceivedMove(Index, packets[i]); }
        else if (packets[i].StartsWith("<12>")) { ReceivedMessage(Index, packets[i]); }
        else if (packets[i].StartsWith("<13>")) { ReceivedInvSlots(Index, packets[i]); }
        else if (packets[i].StartsWith("<14>")) { LatencyCheck(Index); }
        else if (packets[i].StartsWith("<15>")) { MapCheck(Index, packets[i]); }
        else if (packets[i].StartsWith("<16>")) { UseItemCheck(Index, packets[i]); }
        else if (packets[i].StartsWith("<17>")) { EquipItemCheck(Index, packets[i]); }
        else if (packets[i].StartsWith("<18>")) { AttackCheck(Index, packets[i]); }
        else if (packets[i].StartsWith("<19>")) { DirCheck(Index, packets[i]); }
        else if (packets[i].StartsWith("<20>")) { PickItemCheck(Index); }
        else if (packets[i].StartsWith("<21>")) { DropItemCheck(Index, packets[i]); }
        else if (packets[i].StartsWith("<22>")) { ItemCheck(Index, packets[i]); }
        else if (packets[i].StartsWith("<23>")) { WeaponCheck(Index, packets[i]); }
        else if (packets[i].StartsWith("<24>")) { ArmorCheck(Index, packets[i]); }
    }
}

服务器接收以下信息:

<num>DATA\

我的问题是,当我向服务器发送像“açai”这样的文字时, 服务器读起来像这样:

<num>açai

当我发送没有ç

的文本时
<num>text\

所以,发送是在Ruby中:

命令

@socket.send("<12>#{msg}\\")

发件人

  def send(data, flags = 0)
    result = Win32API.new('ws2_32', 'send', 'ppll', 'l').call(@descriptor, data, data.size, flags)
    result == -1 ? SocketError.check : result
  end

因此,我无法分割字符串,并且它的长度非常长。

任何人都有提示或解决方案吗?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。

byte[] buffer = new byte[client.TCPClient.Available];
client.TCPClient.GetStream().Read(buffer, 0, client.TCPClient.Available);

谢谢。