Created
October 11, 2014 17:16
-
-
Save slogsdon/3a908a6b4f98f7f4745c to your computer and use it in GitHub Desktop.
HTTP server using sockets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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