A question about UDP packet receive

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Reeze

    A question about UDP packet receive

    Hi, everyone.
    I need your help!
    I created a UDP server by using $socket =
    stream_socket_s erver($udpAddr, $errno, $errstr, STREAM_SERVER_B IND);
    the client send me a reqest, then server return a "hello string".
    but the client simply send me a name or something very short. I have
    to read upto 100 chars, then the fread can return.

    my Question is: how can I read a udp packet which length is unknow?

    thank a lot!
    Reeze.




    my src code:========== =============== ============
    $pkt = stream_socket_r ecvfrom($socket , 100, 0, $peer);

    $reciv_data = fread($socket, 100);
    $message = preg_replace("/[[:space:]]/", '', $reciv_data); // cleanup
    data
    $response = "hello, $message";
    stream_socket_s endto($socket, $response, 0, $peer);

    echo " In [$message] \n";
    echo " Out [$response] \n";
  • =?UTF-8?B?SXbDoW4gU8OhbmNoZXogT3J0ZWdh?=

    #2
    Re: A question about UDP packet receive

    Reeze wrote:
    I have to read upto 100 chars, then the fread can return.
    >
    my Question is: how can I read a udp packet which length is unknow?
    Guess what: fread() returns when it has read as many characters as
    requested, OR when it hits an end-of-file.

    And, guess what: when the server closes the connection, that is interpreted
    as an end-of-file.

    You should be able to work out the solution with this info. Alternatively,
    RTFM: http://php.net/stream_get_contents

    Cheers,
    --
    ----------------------------------
    Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-

    You fill a much-needed gap.

    Comment

    • Tim Roberts

      #3
      Re: A question about UDP packet receive

      Reeze <reeze.xia@gmai l.comwrote:
      >
      I need your help!
      I created a UDP server by using $socket =
      >stream_socket_ server($udpAddr , $errno, $errstr, STREAM_SERVER_B IND);
      the client send me a reqest, then server return a "hello string".
      but the client simply send me a name or something very short. I have
      >to read upto 100 chars, then the fread can return.
      >
      >my Question is: how can I read a udp packet which length is unknow?
      >...
      >my src code:========== =============== ============
      > $pkt = stream_socket_r ecvfrom($socket , 100, 0, $peer);
      >
      > $reciv_data = fread($socket, 100);
      > $message = preg_replace("/[[:space:]]/", '', $reciv_data); // cleanup
      >data
      > $response = "hello, $message";
      > stream_socket_s endto($socket, $response, 0, $peer);
      >
      > echo " In [$message] \n";
      > echo " Out [$response] \n";
      I suggest that you use EITHER stream_socket_r ecvfrom OR fread. Both serve
      the same purpose in this case, but fread adds another level of buffering.
      It doesn't sound like you really want the buffering.
      --
      Tim Roberts, timr@probo.com
      Providenza & Boekelheide, Inc.

      Comment

      Working...