Deciphering byte stream returned from server

Posted on 2000-05-01
Last Modified: 2008-03-17
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

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?

Question by:mtosh
  • 4
  • 3
  • 2
  • +1
LVL 32

Accepted Solution

bhess1 earned 150 total points
ID: 2766699
The IP address in text format from the byte stream would be something like this (assuming that one server's info is in a byte array 1-16)

Dim SrvrInfo(1 to 16) as Byte
Dim IPAddr as String
Dim Port as Integer

Get_Server_Info   ' Load array with byte stream

IPAddr = SrvrInfo(11) & "." & SrvrInfo(12) & "." & SrvrInfo(13) & "." & SrvrInfo(14)

Port = SrvrInfo(15) * 256 + SrvrInfo(16)

LVL 38

Expert Comment

ID: 2766785
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
    '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

End Sub

LVL 38

Expert Comment

ID: 2766807
sorry should be


Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


Author Comment

ID: 2767989
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!

LVL 38

Expert Comment

ID: 2769706
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.)

Author Comment

ID: 2770562
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!

Expert Comment

ID: 2771920
Community Support has reduced points from 300 to 150

Expert Comment

ID: 2771921
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:

Customer Service

Author Comment

ID: 2773309
Thanks for your help!

Author Comment

ID: 2773373
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.


LVL 32

Expert Comment

ID: 2773975
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!

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question