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


-------------------
pwelter34Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pwelter34Author Commented:
Adjusted points to 150
0
mcriderCommented:
What you should be sending is this:

Msg = Chr$(255)+chr$(255)+chr$(255)+chr$(255)+"info"+chr$(0)


Cheers!

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mcriderCommented:
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!
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

pwelter34Author Commented:
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
0
mcriderCommented:
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!

0
mcriderCommented:
Thanks for the points! Glad I could help!


Cheers!
0
mtoshCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.