Link to home
Start Free TrialLog in
Avatar of pwelter34
pwelter34

asked on

UDP Winsock Call to server

I'm trying to write a winsock program that will connect to my game server to get the status of it.  The protocol used is UDP.  I'm using the winsock control to make the socket connection.  I haven't done UDP connections before so I'm not sure if I'm doing this write. Also the documentation says . . .

"servers will answer the following messages:
Messages are sent to the server by sending 4 consecutive bytes of 255 (32-bit integer -1) and then the string command followed by a zero byte to terminate it"

I'm not sure if I did this right as I'm not sure what is meant by 4 consecutive bytes of 255.

any help would be greatly appreciated

Thanks
Paul


code below
------------------------
Private varData As Variant

Private Sub Command1_Click()
    Dim Buffer As Byte
    Dim Term As Byte
    Dim Msg As Variant
   
   
    Buffer = 255
    Term = 0
   
    Msg = Buffer & Buffer & Buffer & Buffer & "info" & Term
   
    With Winsock1
        .LocalPort = 27015
        .RemoteHost = "209.32.152.229"
        .RemotePort = 27015
        .Protocol = sckUDPProtocol
        .Connect
        .SendData Msg
   
    End With
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Winsock1.GetData varData
End Sub
--------------------------------

Server documentations below
--------------------------------

servers will answer the following messages:
Messages are sent to the server by sending 4 consecutive bytes of 255 (32-bit integer -1) and then the string command followed by a zero byte to terminate it

"ping"
      Server responds with a single byte code ASCII 'j'

"info"
      Server responds with the following packet:
      (int32)            -1
      (byte)            ASCII 'C' (info response, S2A_INFO)
      (string)            net address of server
      (string)            name of the host / server
      (string)            name of the map
      (string)            game directory (i.e. valve/)
      (string)            Game description (e.g. "half-life multiplay")
      (byte)            active client count
      (byte)            maximum clients allowed
      (byte)            protocol version (currently 7)

"players"
      Server responds with the following packet:
      (int32)            -1
      (byte)            ASCII 'D' (players response, S2A_PLAYER)
      (byte)            active client count

      for each active client
            (byte)            client number / index
            (string)            player name
            (int32)            client's frag total
            (float32)            client's total time in-game

"rules"
      Server responds with the following packet:
      (int32)            -1
      (byte)            ASCII 'E' (rules response, S2A_RULES)
      (int16)            number of rules

      for each rule
            (string)            rule name
            (string)            rule value


-------------------
Avatar of pwelter34
pwelter34

ASKER

Adjusted points to 150
ASKER CERTIFIED SOLUTION
Avatar of mcrider
mcrider

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also, the .SendData command should be sent in the Winsock1_Connect event, not directly after the .Connect command...

If you want to send the data inline like you have it, after the .Connect, you need to do a wait loop checking the state of the winsock control.  You can only send data when the controls state is sckConnected (7).


For example:


    With Winsock1
        .LocalPort = 27015
        .RemoteHost = "209.32.152.229"
        .RemotePort = 27015
        .Protocol = sckUDPProtocol
        .Connect
        Do
              If .State = sckConnected then
                    Exit Do
              End If
              DoEvents
        Loop
        .SendData Msg
   
    End With


Cheers!
Actually, with UDP, there is no connection.  I've sort of gotten it to work.  Now, when i do the getdata, an array of bytes is return.  how do i convert that to something i can parse? I haven't worked with binary data that much.  also, when do you close the call close?  should i call that right after the getdata?

thanks for your help


code at this point
--------------------------
Private varData As Variant

Private Sub Command1_Click()
    Msg = Chr$(255) + Chr$(255) + Chr$(255) + Chr$(255) + "info" + Chr$(0)

    With Winsock1
        .LocalPort = 27015
        .RemoteHost = "209.32.152.229"
        .RemotePort = 27015
        .Protocol = sckUDPProtocol
        .Bind
        .SendData Msg
    End With
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Winsock1.GetData varData
End Sub
After you get the data you can close the socket.

From reading the documentation you provided, you have the format to parse...

For example, the "info"
Server responds with the following packet:
   (int32) -1
   (byte) ASCII 'C' (info response, S2A_INFO)
   (string) net address of server
   (string) name of the host / server
   (string) name of the map
   (string) game directory (i.e. valve/)
   (string) Game description (e.g. "half-life multiplay")
   (byte) active client count
   (byte) maximum clients allowed
   (byte) protocol version (currently 7)

so in the buffer you read:

   The 1st character is a hex FF which is -1.
   The next character is a "C".
   The next set of characters is a string that contains the net address of the server.
    ... Etc...

With the string, each one should be terminated with a chr$(0).

You can use the InStr function to find each occurrence of the chr$(0).

Once you have the position, you can use the Left$, Mid$, string functions to parse the buffer...


Hope this helps!


Cheers!

Thanks for the points! Glad I could help!


Cheers!
pwelter34 - just curious to see how you made out.

I'm trying to do the same thing and a little confused with the returned data.

If I use the code as mentioned above to ping a server I get: "??j" with varData as varient.

If varData is a string, I get "ÿÿÿÿj"

Any ideas?

Greg