1

So i have this php code which i need to port to 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);

And im trying to do the same thing in 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();
    }

But it just freezes and gets no response (timeout)

Pyrex
  • 15
  • 6

1 Answers1

0
Byte[] sendBytes = Encoding.ASCII.GetBytes("\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x67\x69\x65\x33\x05");

This line will not produce the byte array that you would expect. As for why, it is explained here: ASCIIEncoding.ASCII.GetBytes() Returning Unexpected Value

Try this instead:

var sendBytes = new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x67, 0x69, 0x65, 0x33, 0x05 }
Xiaoy312
  • 14,292
  • 1
  • 32
  • 44