所以我有这个php代码,需要移植到C#
$socket = stream_socket_client('udp://'.$server , $errno, $errstr, 1);
fwrite($socket, "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x67\x69\x65\x33\x05");
$response = fread($socket, 2048);
我试图在C#中做同样的事情
private void start_Click(object sender, EventArgs e)
{
var client = new UdpClient();
IPEndPoint ep = new
IPEndPoint(IPAddress.Parse("censored"),censored);
client.Connect(ep);
Byte[] sendBytes = Encoding.ASCII.GetBytes("\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x67\x69\x65\x33\x05");
client.Send(sendBytes, sendBytes.Length);
var received = client.Receive(ref ep);
result.Text = received.ToString();
}
但是它只是冻结而没有响应(超时)
答案 0 :(得分:0)
Byte[] sendBytes = Encoding.ASCII.GetBytes("\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x67\x69\x65\x33\x05");
此行将不会产生您期望的字节数组。至于原因,请在此处说明:ASCIIEncoding.ASCII.GetBytes() Returning Unexpected Value
尝试以下方法:
var sendBytes = new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x67, 0x69, 0x65, 0x33, 0x05 }