Link to home
Start Free TrialLog in
Avatar of mtosh
mtosh

asked on

Deciphering byte stream returned from server

I writing a VB application that:

(1) queries a game master server to receive a list of
IP addresses and Port numbers of servers running a
particular game.  

(2) query the individual game servers and return information
such as ping, current map, max number of players and actual
number of players on the server.

I have #2 down (assuming I feed it known IPs and Port numbers.)

I'm having trouble with #1

The documentation for this process is pretty thin:

"... server returns the following:
 -1 (a four byte integer, i.e. FF, FF, FF, FF)
 'f' (single ASCII char)
 '\r' (ASCII carriage return)

 Unique_ID (4 byte unique ID number)

 IP address (4 bytes, highest octect first)
 Port number (2 byte integer, big endian format)"


Here is a breakdown of the first 16 bytes that my app.
received from the master server:


Cloumn |Byte      |Ascii/
No.       |Received|Chr$
-----------------------
 1         |ÿ            |255
 2         |ÿ            |255
 3         |ÿ            |255
 4         |ÿ            |255
 5         |f             |102
 6         |vbCr       |13
 7         |p            |112
 8         |t             |116
 9         |           |2
10        |              |0
11        |Ï             |207
12        |¾           |190
13        |           |18
14        |6            |54
15        |i             |105
16        |‡            |135

This is what I know:

Bytes 1 - 4 are four consecutive bytes of '\0' in C/C++ or
chr$(255) in VB - the 32-bit integer -1

Byte 5 is the ascii character 'f'

Byte 6 is the ascii carriage return character '\r' in C/C++ or
chr$(13) in VB

Bytes 7 - 9 are the Unique ID number for each batch of IPs and
Ports from the Master Server (4 bytes)

Bytes 11-14 are the IP address (4 bytes - highest octect first)

Bytes 15&16 are the Port (a 16-bit integer) for the preceding IP
(2 bytes - big endian format: the "big end" of the 2 byte word is sent
first)

What I don't know is how to convert the bytes for IP and Port correctly.

Another quote:
"With Bytes 15 and 16 (the port number in big endian format) the port
number is 27015 (0x69, 0x87 hex). It is sent as chr$(105), then chr$(135),
not as chr$(135) then chr$(105) (like a NORMAL Intel integer)."

QUESTION#1=> How do you translate four consecutive bytes of chr$(255) to -1?

QUESTION#2=> What would the IP address be of chr$(207), chr$(190), chr$(18), chr$(54)?

QUESTION#2=> How do you get 27015 out of chr$(105) and chr$(135)?
I know that hex(105) = 69 and hex(135) = 87. How do you arrive at 27015?

Thanks!
Greg
ASKER CERTIFIED SOLUTION
Avatar of Brendt Hess
Brendt Hess
Flag of United States of America image

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
Here's some sample code to convert bytes to a signed long and to a signed integer.  There is a note on how to convert to an unsigned integer below in a comment.

Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, _
    Source As Any, ByVal Length As Long)

Private Sub Command1_Click()

    Dim b1(1 To 4) As Byte
    Dim b2(1 To 2) As Byte
    Dim lngVal As Long
    Dim intVal As Integer
    Dim i As Integer
   
    For i = 1 To 4
        b1(i) = 255
    Next
   
    'Change the byte order.
    b2(1) = 135
    b2(2) = 105
   
   
   
    CopyMemory lngVal, b1(1), 4
   
    Debug.Print lngVal
   
   CopyMemory intVal, b2(1), 2
   
    Debug.Print intVal
   
    'if the port is an unsigned integer then use
    'cint(105)*256^1+135*256^0

End Sub


sorry should be

'clng(105)*256^1+135*256^0


Avatar of mtosh
mtosh

ASKER

Thanks for the quick responses!

The info that you both gave me has gotten me over the coding hump.

Since I asked 3 specific questions:

#1 - how to convert the 32-bit integer to -1
#2 - how to get the IP address
#3 - how to determine the port #

and bhess1 answered #2 and #3 correctly and PaulHews answered #1 and #3 correctly, I'd like to split the points evenly between you two.

I'd like to award bhess1 with 150 points and PaulHews with 150 points.

Now how do I split the points?


Thanks for your help!

Greg
Could try to reduce points for question to 150 and award to bhess.  Then post a new blank 150 point q, for me.

(Don't know if that will work, but it's a suggestion.)
Avatar of mtosh

ASKER

I posted the question in the support forum.  Hopefully a tech will split the points up between both of you.  I'll keep an eye on it.

Once again - thanks!
Community Support has reduced points from 300 to 150
Reducing points to one half to allow for split.

You can now accept one of the comments in this thread as an answer. To award the other Expert, you can create a new question in this topic area with a title of 'For ExpertName -- 10336916' using that Experts username.

Remember, the Accept Comment as Answer button is in the header of the comment.

For your convenience, you can use this link to create the new question:
https://www.experts-exchange.com/bin/NewQForm?ta=31

darinw
Customer Service
Avatar of mtosh

ASKER

Thanks for your help!
Avatar of mtosh

ASKER

bhess1 - I don't know why the point value says 15 instead of 150.  I created another question for you with a point value of 135 (for the remaining points).

PaulHews - I created a question for you so that you can get your 150 points as well.

Thanks.

mtosh
The new point value for the question is 1/10th of the original value.  That's how much it would 'cost' some one who had a similar question to get the answer.  Go ahead and delete the other one - I already received the full point value.  Thanks!