我创建了一个发送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