Last active
May 7, 2024 13:12
-
-
Save CamKem/ae23795d17ac89dcd3914715d04206ef to your computer and use it in GitHub Desktop.
Terminal Chat Server
This file contains 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 | |
// Client script | |
$socket = stream_socket_client("tcp://localhost:8000", $errorNumber, $errorMessage); | |
if (!$socket) { | |
echo "{$errorMessage} ({$errorNumber})<br />\n"; | |
} else { | |
stream_set_blocking($socket, false); | |
$username = readline("Enter your username: "); | |
fwrite($socket, trim($username) . "\n"); | |
while (true) { | |
$read = array(STDIN, $socket); | |
$write = null; | |
$except = null; | |
if (stream_select($read, $write, $except, 0, 200000)) { | |
if (in_array(STDIN, $read, true)) { | |
$input = fgets(STDIN); | |
if ($input === false || trim($input) === '') { | |
continue; | |
} | |
if (trim($input) === '/quit') { | |
fwrite($socket, trim($input) . "\n"); | |
break; | |
} | |
fwrite($socket, trim($input) . "\n"); | |
} | |
if (in_array($socket, $read, true)) { | |
$message = fgets($socket); | |
if ($message === false) { | |
echo "Server disconnected\n"; | |
break; | |
} | |
echo $message; | |
} | |
} | |
} | |
fclose($socket); | |
} |
This file contains 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 | |
// Server script | |
$server = stream_socket_server("tcp://localhost:8000", $errorNumber, $errorMessage); | |
if (!$server) { | |
echo "{$errorMessage} ({$errorNumber})<br />\n"; | |
} else { | |
$clients = array(); | |
$usernames = array(); | |
while (true) { | |
$read = array_merge(array($server), $clients); | |
$write = null; | |
$except = null; | |
if (stream_select($read, $write, $except, 0, 200000)) { | |
if (in_array($server, $read, true)) { | |
$clients[] = $client = stream_socket_accept($server); | |
$username = trim(fgets($client)); | |
$usernames[(int)$client] = $username; | |
echo $username . " connected\n"; | |
$key = array_search($server, $read, true); | |
unset($read[$key]); | |
fwrite($client, "Welcome to the chat server, {$username}\n"); | |
} | |
foreach ($read as $client) { | |
$message = fgets($client); | |
if ($message === false || feof($client) || trim($message) === '/quit') { | |
echo $usernames[(int)$client] . " disconnected\n"; | |
$key = array_search($client, $clients, true); | |
unset($clients[$key], $usernames[(int)$client]); | |
fclose($client); | |
} else { | |
foreach ($clients as $other_client) { | |
if ($other_client !== $client) { | |
fwrite($other_client, $usernames[(int)$client] . ": " . $message); | |
} | |
} | |
} | |
} | |
} | |
} | |
fclose($server); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment