[Gllug] OT: reading from a socket in php
salsaman at xs4all.nl
salsaman at xs4all.nl
Wed Oct 6 05:14:44 UTC 2010
I should explain a bit more perhaps. The php script sends a trigger to a
running process which makes the process open a send port on udp 9998. The
php script should then connect to the other end of the socket and read the
return data which is being sent. The data is terminated with a NULL byte.
Data can be read using nc -l -u 9998, and via a perl script. Tcpdump also
shows the process responding with 6 bytes on udp 9998.
The php script is able to send (the trigger is done by sending a command
on port 9999), so it seems strange that it cannot receive anything.
Also, the code below is just to test, it only receives 1 byte, in the real
script up to 1024 bytes are read in a loop until there is no more data.
Regards,
Salsaman.
On Wed, October 6, 2010 04:36, salsaman at xs4all.nl wrote:
> Hi all,
> I am trying to write a simple php application which reads from udp port
> 9998/localhost. I know the data is being sent, but php never receives
> anything.
>
> The code i am trying is something like:
>
>
>
> function get_response($status_sock)
> {
>
> do {
> // tried also with PHP_NORMAL_READ
> $recv = socket_read($status_sock, '1024',PHP_BINARY_READ);
>
> // i have also tried:
> //$from='';
> //$port=0;
>
> // socket_recvfrom($status_sock,$recv,1,0,$from,$port);
>
> // tried also with socket_read()
>
> if ($recv != "") {
> // never reaches here...
> echo("got $recv\n");
> return;
> }
> usleep(10000);
> } while ($recv=="");
>
> }
>
> if(($status_sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) < 0) {
> echo "Could not create status socket.\n";
> }
>
> if (!socket_connect($status_sock,"127.0.0.1",9998)) {
> echo "Could not connect";
> }
>
> get_response($status_sock);
>
> socket_close($status_sock);
>
>
>
> Why am I not receiveing any data in the socket ?
>
>
> Salsaman.
>
>
> --
> Gllug mailing list - Gllug at gllug.org.uk
> http://lists.gllug.org.uk/mailman/listinfo/gllug
>
>
--
Gllug mailing list - Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
More information about the GLLUG
mailing list