我創建了一個發送get server list命令的客戶端,但收到的字節不可讀。c#udp客戶端receivebtye無法讀取
這裏是我的代碼:
byte[] receiveBytes = udp.Receive(ref RemoteIpEndPoint);
string[] returnData = Encoding.Default.GetString(udp.Receive(ref RemoteIpEndPoint)).Split('\\');
textBox1.Lines = returnData;
在當地人,我看到寫值
但是程序告訴我這個
有人可以告訴我,什麼是錯的我的代碼?
好吧,我改變我的代碼
yte[] receiveBytes = udp.Receive(ref RemoteIpEndPoint);
int size = receiveBytes.Length;
int i = 0;
while (i <= size-5)
{
string ip = receiveBytes[i] + "." + receiveBytes[i + 1] + "." + receiveBytes[i + 2] + "." + receiveBytes[i + 3] ;
int port = receiveBytes[i + 4] * 256 + receiveBytes[i + 5];
textBox1.Text += ip + ":" + port.ToString() + Environment.NewLine;
i = i + 6;
}
但收到的數據右心不是!
我在php上找到smiler代碼及其工作。
$data = explode("\\", $data);
for($i=0, $o=0; $i<count($data); $i++) {
if (strlen($data[$i])>=4) { //fix
// First 4 bytes are the ip:
$list_server[$o]['ip']=ord($data[$i][0]).".".ord($data[$i][1]).".".ord($data[$i][2]).".".ord($data[$i][3]);
// Last 2 bytes are the port, Takes penultimate number and multiply by 256 and sum with the last number:
$list_server[$o]['port']=(ord($data[$i][4])*256) + ord($data[$i][5]);
//GetName($list_server[$o]['ip'],$list_server[$o]['port']);
$o++;
}
}
我不能猜測我的代碼有什麼問題。
你使用哪種協議? Quake3的?你想要做什麼? – Eun 2013-03-18 15:10:11
yeap是Quale3,我想要創建服務器列表和狀態 – madman 2013-03-18 15:27:11
即使英語不是您的母語,您至少可以嘗試編寫適當的句子。 – jgauffin 2013-03-18 15:31:14