[Sussex] Sockets interface in PHP

Oli oli at 3ait.co.uk
Fri Mar 30 09:42:58 UTC 2007


Hi Brendan,

You need to set the O_NONBLOCK option on the socket just after you
create it and also trap the error you'll get from socket_read when it
returns no data.  I'm afraid I don't know the PHP calls exactly for this
offhand.

:-)

HTH,

-Oli

Brendan Whelan wrote:
> I am developing a sockets interface, in PHP running under Fedora Core 5,
> to receive requests and return data. See below is the program in its
> most basic form. It accepts requests and returns data but the
> program waits on the socket_read and only moves on when either data is
> received or the sending system terminates the connection. This means
> that other users/systems cannot gain access. I have hunted around the
> Internet and tried various combinations without success. 
>  
> Any suggestions?
>  
> Thanks, Brendan  
>  
> #!/usr/local/bin/php -q
> <?php
> // Simple sockets receive program
> error_reporting (E_ALL);
>  
> /* Allow the script to hang around waiting for connections. */
> set_time_limit (0);
>  
> /* Turn on implicit output flushing so we see what we're getting
>  * as it comes in. */
> ob_implicit_flush ();
> print "Started\n";
>  
> $address = '192.168.40.19';
> // PORT NEEDS TO BE PERMITTED THROUGH FIREWALL
> $port = 21244;
>  
> if (($sock = socket_create (AF_INET, SOCK_STREAM, 0)) < 0) {
>     echo "socket_create() failed: reason: " . socket_strerror ($sock) .
> "\n";
> }
>  
> if (($ret = socket_bind ($sock, $address, $port)) < 0) {
>     echo "socket_bind() failed: reason: " . socket_strerror ($ret) . "\n";
> }
>  
> if (($ret = socket_listen ($sock, 5)) < 0) {
>     echo "socket_listen() failed: reason: " . socket_strerror ($ret) . "\n";
> }
>  
> do {
> print "In loop\n";
>     if (($msgsock = socket_accept($sock)) < 0) {
>         echo "socket_accept() failed: reason: " . socket_strerror
> ($msgsock) . "\n";
>         break;
>     }
> print "After accept\n";
>     /* Send instructions. */
>     $msg = "\nWelcome to the PHP Test Server. \n" .
>         "To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
>     socket_write($msgsock, $msg, strlen($msg));
> print "Above inner loop\n";
>     do {
>         if (FALSE === ($buf = socket_read ($msgsock, 2048,
> PHP_NORMAL_READ))) {
>             echo "socket_read() failed: reason: " . socket_strerror
> ($ret) . "\n";
>             break 2;
>         }
>         if (!$buf = trim ($buf)) {
>             continue;
>         }
>         if ($buf == 'quit') {
>             break;
>         }
>         if ($buf == 'shutdown') {
>             socket_close ($msgsock);
>             break 2;
>         }
>         $talkback = "PHP: You said '$buf'.\n";
>         socket_write ($msgsock, $talkback, strlen ($talkback));
>         echo "$buf\n";
>     } while (true);
>     socket_close ($msgsock);
> } while (true);
>  
> socket_close ($sock);
> ?>
> 
> 
> ------------------------------------------------------------------------
> 
> __ 
> Sussex mailing list
> Sussex at mailman.lug.org.uk
> E-mail Address: sussex at mailman.lug.org.uk
> Sussex LUG Website: http://www.sussex.lug.org.uk/
> https://mailman.lug.org.uk/mailman/listinfo/sussex

-- 
Oli Comber
Systems Developer
3aIT Limited - Official Corporate Sponsor of the British Bobsleigh Team

4-10 Barttelot Rd   Horsham   West Sussex   RH12 1DQ
M: +44 (0)77255 82405   T: +44 (0)870 881 5097   F: +44 (0)870 116 0793

3aIT Limited is a company registered in England and Wales.
CoReg: 3866698   VATReg: 771388600


Visit www.3aIT.co.uk for Design, Systems, Support

Disclaimer:
The information contained within this email is confidential and may be
legally privileged. It is intended solely for the addressee. If you are
not the intended recipient, any disclosure, copying or distribution of
this email is prohibited and may be unlawful. The content of this email
represents the views of the individual and not necessarily 3aIT Limited.
 3aIT Limited reserves the right to monitor the content of all emails in
accordance with lawful business practice. Whilst every effort is made to
ensure that attachments are free from computer viruses before
transmission, 3aIT Limited does not accept any liability in respect of
any virus that is not detected.
3aIT Limited




More information about the Sussex mailing list