2015-02-06 132 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); 

謝謝。