2015-02-06 84 views
1

其实我在服务器中有“接收数据”的问题。字符串长度Winsock + UTF8 +“ç”

一切正常,这里就是该服务器接收到的数据所做的:

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\ 

我问题是,当我发送像“aai”这样的文本给服务器时, 服务器读取像这样:

<num>açai 

当我没有使用C发送文本

<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 

因此,我不能拆分字符串,并且它的长度非常长。

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

+0

给出发送数据到服务器的代码,现在只有一半的图片 – Fredou 2015-02-06 19:52:54

+0

问题编辑。 – user3571412 2015-02-06 19:56:52

+0

如果您在客户端输出data.size,它是否提供适当的大小? – Fredou 2015-02-06 20:01:50

回答

0

我解决了这个问题。

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

谢谢。