socket_get_status() - PHP

This is a discussion on socket_get_status() - PHP ; Hi, Exactly what does the unread_bytes parameter of the return of socket_get_status() mean? I thought it would be the amount of data waiting to be gotten via fgets() or fread(), but not according to my lil' test (interactive mode): <?php ...

+ Reply to Thread
Results 1 to 2 of 2

socket_get_status()

  1. Default socket_get_status()

    Hi,

    Exactly what does the unread_bytes parameter of the return of
    socket_get_status() mean? I thought it would be the amount of data waiting
    to be gotten via fgets() or fread(), but not according to my lil' test
    (interactive mode):

    <?php
    $fp = fsockopen('10.1.1.2', 25);
    var_dump(socket_get_status($fp));
    array(4) {
    ["timed_out"]=>
    bool(false)
    ["blocked"]=>
    bool(true)
    ["eof"]=>
    bool(false)
    ["unread_bytes"]=>
    int(0)
    }
    echo fgets($fp, 512);
    220 heyes-computing.net Ready for action (Mailtraq 1.1.5.1167/SMTP)

    --
    Richard Heyes
    "I know not with what weapons World War III will be fought, but World War IV
    will be fought with sticks and stones." - Albert Einstein



  2. Default Re: [PHP] socket_get_status()

    Hi,

    Well I'm not totally sure, why socket_get_status would
    not give you the correct value, other than a bug, as
    the manual does indicate the socket functions are
    experimental.

    There are altenative ways to get the number of bytes
    in the buffer, like using ioctl with FIONREAD,
    ofcourse that is a C function, and I can't really
    locate a PHP counterpart for it, other than what may
    be a counterpart (socket_get_status). However yet
    another solution is using the recv() counterpart,
    socket_recv() with the MSG_PEEK flag.

    Now you'd probably open up a local copy of the php
    manpages, hunting for socket_recv()! Well it's not
    there, it's an undocumented function (yet), and to my
    knowlegde exists in PHP 4.0.7, atleast in the CVS.

    function prototype:
    mixed socket_recv(resource socket, int len, int flags)

    and example usage:
    $data = socket_recv($sockfd, $nbytes, MSG_PEEK);
    $len = sizeof($data);

    Now you would have to loop through the the complete
    buffer, and let me remind you, that MSG_PEEK means
    peeking into the buffer, it does not "actually" remove
    the data from the recv() queue but only gives you a
    copy, if you call the function without MSG_PEEK you
    will get the same data, and then the data will be
    discarded.

    This is one way of finding out the no. of bytes in the
    buffer, ofcourse socket_get_status() is surely much
    better.

    Incase I interpreted you incorrectly and you wanted to
    know the no. of bytes the recieve buffer can hold then
    here's the function, and this is also 4.0.7+ :

    function prototype:
    mixed socket_getopt(resource socket, int level, int
    optname)

    example usage:

    $info = socket_getopt($sockfd, SOL_SOCKET, SO_RCVBUF);

    and it remains pretty much the same for the send
    buffer :

    $info = socket_getopt($sockfd, SOL_SOCKET, SO_SNDBUF);

    Hope that helps.

    P.S - phpguru.org, nice domain name.

    =====
    *********************************
    Know more about me:
    http://www.geocities.com/mimodit
    *********************************

    __________________________________________________
    Do You Yahoo!?
    Listen to your Yahoo! Mail messages from any phone.
    http://phone.yahoo.com

+ Reply to Thread