Page 1 of 1

Packets between PHP and C++

Posted: Thu Jul 27, 2006 10:25 pm
by kaoskorruption
When a packet is created in C++, it looks like this:

Code: Select all

STARTPACKET(pak,0x782,0x09);  
SETBYTE( pak, 0x00, 0x23);  
SETBYTE( pak, 0x01,0x00);  
SETBYTE( pak, 0x03, 0x53);  
SETWORD( pak, 0x04, 0x0200);
When a packet is created in PHP, it looks like this:

Code: Select all

socket_write ( $sock, "test", strlen ( "test" ) );
Of course, nomatter what language it is that you are sending a packet in, it is still just a string of data. And this string of data can be viewed in many different ways such as hex or ASCII.

What I don't understand is how to handle packets that have been created using STARTPACKET in C++ with PHP's socket_read and socket_write functions. Socket_read() and socket_write() deal with a string. STARTPACKET is very complicated. I've been told that STARTPACKET uses some header to identify what packet it is. Is there a way to identify packets using headers in PHP? I've seen some functions such as pack and unpack that look useful, but I have NO idea how to use them to do this.

I just need to communicate with a program written in C++ using winsock with PHP.

Posted: Fri Jul 28, 2006 12:10 am
by alex.barylski
I've never used those C++ functions before, but from convention I can say with confidence they aren't functions but just low level macros...

Secondly, search Google for C++ Sockets and use an existing third party library...

Make programming a sockets applicaiton much easier...

http://www.google.ca/search?hl=en&q=C%2 ... kets&meta=

There are tons, pick one well supported and it will make your development time much easier, as the API will be similiar to PHP socket_*

Cheers :)

Posted: Fri Jul 28, 2006 12:42 am
by kaoskorruption
Actually I'm not making the client that is using C++ with winsock. I'm only making the server, which is in PHP. My friend is making the client. So I won't be able to change the library, but I can suggest he use libraries other than winsock.

Thanks!

Lemme see if this question is more answerable:

When coding packets in most languages, you would end up with something like this:

Code: Select all

0x123 02 12 35 32
But in PHP, using the socket_write function, you end up with some ASCII values. I understand that the "02 12 35 32" part of the above string are hex values. Which I can convert to ASCII. What I don't understand is what the 0x123 part is. I've been told that is some sort of "header" that identifies the packet. How do I read and write these headers in php?

Posted: Fri Jul 28, 2006 1:18 am
by alex.barylski
So I won't be able to change the library, but I can suggest he use libraries other than winsock
Winsock is only a library you would use if developing the client under Windows...

BSD sockets is another choice...

Anyways, winsock/BSD sockets is the library...but it's a low level API...

PHP doesn't offer low level API's, well it could, but AFAIK it doesn't...

It uses an high level wrapper of low level routines, to make the task of working with sockets easier...

Thsoe classes I showed you, may use winsock or BSD sockets...I have no idea, you will need to research that yourself and figure that out...

Those classes though are simply OOP wrappers around a C sockets API...make your job easier...more closely resembling that of PHP instead of assembler, with all those macros, and hex codes... :D

In anycase...
I've been told that is some sort of "header" that identifies the packet. How do I read and write these headers in php?
It's not a header, perse...it's a convention...in C/C++ programming circles it's syntax represents that of a hex value

0x = HEX

I am not sure how you got that information...but it's confusing me...

When you read data, using SOCKET macros, I assume your getting data back as such...

But they way you represent it is usally consistent...

Code: Select all

0x123 02 12 35 32
Should be

Code: Select all

0x123 0x02 0x12 0x35 0x32
Or

Code: Select all

123 02 12 35 32
Both could be hex and the latter could also be HEX or DECIMAL...

The problems you are facing are being caused because your working with SOCKET at a low level, so I suggest you heed my advice and look into a high level wrapper, which should read data and return it much like PHP socket_* do...

Posted: Fri Jul 28, 2006 2:10 am
by kaoskorruption
Ok thanks very much. You've answered a question that I couldn't figure out for days.

So just one last check to make sure I understand correctly.

PHP uses a high level wrapper to make sockets easier, while C++ uses a lower level API that requires you to mess with the binary/hex crap with the macros etc?

And the solution is to simply use a higher level C++ wrapper other than winsock to do sockets?

If this is so, how would you tell packets apart?

For example, you have a server that echos data in blue if packet A is received, and echos data in red if packet B is received.
In the past when working with PHP, I've just defined a packet using the first byte or two. For example:
$data = "A<data>"; or $data = B<data>;

The script would then split the string into two smaller strings, possibly using substring. The first new string would be "A", and the second new string would be <data>. Since the first string was A, the script would know to echo the data in blue.

So what I'm asking is, is my method the best way to tell packets apart when using higher level wrappers such as PHP?