在php中制作Telnet程序卡在这个:转义字符是'^]'



我正在尝试创建这样的东西:

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.

您可以使用套接字函数在 PHP 脚本中创建服务器。查看页面示例 #1 套接字示例:PHP 文档提供的简单 TCP/IP 服务器如何在 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 "$bufn";
} while (true);
socket_close($msgsock);
} while (true);
socket_close($sock);
?>

echo在这里不起作用,因为echo输出STDIO。您需要使用fwrite函数写入套接字,就像写入普通系统文件一样。

看看吧:

https://www.php.net/manual/en/function.fsockopen.php

https://www.php.net/manual/en/function.fwrite.php

最新更新