MadJuggla9
2[H]4U
- Joined
- Oct 9, 2002
- Messages
- 3,515
I'm wanting a php program that runs on my machine that I can telnet to on a certain port and type commands to, possibly tie it into a shell or something later.
I've found 2 examples of code to start off but neither work. I tried enabling the sockets library extension and it still can't seem to find the libraries. I get these errors, ideas?:
listen_all.php
socket_bind.php
I've found 2 examples of code to start off but neither work. I tried enabling the sockets library extension and it still can't seem to find the libraries. I get these errors, ideas?:
Code:
C:\Users\chris\sockets>php listen_all.php
Fatal error: Call to undefined function socket_create_listen() in C:\Users\chris
\sockets\listen_all.php on line 3
C:\Users\chris\sockets>php socket_bind.php
Fatal error: Call to undefined function socket_create() in C:\Users\chris\socket
s\socket_bind.php on line 6
C:\Users\chris\sockets>
listen_all.php
Code:
<?php
$socket = socket_create_listen("12345") or die();
echo "doesnt make it to here";
if (!$socket) {
echo "Failed to create socket!\n";
exit;
}
while (true) {
$client = socket_accept($socket);
$welcome = "\nWelcome to the server.\nType '!close' to close this connection, or type '!halt' to halt the server.\n";
socket_write($client, $welcome);
while (true) {
$input = trim(socket_read ($client, 256));
if ($input == '!close') {
echo "input was !CLOSE\n";
break;
}
if ($input == '!halt') {
echo "input was !HALT\n";
socket_close ($client);
break 2;
}
$output = str_rot13($input) . "\n";
socket_write($client, $output);
print "Them: $input, Us: $output\n";
}
socket_close ($client);
}
socket_close ($socket);
?>
socket_bind.php
Code:
<?php
$address = "127.0.0.1";
$port = "12345";
/* create a socket in the AF_INET family, using SOCK_STREAM for TCP connection */
$mysock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($mysock, $address, $port);
socket_listen($mysock, 5);
$client = socket_accept($mysock);
// Read the input from the client – 1024 bytes
$input = socket_read($client, 1024);
$output = "thanks for connecting, you wrote: ".$input."\r\n";
// Display output back to client
socket_write($client, $output);
socket_close($client);
socket_close($mysock);
?>