让PHP响应telnet(PHP Telnet Server)
我有一个项目要做,它是这样的:
当您打开 telnet 控制台并 telnet 到我的服务器时,PHP 应该响应: “你好,你叫什么名字?”
然后你输入你的名字等等。我怎样才能在 PHP 中做到这一点?有可能吗?
I have a project to do and it goes like this:
When you open a telnet console and telnet to my server, PHP should respond with:
"Hello, What is your name?"
And you type in your name and so on. How can I do this in PHP? Is it even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅http://php.net/manual/en/book.sockets.php
这是一个很好的入门教程。 http://devzone.zend.com/article/1086
谷歌也是你的朋友: http://www.google.ca/search?q=php+socket+服务器
See http://php.net/manual/en/book.sockets.php
This is a good tutorial to get you started. http://devzone.zend.com/article/1086
And google is your friend here as well: http://www.google.ca/search?q=php+socket+server
实际的 TELNET 可能有点难以正确;有很多控制序列用于影响终端如何显示其内容。
如果您只是想编写一个人们可以通过 TELNET 使用的服务,那么最好使用现有的 TELNET 服务器(Ubuntu 有几个可用的软件包,用于简单的 TELNET:inetutils- telnetd、
telnetd
;对于加密的 TELNET:telnetd-ssl
;对于 Kerberos 验证的 TELNET:heimdal-servers
、krb5-telnetd
.) 提供 TELNET 功能,并将 PHP 程序设置为您希望每个人登录的用户的登录 shell。 (有关登录 shell 的信息,请参阅passwd(5)
。)如果您想自己执行所有套接字操作,那么您需要使用 BSD 套接字支持; 创建套接字, 将服务器名称绑定到套接字, 监听套接字,当有新连接到来时,< a href="http://php.net/manual/en/function.pcntl-fork.php" rel="nofollow noreferrer">分叉一个新进程来处理新客户端,以及 接受新连接。
一旦您有了一个连接了客户端的新进程,您就可以使用标准的读写操作向客户端发送和接收数据。如果您确实想支持完整的 TELNET 规范,则需要进行一些巧妙的编程。如果您不介意无法访问高级终端功能(例如由
readline(3)
、ncurses(3)
或slang
提供的功能>) 那么您可能需要在第八位打开的情况下删除字符,以摆脱客户端的 TELNET 控制字符。 (我没有尝试过这个,因为当我想要在主机之间建立干净未加密和未经身份验证的连接时,我总是使用netcat
工具。)Actual TELNET can be a little difficult to get correct; there are a lot of control sequences that are used to influence how a terminal displays its contents.
If you simply want to write a service that people can use via TELNET, then it would probably be best to use an existing TELNET server (Ubuntu has several packages available, for simple TELNET:
inetutils-telnetd
,telnetd
; for encrypted TELNET:telnetd-ssl
; for Kerberos-authenticated TELNET:heimdal-servers
,krb5-telnetd
.) to provide TELNET functionality, and set your PHP program as the login shell for whichever user you want everyone to log in as. (Seepasswd(5)
for information on the login shell.)If you want to do all the socket operations yourself, then you'll need to use the BSD sockets support; create a socket, bind a server name to the socket, listen on the socket, and when a new connection comes in, fork a new process to handle the new client, and accept the new connection.
Once you have a new process with a connected client, you can use the standard read and write operations to send and receive data to the client. If you really want to support the full TELNET specification, it'll take some clever programming. If you don't mind not having access to advanced terminal features (such as provided by
readline(3)
,ncurses(3)
, orslang
) then you may need to drop characters with the eighth bit turned on to get rid of the TELNET control characters from your clients. (I've not tried this, because I've always used thenetcat
tool when I wanted a clean unencrypted and unauthenticated connection between hosts.)