在 php 中制作 Telnet 程序卡在了这里:转义字符是 '^]'
Making Telnet program in php stuck at this : Escape character is '^]'
我正在尝试创建这样的东西:
telnet towel.blinkenlights.nl 666
您必须在终端中写入,它会向您发送一些响应,如果服务器要求,用户也可以发送输入。
我已经搜索了很多,但还不够。只是这个小代码:
<?php
$IP_ADDR='127.0.0.1';
$PORT='80';
echo "Welcome to LOCALHOST...";
$connection = fsockopen($IP_ADDR, $PORT);
if(!$connection){
echo "No Connection";
}else{
echo "Hello, what's your name?";
}
?>
但输出显示如下:
telnet 127.0.0.1 80
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
所以在与本地主机连接后它没有显示输出,我必须输入任何字符然后按回车键然后它显示这个输出:
g
Welcome to LOCALHOST...Hello, what's your name?Connection closed by foreign host.
那么每次为输出插入字符时如何避免这种情况?
请帮助我。
谢谢
编辑代码:
My Code :
$IP_ADDR='127.0.0.1';
$PORT='80';
$connection = fsockopen($IP_ADDR, $PORT);
if(!$connection){
echo "No Connection";
}else{
$filename = "sampledata.txt";
$somecontent = "Weekend is about to come.";
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file";
exit;
}
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file";
exit;
}
echo "Success";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
}
Output :
telnet 127.0.0.1 80
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
d
Success, wrote (Weekend is about to come.) to file (sampledata.txt)Connection closed by foreign host.
echo
在这里不起作用,因为 echo
将输出放入 STDIO
。您需要使用 fwrite
函数写入套接字,就像您对普通系统文件所做的那样。
查看:
您使用 PHP 文档提供的 socket functions to create a server inside your PHP script. Check the page Example #1 Socket example: Simple TCP/IP server 如何在 PHP 中创建服务器。
#!/usr/local/bin/php -q
<?php
error_reporting(E_ALL);
/* Allow the script to hang around waiting for connections. */
set_time_limit(0);
/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush();
$address = '192.168.1.53';
$port = 10000;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_listen($sock, 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
do {
if (($msgsock = socket_accept($sock)) === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
break;
}
/* Send instructions. */
$msg = "\nWelcome to the PHP Test Server. \n" .
"To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
socket_write($msgsock, $msg, strlen($msg));
do {
if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
break 2;
}
if (!$buf = trim($buf)) {
continue;
}
if ($buf == 'quit') {
break;
}
if ($buf == 'shutdown') {
socket_close($msgsock);
break 2;
}
$talkback = "PHP: You said '$buf'.\n";
socket_write($msgsock, $talkback, strlen($talkback));
echo "$buf\n";
} while (true);
socket_close($msgsock);
} while (true);
socket_close($sock);
?>
我正在尝试创建这样的东西:
telnet towel.blinkenlights.nl 666
您必须在终端中写入,它会向您发送一些响应,如果服务器要求,用户也可以发送输入。
我已经搜索了很多,但还不够。只是这个小代码:
<?php
$IP_ADDR='127.0.0.1';
$PORT='80';
echo "Welcome to LOCALHOST...";
$connection = fsockopen($IP_ADDR, $PORT);
if(!$connection){
echo "No Connection";
}else{
echo "Hello, what's your name?";
}
?>
但输出显示如下:
telnet 127.0.0.1 80
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
所以在与本地主机连接后它没有显示输出,我必须输入任何字符然后按回车键然后它显示这个输出:
g
Welcome to LOCALHOST...Hello, what's your name?Connection closed by foreign host.
那么每次为输出插入字符时如何避免这种情况?
请帮助我。 谢谢
编辑代码:
My Code :
$IP_ADDR='127.0.0.1';
$PORT='80';
$connection = fsockopen($IP_ADDR, $PORT);
if(!$connection){
echo "No Connection";
}else{
$filename = "sampledata.txt";
$somecontent = "Weekend is about to come.";
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file";
exit;
}
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file";
exit;
}
echo "Success";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
}
Output :
telnet 127.0.0.1 80
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
d
Success, wrote (Weekend is about to come.) to file (sampledata.txt)Connection closed by foreign host.
echo
在这里不起作用,因为 echo
将输出放入 STDIO
。您需要使用 fwrite
函数写入套接字,就像您对普通系统文件所做的那样。
查看:
您使用 PHP 文档提供的 socket functions to create a server inside your PHP script. Check the page Example #1 Socket example: Simple TCP/IP server 如何在 PHP 中创建服务器。
#!/usr/local/bin/php -q
<?php
error_reporting(E_ALL);
/* Allow the script to hang around waiting for connections. */
set_time_limit(0);
/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush();
$address = '192.168.1.53';
$port = 10000;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_listen($sock, 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
do {
if (($msgsock = socket_accept($sock)) === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
break;
}
/* Send instructions. */
$msg = "\nWelcome to the PHP Test Server. \n" .
"To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
socket_write($msgsock, $msg, strlen($msg));
do {
if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
break 2;
}
if (!$buf = trim($buf)) {
continue;
}
if ($buf == 'quit') {
break;
}
if ($buf == 'shutdown') {
socket_close($msgsock);
break 2;
}
$talkback = "PHP: You said '$buf'.\n";
socket_write($msgsock, $talkback, strlen($talkback));
echo "$buf\n";
} while (true);
socket_close($msgsock);
} while (true);
socket_close($sock);
?>