Skip to content

Instantly share code, notes, and snippets.

@olleharstedt
Created September 5, 2019 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olleharstedt/d8cc4e5c27b0d1dbaf311013c176d2d4 to your computer and use it in GitHub Desktop.
Save olleharstedt/d8cc4e5c27b0d1dbaf311013c176d2d4 to your computer and use it in GitHub Desktop.
socket to browser
<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (socket_bind($sock, '127.0.0.10', 888) === false) {
die('cannot bind' . PHP_EOL);
}
if (socket_listen($sock, 5) === false) {
die('cannot listen' . PHP_EOL);
}
do {
if (($msgsock = socket_accept($sock)) === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
$msg = "HTTP/1.1 200\r\nContent-Type: text/html\r\n\r\n"
. "<!DOCTYPE html><html><head></head><body><h1>asd</h1></body></html>\r\n\r\n";
// Request does NOT work when read is uncommented.
//$buf = socket_read($msgsock, 4012, PHP_NORMAL_READ);
//echo $buf . PHP_EOL;
socket_write($msgsock, $msg, strlen($msg));
socket_close($msgsock);
continue;
} while (true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment