Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    254
    Rep Power
    13

    Question query string decoding?


    Does anyone know how to decode a HL server query string?
    Here is a query code

    Code:
    Private Sub send_Click()
        winsock.RemoteHost = ip 'server ip
        winsock.RemotePort = CInt(Port) 'server port
        winsock.SendData Char(255) & Char(255) & Char(255) & Char(255) & "players"
    End Sub
    
    
    Private Sub winsock_DataArrival(ByVal bytesTotal As Long)
         Dim Data As String
         winsock.GetData Data, , bytesTotal 'returned packet
    End Sub
    It returns some crazy string that needs to be decoded or something?
    Thank you for any help.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Don't think anyone can help you unless you tell us what the encoding method from the server is first (or provide us a sample string possibly)
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    254
    Rep Power
    13
    the return data is inthe following format

    (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

    sample data returned , I recieved the data as string:
    D
    Cygnus )BLee KUM Kee  Cwildchild )BMeMp|-|IS i2AiNeS  6_DMr.Deagle
    YE[needclan] CsMaster  tD
    Most of the time asc() works to turn the strings into numbers but negative numbers dont work with asc()?
    Thank you for any help.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    Dublin
    Posts
    413
    Rep Power
    14
    have you tried defining the data variable as a user defined type / structure in the same format as the data?
    or as a byte array while you debug...
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    254
    Rep Power
    13
    byte array is okay, but is it correct that say -1 ( negative one)
    is 255 as a byte because thats what i get, and so on -2 = 254, -3 = 243 is that right?
    Thank you for any help.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    Dublin
    Posts
    413
    Rep Power
    14
    i would have thought that a 32 bit int would be spread over 4 bytes - and for -1 you'll get four 255's in each of the bytes ... but you're right that the negatives are 255 = 2^8-1 = -1, 254 = -2 etc. for 8 bit. 65535 = 2^16-1 = -1, 65534 = -2 for 16bit and so on, if you follow

    if you read the first value into a long it should just come out as -1
    ie (i guess)
    Code:
    Private Sub winsock_DataArrival(ByVal bytesTotal As Long)
         Dim Data As String, MinusOne as Long
         winsock.GetData MinusOne, , 4
         winsock.GetData Data, , bytesTotal-4
    End Sub
    What do you think?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    Dublin
    Posts
    413
    Rep Power
    14
    i think the tidiest way might be to read into user defined structures like this (from the top of my head)
    Code:
    Private Type Header
      MinusOne as Long
      Response as Byte
      PlayerCount as Byte
    End Type
    
    Private Type Player
      Index as Byte
      Name as String
      FragTotal as Long
      TimeTotal as Double
    End Type
    
    Private Sub winsock_DataArrival(ByVal bytesTotal As Long)
      Dim Header as Header, Player as Player, i as Byte
      call winsock.GetData(Header, , Len(Header))
      For i=1 to Header.PlayerCount
        call winsock.GetData(Player, , Len(Player))
      Next i
    End Sub
    Does that make sense? Like I say, it's off the top of my head so I don't necessarily expect to to work straight off - just to illustrate what i mean
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    254
    Rep Power
    13
    it sounds good
    I get error " only user-defined types defined in public objects modules can be coerced to or from a variant or passed to late-bound functions" and it points to "Call Winsock.GetData(Header, , Len(Header))" and hilights Header
    Thank you for any help.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    Dublin
    Posts
    413
    Rep Power
    14
    Try playing around a bit with where you declare the type (at the top of which module) and also try changing Private Type to Public Type etc. and see what the compiler accepts
  18. #10
  19. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    254
    Rep Power
    13
    ya, i tried placing it everywhere and changed to public and stuff, still gives me that error....
    Thank you for any help.
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    254
    Rep Power
    13
    I have problem with getting data from a winsock packet....
    in the udp it returns int32 along with strings and float32
    so the first int32

    i go

    dim num as long
    winsock.getdata num,vblong,4

    But i get an error saying that the "datagram is too large to fit into the buffer and is trucated", what does that mean? isnt Long 4 bytes?

    Also if I just do the bytes array like

    dim bytes() as byte
    winsock.getdata bytes

    the float32 is spread in 4 slots, like bytes(1), bytes(2), bytes(3), and bytes(4), so how can they be turn it into a single number?
    Thank you for any help.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    Dublin
    Posts
    413
    Rep Power
    14
    yes, a long is 4 bytes (you can use LenB to check)
    I think you can possibly load the float the same way (into a single?) ... otherwise you could experiment with some different numbers and see what you get in your byte array for different float values.
  24. #13
  25. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    254
    Rep Power
    13
    like turn those 4 bytes into one number, I tried

    clng(bytes(1)&bytes(2)&bytes(3)&bytes(4))

    but it doesnt work, it gives over flow error.
    Thank you for any help.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    Dublin
    Posts
    413
    Rep Power
    14
    no, i mean find out (search the net, i guess) how vb stores floats and reconstruct the relevant value from the byte array. what happens if you read directly into a single?
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    Dublin
    Posts
    413
    Rep Power
    14
    some links...

    an explanation of converting from single number to underlying bytes
    http://www.cis.udel.edu/~sullivan/cs...ternalRep.html

    a microsoft description:
    http://msdn.microsoft.com/library/de...m/decla_36.asp

    and a description of the byte representation:
    http://msdn.microsoft.com/library/de..._Languages.asp
    (I think there's an error in that the sign bit is missing from the real * 4 definition and the mantissa has nine bits ... i guess that the first mantissa bit should, in fact, be the sign bit

    here's a conversion function from the top of my head:
    Code:
    Option Base 0
    
    Public Function bytesToSingle(ByRef bytes() As Byte) As Single
    Dim sign As Integer, exponent As Byte, mantissa As Long
      Let sign = IIf(bytes(0) \ 128, -1, 1)
      Let exponent = (2 * (bytes(0) And 127) + bytes(1) \ 128)
      Let mantissa = (bytes(1) Or 128) * 65536& _
                    + bytes(2) * 256& _
                    + bytes(3)
      Let bytesToSingle = sign * mantissa * 2 ^ (exponent - 150)
    End Function
    Last edited by epl; March 21st, 2003 at 04:13 AM.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo