sockets - variable length packets in php -


I am receiving packets sent to my server via UDP. I am using socket_read to read the data and it is just coming up properly. Although I have attended an error in my case the length parameter of socket_read is not always the same, the length of the data can be anywhere from 50-150 bytes. The one thing that remains constant is that the data set has ended with a \ x00 byte, how will I get the job of reading me to always read this face? I've already tried the PHP_NORMAL_READ flag, but docs says that it only ends at \ n or \ r which I really do not want (it tries it does not work for my data). At the same time, the php page for socket_read located in length parameter description,

The maximum number of read bytes specified by the length parameter is specified. Otherwise you can use \ r, \ n, or \ 0 to read (depending on type parameter, see below).

does not say anything about type / 0 bytes. A piece of its documentation is missing. What I need is a function that will either allow me to specify a delimiter for my data, automatically read all the data from the available socket. The Socket_recv function can be a solution, but its inaccuracy and I do not know how it works.

Thanks in advance.

If I understand correctly, then you want to read the data from the socket, as long as reading the data If there is no other data, then there is a quantity of data when there is a problem and you do not do it according to the related manual page:

Note: socket_read () a zero length string (" ) When someone else does not have to read the data.

You should be able to handle variable-length data by reading byte-by-byte on the zero-length string:

  while ($ $ CurrentByte = Socket_read ($ socket, 1))! = "") {// whatever you want with current byte}  

Comments