Jump to content

Sending bytes over a socket.


DWilliams

Recommended Posts

I haven't ever worked with sockets in PHP before. I'm attempting to write a simple PHP script that follows a protocol to connect to a game's chat server.

 

The protocol dictates that upon establishing a connection, the client sends a connection string as such: "CHAT:<chat name>\n<ip address><port>", and the server responds with "YES:<chat name>". I coded this part fine, and my script connects to the server just fine.

 

The possible commands once connected follow this format: <COMMAND BYTE><data><END OF COMMAND>. The documentation I have lists END OF COMMAND as 255, and for an example command byte, CHAT_TEXT_EVERYBODY is 4.

 

I'm a bit puzzled  as to how to send these command, however. I've tried just sending the bytes as a string, outside a string, converted to binary/hex, etc. How do I send a properly formed command string?

Link to comment
https://forums.phpfreaks.com/topic/188135-sending-bytes-over-a-socket/
Share on other sites

I haven't ever worked with sockets in PHP before. I'm attempting to write a simple PHP script that follows a protocol to connect to a game's chat server.

 

The protocol dictates that upon establishing a connection, the client sends a connection string as such: "CHAT:<chat name>\n<ip address><port>", and the server responds with "YES:<chat name>". I coded this part fine, and my script connects to the server just fine.

 

The possible commands once connected follow this format: <COMMAND BYTE><data><END OF COMMAND>. The documentation I have lists END OF COMMAND as 255, and for an example command byte, CHAT_TEXT_EVERYBODY is 4.

 

I'm a bit puzzled  as to how to send these command, however. I've tried just sending the bytes as a string, outside a string, converted to binary/hex, etc. How do I send a properly formed command string?

 

Escapes are your friend, when you do not want to convert.

$command = "\255\x\x\x\x\x\x\x\x\x\x\x\x\4";

Edit:  Someone else posted, but posted it anyway since it gives another option.

 

 

Strings in PHP are essentially binary safe, so what you can do is something like this:

 

$str = chr(4) . "Hey...  Here's something to everyone!" . chr(255);

 

Another option could be sprintf:

 

$str = sprintf("%c%s%c", 4, "Hey.....", 255);

 

Or even:

 

$str = sprintf("%c%s" . chr(255), 4, "Hey.....");

Alright so using chr() works just fine, although I have another problem. I wanted to assign each command byte to a variable so that I can just use the variable in place of remembering each number.

 

The following does not work:

$CHAT_MESSAGE = 		chr(7);
$CHAT_END_OF_COMMAND = 		chr(255);

socket_write($socket, $CHAT_MESSAGE . $message . $CHAT_END_OF_COMMAND);

 

however, this DOES work:

socket_write($socket, chr(7) . $message . chr(255));

 

How should I deal with this?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.