SilverHood Apps's solution to "I need help with code that can receive TCP data into a PHP file"

Hi, do test and let me know.. <\/p>\n

<?php<\/span>\n\n/* Allow the script to hang around waiting for connections. */<\/span>\n\nset_time_limit<\/span>(<\/span>0<\/span>);<\/span>\n\n/* Turn on implicit output flushing so we see what we’re getting<\/span>\n\n* as it comes in. */<\/span>\n\nob_implicit_flush<\/span>();<\/span>\n\n$hostname<\/span> =<\/span> &<\/span>quot<\/span>;<\/span>www<\/span>.<\/span>example<\/span>.<\/span>com<\/span>&<\/span>quot<\/span>;;<\/span>\n\n$address<\/span> =<\/span> gethostbyname<\/span>(<\/span>$hostname<\/span>);<\/span> //eg. ‘100.220.110.5’;<\/span>\n\n$port<\/span> =<\/span> 80<\/span>;<\/span>\n\nif<\/span> ((<\/span>$sock<\/span> =<\/span> socket_create<\/span>(<\/span>AF_INET<\/span>,<\/span> SOCK_STREAM<\/span>,<\/span> SOL_TCP<\/span>))<\/span> ===<\/span> false<\/span>)<\/span> {<\/span>\n\n    echo<\/span> “socket_create<\/span>()<\/span> failed<\/span>:<\/span> reason<\/span>:<\/span> ”<\/span> .<\/span> socket_strerror<\/span>(<\/span>socket_last_error<\/span>())<\/span> .<\/span> “\\n”<\/span>;<\/span>\n\n}<\/span>\n\nif<\/span> (<\/span>socket_bind<\/span>(<\/span>$sock<\/span>,<\/span> $address<\/span>,<\/span> $port<\/span>)<\/span> ===<\/span> false<\/span>)<\/span> {<\/span>\n\n    echo<\/span> “socket_bind<\/span>()<\/span> failed<\/span>:<\/span> reason<\/span>:<\/span> ”<\/span> .<\/span> socket_strerror<\/span>(<\/span>socket_last_error<\/span>(<\/span>$sock<\/span>))<\/span> .<\/span> “\\n”<\/span>;<\/span>\n\n}<\/span>\n\nif<\/span> (<\/span>socket_listen<\/span>(<\/span>$sock<\/span>,<\/span> 5<\/span>)<\/span> ===<\/span> false<\/span>)<\/span> {<\/span>\n\n    echo<\/span> “socket_listen<\/span>()<\/span> failed<\/span>:<\/span> reason<\/span>:<\/span> ”<\/span> .<\/span> socket_strerror<\/span>(<\/span>socket_last_error<\/span>(<\/span>$sock<\/span>))<\/span> .<\/span> “\\n”<\/span>;<\/span>\n\n}<\/span>\n\ndo<\/span> {<\/span>\n\n    if<\/span> ((<\/span>$msgsock<\/span> =<\/span> socket_accept<\/span>(<\/span>$sock<\/span>))<\/span> ===<\/span> false<\/span>)<\/span> {<\/span>\n\n        echo<\/span> “socket_accept<\/span>()<\/span> failed<\/span>:<\/span> reason<\/span>:<\/span> ”<\/span> .<\/span> socket_strerror<\/span>(<\/span>socket_last_error<\/span>(<\/span>$sock<\/span>))<\/span> .<\/span> “\\n”<\/span>;<\/span>\n\n        break<\/span>;<\/span>\n\n    }<\/span>\n\n    do<\/span> {<\/span>\n\n        if<\/span> (<\/span>false<\/span> ===<\/span> (<\/span>$buf<\/span> =<\/span> socket_read<\/span>(<\/span>$msgsock<\/span>,<\/span> 2048<\/span>,<\/span> PHP_NORMAL_READ<\/span>)))<\/span> {<\/span>\n\n            echo<\/span> “socket_read<\/span>()<\/span> failed<\/span>:<\/span> reason<\/span>:<\/span> ”<\/span> .<\/span> socket_strerror<\/span>(<\/span>socket_last_error<\/span>(<\/span>$msgsock<\/span>))<\/span> .<\/span> “\\n”<\/span>;<\/span>\n\n            break<\/span> 2<\/span>;<\/span>\n\n        }<\/span>\n        //trim the message<\/span>\n        if<\/span> (<\/span>!<\/span>$buf<\/span> =<\/span> trim<\/span>(<\/span>$buf<\/span>))<\/span> {<\/span>\n            continue<\/span>;<\/span>\n        }<\/span>\n\n        //if device said quite then exit<\/span>\n        if<\/span> (<\/span>$buf<\/span> ==<\/span> 'quit'<\/span>)<\/span> {<\/span>\n            break<\/span>;<\/span>\n        }<\/span>\n\n        //if server needs to be shut down<\/span>\n        if<\/span> (<\/span>$buf<\/span> ==<\/span> 'shutdown'<\/span>)<\/span> {<\/span>\n            socket_close<\/span>(<\/span>$msgsock<\/span>);<\/span>\n            break<\/span> 2<\/span>;<\/span>\n        }<\/span>\n\n        //Use only for taking back to your device<\/span>\n\n        //$talkback = “PHP: You said ‘$buf’.\\n”;<\/span>\n\n        //socket_write($msgsock, $talkback, strlen($talkback));<\/span>\n\n        echo<\/span> “<\/span>$buf\\n”<\/span>;<\/span>\n\n    }<\/span> while<\/span> (<\/span>true<\/span>);<\/span>\n\n    socket_close<\/span>(<\/span>$msgsock<\/span>);<\/span>\n\n}<\/span> while<\/span> (<\/span>true<\/span>);<\/span>\n\nsocket_close<\/span>(<\/span>$sock<\/span>);<\/span>\n\n?><\/span>\n<\/pre><\/div>
Hi, the main part here is to read `$buf = socket_read($msgsock, 2048, PHP_NORMAL_READ) echo $buf` This is put into an infinite loop so that it keeps listening to the socket inside `do{ }while(true)` Complete code, do test and let me know..

User: SilverHood Apps

Question: I need help with code that can receive TCP data into a PHP file

Back to question