UDP sockets

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to communicate with a Quake 3 server. To get its status, you send
0x255 (x4) + "serverinfo" via a UDP socket to port 27960. It then sends you
back a bunch of text representing the server's status.

I've developed this in PHP before, but in C#, the code blocks at the
UdpSocket.Receive() call and never recovers. No exceptions are thrown, just
the program execution stops.

The code:

<pre>
public static string RetrieveRawServerInfo(string hostname, uint port)
{
byte[] request = StringToByteArray(SERVER_INFO_REQUEST, REQUEST_LEADER);
byte[] buffer;

try
{
Console.Error.WriteLine("Creating remote endpoint");
IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0);

Console.Error.WriteLine("Creating socket");
UdpClient client = new UdpClient(hostname, (int)port);

//Console.Error.WriteLine("Connecting");
//client.Connect(hostname, (int)port);

Console.Error.WriteLine("Sending request \"" +
Encoding.ASCII.GetString(request) + "\" (" + request.Length + " bytes)");
client.Send(request, request.Length);

Console.Error.WriteLine("Receiving");
buffer = client.Receive(ref remote);

Console.Error.WriteLine("Received " + buffer.Length + " bytes");

return Encoding.ASCII.GetString(buffer);
}
catch (SocketException e)
{
throw new ApplicationException("Socket error during request", e);
}
}
</pre>

The resulting output:
<pre>
Creating remote endpoint
Creating socket
Sending request "serverinfo" (14 bytes)
Receiving
</pre>
What am I doing wrong? I realize a multithreaded solution would be best to
detect timeouts, but I want to develop a blocking method for now.
 
Even if you send a malformed command, it should respond with 0x255 (x4) +
"disconnect" Right now I'm not getting any response back.
 
Even if you send a malformed command, it will send a "disconnected" response
back. Right now I'm getting nothing.
 
Back
Top