note 106942 added to sockets.examples

From: Date: Tue, 20 Dec 2011 03:41:12 +0000
Subject: note 106942 added to sockets.examples
Groups: php.notes 
Request: Send a blank email to php-notes+get-185170@lists.php.net to get a copy of this message
HTTP server example: <?php //Accpet the http client request and generate response content. //As a demo, this function just send "PHP HTTP Server" to client. function handle_http_request($address, $port) { $max_backlog = 16; $res_content = "HTTP/1.1 200 OK Content-Length: 15 Content-Type: text/plain; charset=UTF-8 PHP HTTP Server"; $res_len = strlen($res_content); //Create, bind and listen to socket if(($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === FALSE) { echo "Create socket failed!\n"; exit; } if((socket_bind($socket, $address, $port)) === FALSE) { echo "Bind socket failed!\n"; exit; } if((socket_listen($socket, $max_backlog)) === FALSE) { echo "Listen to socket failed!\n"; exit; } //Loop while(TRUE) { if(($accept_socket = socket_accept($socket)) === FALSE) { continue; } else { socket_write($accept_socket, $res_content, $res_len); socket_close($accept_socket); } } } //Run as daemon process. function run() { if(($pid1 = pcntl_fork()) === 0) //First child process { posix_setsid(); //Set first child process as the session leader. if(($pid2 = pcntl_fork()) === 0) //Second child process, which run as daemon. { //Replaced with your own domain or address. handle_http_request('www.codinglabs.org', 9999); } else { //First child process exit; exit; } } else { //Wait for first child process exit; pcntl_wait($status); } } //Entry point. run(); ?> ---- Server IP: 220.181.84.183 Probable Submitter: 220.181.136.232 ---- Manual Page -- http://www.php.net/manual/en/sockets.examples.php Edit -- https://master.php.net/note/edit/106942 Del: integrated -- https://master.php.net/note/delete/106942/integrated Del: useless -- https://master.php.net/note/delete/106942/useless Del: bad code -- https://master.php.net/note/delete/106942/bad+code Del: spam -- https://master.php.net/note/delete/106942/spam Del: non-english -- https://master.php.net/note/delete/106942/non-english Del: in docs -- https://master.php.net/note/delete/106942/in+docs Del: other reasons-- https://master.php.net/note/delete/106942 Reject -- https://master.php.net/note/reject/106942 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#185170) next »