1

I'm writing a simple socket code to write data to using socket_write() and in return retrieve data using socket_read().

This is basic set-up that I have:

  $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die ("Could not create connection");
  $socket_conn = socket_connect($socket, "some_ip", some_digit); // internal network
  $socket_params = pack('I',0) . strval($file['filename'] .'.'. $file['extension']) . pack('I', $file['size']) . pack('I', 0) . strval(File::read($file['saved_to'] . $file['saved_as'], true)) . pack('I', 0);
  $t = socket_write($socket, $socket_params, strlen($socket_params)) or die ("Could not create connection");        
  $status = unpack('I', socket_read($socket, 4));
  socket_close($socket);

After running this, I will get the error ErrorException [ Warning ]: unpack() [function.unpack]: Type I: not enough input, need 4, have 0. I'm not sure why this is happening as $t returns 7423 which is the size I get if I var_dump($socket_params).

However at the server side, it's showing that it has only received 1448 bytes and the request therefore got terminated. The server-side is working fine with the current live PHP implementation so I'm ruling out any funkiness with the server-side for now.

I've uploaded my test-code to production to test (on the exact same server as the current live one) and it's returning the same error.

For what's worth, I'm using FuelPHP 1.0 RC3, and the current live implementation is using CakePHP. Local machine is a OS X 10.6.8(MAMP 1.9.4) and production machine is a 10.04 LTS(Apache/PHP5.3.x).

Lastly, I've been searching and since it's having the same issue on Linux machine, I think it's safe to rule this out too (http://stackoverflow.com/questions/6878603/strange-raw-socket-on-mac-os-x http://sock-raw.org/papers/sock_raw) right? I'm not quite sure how to work around this, if it's the problem?

Thanks in advance, I'm not sure what other information I need to provide as this is my first time doing socket programming. I could use some guidance for debugging so fire away any advice!

vgonisanz
  • 11,831
  • 13
  • 78
  • 130
draco
  • 91
  • 1
  • 4
  • 1
    1448 sounds suspiciously close to the average TCP MTU setting, I suspect that if you pull out Wireshark or some other packet sniffer you will find that all the data is being transmitted, but in two seperate packets. Just a thought.... – DaveRandom Aug 16 '11 at 15:22
  • Also, there's no need to pass the `$length` parameter to socket_write, especially if you're having problems like this – DaveRandom Aug 16 '11 at 15:25
  • I haven't gotten around to checking it's sending in multiple packets yet, but if it is, any workarounds this? – draco Aug 17 '11 at 15:00
  • Not really, as it would be the server side that is ignoring the extra packets... seems unlikely now I put it like that - maybe try playing around with the `SO_SNDBUF` socket option? This is why whenever I do socket level programming I have Wireshark running when I test so I can see exactly what is being exchanged over the network. You should be up on some of the lower level mechanics of TCP/IP before you even attempt socket programming, as well. What *is* the server side exactly? An app you wrote, or something else? – DaveRandom Aug 17 '11 at 15:07
  • I have no control over the server side app except to get in touch with the author. So I really want to know if it's an client side issue as it's working for the current live implementation. Ok I will get to using wireshark and report back. Thanks! – draco Aug 18 '11 at 02:43

0 Answers0