English 中文(简体)
Connecting to a Source game server in VB.NET
原标题:

I m developing an application that detects Source-based games running on the LAN. Following up on the specifications provided by Valve, I have it narrowed down to exactly what I want: making a UDP connection on port 27015, sending an A2S_INFO query (0xFFFFFFFF followed by "TSource Engine Query") and parsing the binary reply.

This is the code I m working with:

Dim sIP As String = "192.168.0.154"
Dim nPort As Integer = 27015

Dim connectionMessage As String = "ÿÿÿÿTSource Engine Query" & Chr(0)

Dim endPoint As New IPEndPoint(IPAddress.Parse(sIP), nPort)

Dim client As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 6000)
client.SendTo(Encoding.ASCII.GetBytes(connectionMessage), endPoint)
Console.WriteLine("Message sent to " & sIP & ":" & nPort & vbCrLf & connectionMessage)
Dim sBuffer(1400) As Byte
Try
    client.ReceiveFrom(sBuffer, endPoint)
    MsgBox(System.Text.Encoding.Unicode.GetString(sBuffer))
Catch ex As SocketException
    MsgBox("Failed to receive response on " & sIP & ":" & nPort & ":" & vbCrLf & ex.Message)
End Try

I keep getting SocketError.TimedOut exceptions, informing me:

Failed to receive response on 192.168.0.154:27015: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

I m fairly certain this is very close to the answer, because the following very simple PHP version works like a charm:

$ip = "192.168.0.154";
$port = 27015;

$fp = fsockopen("udp://".$ip,$port, $errno, $errstr);
socket_set_timeout($fp, 6);

$prefix = "xffxffxffxff";
$command = "TSource Engine Query";
$msg = "$prefix$command";
fputs($fp, $msg, strlen($msg));

$response = "";
do {
    $response .= fgets($fp, 16);
    $status = socket_get_status($fp);
} while ($status[ unread_bytes ]);
fclose ($fp);

echo $response;

This provides me with a reply in accordance with the specifications.

I m close, but something s amiss with my VB.NET code. What am I doing wrong?

The solution

The encoding type is not valid for transmitting binary data. I replaced the following:

Encoding.ASCII.GetBytes(connectionMessage)

with:

System.Text.Encoding.[Default].GetBytes(connectionMessage)

And it was instantly solved.

A more robust solution, suggested by "pub", might be to specify the code page (although I haven t tested this):

Encoding.GetEncoding("iso-8859-1").GetBytes(connectionMessage)
最佳回答

Did you take a look at this Multi-Threaded Game Server Browser in VB.Net article at CodeProject? If you don t want to re-use the code that s already been written, I m sure you can check how they did it to see if you can find your problem.

Hope that helps, peace.

问题回答

For those couple guys around the world who doesn t have iso-8859-1 code page set as default on computer, use:

Encoding.GetEncoding("iso-8859-1").GetBytes(connectionMessage)





相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签