0

I need to unpack a response from a server that has a packed 4byte integer, which indicates the length of the response. I don't even know how to trouble shoot, since I have no access to the server. Basically, the leading byte is off somehow. The following gives me a byte of 0 first, then distorted where it is not accurate.

$size_get=fgets($fp, 4);
$size=unpack('N',$size_get);
echo fgets($fp, $size[1]) . PHP_EOL;

ANSWERED PART:

I'm using a socket connection and the provider is asking that I pack the length of the message, into the beginning of the message, as a 4byte integer. They arent familiar with php, so stackoverflow is my go-to. I searched but nothing really comes up.

$message='{login:"username";pass:"password"}';
$length=pack("N",strlen($message));
fwrite($connection, $length.$message);

I'm using stream_socket_client().

user1695981
  • 89
  • 1
  • 11
  • Nothing? Have you found [Converting bytes retrieved over a php socket to a integer](http://stackoverflow.com/questions/14510817/converting-bytes-retrieved-over-a-php-socket-to-a-integer)? Just `pack()` instead of `unpack()`. Explain what part specifically you are having trouble with. – CodeCaster Jan 22 '14 at 13:50
  • I'm sending the message. I edited with a bit more information, but I was afraid it was going to mislead someone, that's why I didn't originally. – user1695981 Jan 22 '14 at 13:55
  • It's usually easier to correct existing code than to make it all up out of some description. What does this code do? – CodeCaster Jan 22 '14 at 14:01
  • I'm not making anything up. The only thing that is not on the code is the username and password. This is a message I need to send to a socket. The socket requirements are that I send a 4-byte long integer that carries the length of the message, in the beginning of the message. – user1695981 Jan 22 '14 at 14:05
  • I meant you're looking for code that solves your problem, and it's easier to answer if you show what you already have, otherwise we'll have to guess at how your current code works from your description. My question is: **what does your current code do**? – CodeCaster Jan 22 '14 at 14:11
  • Apologies, I thought you meant I came up with the code just for stackoverflow. Opens a connection and keeps it open. No reply from server. The server accepts the connection but doesn't receive the correct message (bytes issue), so it does not respond with a reply. – user1695981 Jan 22 '14 at 14:14
  • No problem. You might then want to create a listening socket to which you send your message, so you can inspect what is actually being sent and received. We can't and don't know what server you're talking to, what format it expects or why your message doesn't get accepted. – CodeCaster Jan 22 '14 at 14:16
  • I believe the issue is with the bytevalue, $length not being 4 bytes long. – user1695981 Jan 22 '14 at 14:29
  • You can inspect that by dumping the $length value or, ironically, by inspecting its length. – CodeCaster Jan 22 '14 at 14:35
  • ok, after a lot of fidgeting, I got the command that is sent to be ok. Now, the reply also has the same response and it does not work so well. I can't get the unpack to work. is there anything I am missing. It should also be a byte indicating the length of the response. check op – user1695981 Jan 22 '14 at 18:17

0 Answers0