2012-10-12 33 views
-2

我一直在想如何能遊戲產生這樣的分組的分組:如何創建C#

22 00 11 00 6D 79 75 73 65 72 6E 61 6D 65 00 00 00 00 00 00 6D 79 70 61 73 73 77 6F 72 64 00 00 00 00 00 00 

LENGTH-HEADER-USERNAME-PASSWORD 

在遊戲代碼什麼應該是它們的功能還是他們如何寫類似的東西?它只是Encoding.ASCII.GetBytes("Some String Values")?雖然我懷疑它是這樣寫的。

每當我試圖問某人,他認爲我想分析數據包。我不知道 - 我想知道我需要做什麼才能在C#中創建類似上面的數據包。

+0

考慮到你對先前類似問題中的預覽鏈接評論的反應如何,我不願意發表這個,但可能[this](http://stackoverflow.com/questions/10043621/create-a-network-在數據包中快速發送並通過網絡發送)可以提供幫助? – psubsee2003

+0

遊戲或網絡包需要更多解釋 – Yohannes

+0

這是一個從遊戲中嗅探出來的數據包。 –

回答

0

使用字符串生成器當然是很遠的數據包結構,您必須使用byte []並通過索引將值附加到它。

+0

感謝您的重播我注意到,從一段時間,但您的意見表示讚賞。 –

4

您放置的示例代碼應將字符串轉換爲字節數組。根據您使用的編碼(例如,ASCII,Unicode等),您可能會從同一個字符串中獲取不同的字節數組。

術語數據包通常用於通過網絡發送數據;但數據包本身只是字節數組。

你得到的信息讀取myUsername,myPassword。下面的C#代碼將爲您翻譯。

 byte[] packet = new byte[] { 0x22, 0x00, 0x11, 0x00, 0x6D, 0x79, 0x75, 0x73, 0x65, 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6D, 0x79, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 
     string test = Encoding.ASCII.GetString(packet); 
     Console.WriteLine(test); 
     Console.ReadKey(); 

所以創建類似的東西我想嘗試:

const int HeaderLength = 2; 
    const int UsernameMaxLength = 16; 
    const int PasswordMaxLength = 16; 
    public static byte[] CreatePacket(int header, string username, string password)//I assume the header's some kind of record ID? 
    { 
     int messageLength = UsernameMaxLength + PasswordMaxLength + HeaderLength; 
     StringBuilder sb = new StringBuilder(messageLength+ 2); 
     sb.Append((char)messageLength); 
     sb.Append(char.MinValue); 
     sb.Append((char)header); 
     sb.Append(char.MinValue); 
     sb.Append(username.PadRight(UsernameMaxLength, char.MinValue)); 
     sb.Append(password.PadRight(PasswordMaxLength, char.MinValue)); 
     return Encoding.ASCII.GetBytes(sb.ToString()); 
    } 

然後調用此代碼:

byte[] myTest = CreatePacket(17, "myusername", "mypassword"); 
+0

好吧,我明白了,但我想知道如何創建一個數據包?我的意思是如何爲服務器編寫數據消息? –

+0

我不想翻譯的人我想知道這樣的東西是如何從(包)開始創建的?!!假設我想發送一個具有該結構的數據包:大小,標題,用戶名,密碼。我該怎麼辦 ? –

+0

看看這裏的例子,他們可能會介紹你之後:http://www.csharp-examples.net/socket-send-receive/ – JohnLBevan