Skip to content

Instantly share code, notes, and snippets.

@slogsdon
Created October 11, 2014 17:16
Show Gist options
  • Save slogsdon/3a908a6b4f98f7f4745c to your computer and use it in GitHub Desktop.
Save slogsdon/3a908a6b4f98f7f4745c to your computer and use it in GitHub Desktop.
HTTP server using sockets
<?php
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die('Failed to create socket!');
socket_bind($socket, 0, 3000);
socket_listen($socket);
$msg = '<html><head><title>Hello, world!</title></head><body>Hello, world!</body></html>';
for (;;) {
if ($client = socket_accept($socket)) {
socket_write($client, "HTTP/1.1 200 OK\r\n" .
"Content-length: " . strlen($msg) . "\r\n" .
"Content-Type: text/html; charset=UTF-8\r\n\r\n" .
$msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment