1

所以我有这个我需要移植到 C# 的 php 代码

 $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();
    }

但它只是冻结并且没有响应(超时)

4

1 回答 1

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 }
于 2018-10-24T20:28:27.347 回答