DWilliams Posted January 12, 2010 Share Posted January 12, 2010 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? Quote Link to comment Share on other sites More sharing options...
oni-kun Posted January 12, 2010 Share Posted January 12, 2010 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"; Quote Link to comment Share on other sites More sharing options...
corbin Posted January 12, 2010 Share Posted January 12, 2010 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....."); Quote Link to comment Share on other sites More sharing options...
DWilliams Posted January 17, 2010 Author Share Posted January 17, 2010 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? Quote Link to comment Share on other sites More sharing options...
oni-kun Posted January 18, 2010 Share Posted January 18, 2010 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)); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.