{"id":56,"date":"2005-07-31T06:21:18","date_gmt":"2005-07-31T11:21:18","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/php\/56"},"modified":"2007-08-07T22:21:33","modified_gmt":"2007-08-08T03:21:33","slug":"tcp-socket-server-in-php","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/PHP\/56\/","title":{"rendered":"TCP Socket Server in PHP"},"content":{"rendered":"<p>This is a very basic TCP server written in PHP.  Run it locally on your own machine; don&#8217;t try it on a production server first. It has an endless loop that will keep it going until you kill the process.<\/p>\n<p>Thanks to John Schimmel for writing it.<\/p>\n<p><!--more--><\/p>\n<pre>\n#!\/usr\/local\/bin\/php &#8211;q \n\n&lt;?php \n\/\/ by John Schimmel \n\/\/ modified from the code at http:\/\/www.zend.com\/pecl\/tutorials\/sockets.php\n\/\/ \n\/\/ run this from terminal on mac os x or another command line interface.\n\n\/\/ Set time limit to indefinite execution \nset_time_limit (0); \n\n\/\/ Set the ip and port we will listen on \n$address = '127.0.0.1'; \n$port = 9000; \n\n\/\/ Create a TCP Stream socket \n$sock = socket_create(AF_INET, SOCK_STREAM, 0); \necho \"PHP Socket Server started at \" . $address . \" \" . $port . \"\\n\";\n\n\/\/ Bind the socket to an address\/port \nsocket_bind($sock, $address, $port) or die('Could not bind to address'); \n\/\/ Start listening for connections \nsocket_listen($sock); \n\n\/\/loop and listen\n\nwhile (true) {\n    \/* Accept incoming requests and handle them as child processes *\/ \n    $client = socket_accept($sock); \n    \n    \/\/ Read the input from the client &#8211; 1024 bytes \n    $input = socket_read($client, 1024); \n    \n    \/\/ Strip all white spaces from input \n    $output = ereg_replace(\"[ \\t\\n\\r]\",\"\",$input).\"\\0\"; \n    \n    \/\/ Display output back to client \n    socket_write($client, \"you wrote \" . $input . \"\\n\"); \n    \n    \/\/ display input on server side\n    echo \"received: \" . $input . \"\\n\";\n}\n\n\/\/ Close the client (child) socket \nsocket_close($client); \n\n\/\/ Close the master sockets \nsocket_close($sock); \n?&gt;\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Run it locally on your own machine; don&#8217;t try it on a production server first.  It has an endless loop that will keep it going until you kill the process.Thanks to John Schimmel for writing it.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-PHP"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}