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
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";

Link to comment
Share on other sites

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.....");

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

It should work, but if you want to replace it with something simpler than you may just have to do this:

$CHAT_MESSAGE = 7;
$CHAT_END_OF_COMMAND = 255;

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.