简体   繁体   English

Telnet服务器

[英]Telnet Server

I would like to implement a telnet server in C. How would I proceed with this? 我想在C中实现一个telnet服务器。我该如何处理? Which RFCs should I look at? 我应该看哪些RFC? This is important to me, and I would appreciate any help. 这对我很重要,我将不胜感激。

For very basic telnet (just telnet to a port and echo bytes), there's not much to do. 对于非常基本的telnet(只是telnet到端口和echo字节),没什么可做的。 Read from a socket, process it (in an echo server, do nothing), spit back a result. 从套接字读取,处理它(在回显服务器中,什么也不做),吐出结果。 You could implement a simple MUD-style server without knowing anything in any RFCs. 您可以在不了解任何RFC的情况下实现简单的MUD样式的服务器。

But if you're really concerned about RFCs, RFC 854 might be a starting point. 但是如果你真的关心RFC,那么RFC 854可能就是一个起点。

http://www.faqs.org/rfcs/rfc854.html http://www.faqs.org/rfcs/rfc854.html

If you are serious about network programming I would highly recommend Richard W. Stevens' " UNIX Network Programming Vol 1 " - it's much better reading than RFCs with great examples. 如果你认真对待网络编程我会强烈推荐Richard W. Stevens的“ UNIX网络编程第1卷 ” - 它比RFC有更好的例子。

It is very expensive book but there are cheap paperback edition available on eBay. 这是非常昂贵的书,但在eBay上有便宜的平装版。 Even if you get expensive hard-cover edition it worth every penny you paid. 即使你得到昂贵的硬封面版,也值得你付出的每一分钱。

Note that real telnet is not just a simple interface that handles the stdin and stdout of the user's login shell. 需要注意的是真正的telnet 只是一个简单的界面,处理stdinstdout的用户的登录shell的。

There's lots of additional functionality that is carried separately in 'options', which handle such things as setting the $TERM environment variable, setting the rows/columns (and resetting them if the user resizes their terminal). 在'options'中分别进行了许多附加功能,它们处理诸如设置$TERM环境变量,设置行/列(以及在用户调整其终端时重置它们)之类的事情。

If you are looking to do real telnet, and not just a simple TCP server, then indeed RFC 854 is your starting point. 如果你想要真正的 telnet,而不仅仅是一个简单的TCP服务器,那么RFC 854确实是你的起点。 However there's stacks more relevant RFCs which describe those options mentioned above which are listed at http://en.wikipedia.org/wiki/Telnet 然而,堆栈更相关的RFC描述了上面提到的那些选项,这些选项列在http://en.wikipedia.org/wiki/Telnet

If you need help with socket programming etc. 如果您需要插槽编程等方面的帮助

checkout beej's guide: http://beej.us/guide/bgnet/ checkout beej的指南: http ://beej.us/guide/bgnet/

Knowing how the socket API works internally is very useful, because it is often exported with very minor changes by higher level languages. 了解套接字API如何在内部工作非常有用,因为它通常通过更高级别的语言进行非常小的更改而导出。

That said, you might want to use the event loop support provided by GLib and use the related networking library GNet . 也就是说,您可能希望使用GLib提供的事件循环支持并使用相关的网络库GNet

Here's how to use GNet to open a socket on port 4000, then close every connection made to it. 以下是如何使用GNet在端口4000上打开套接字,然后关闭对其进行的每个连接。 There is a little bit of magic here as the server registers itself with the default main context as part of its creation. 这里有一点点魔力,因为服务器将其自身注册为默认主要上下文作为其创建的一部分。

#include <glib.h>
#include <gnet.h>

void client_connect(GServer G_GNUC_UNUSED *server, GConn *conn, gpointer G_GNUC_UNUSED user_data){
  g_print("Connection from %s\n", conn->hostname);
  gnet_conn_disconnect(conn);
  gnet_conn_unref(conn); conn = NULL;
}

int main(void){
  GMainLoop *loop = g_main_loop_new(NULL, FALSE);
  GServer *server;
  gnet_init();
  server = gnet_server_new(NULL, 4000, client_connect, NULL);
  g_main_loop_run(loop);
  g_main_loop_unref(loop); loop = NULL;
  return 0;
}

I recommend installing Wireshark to watch the Telnet traffic using an existing Telnet server. 我建议使用现有的Telnet服务器安装Wireshark以观察Telnet流量。 Then looking at the log, you can gain a better understanding of how the server communicates with the client. 然后查看日志,您可以更好地了解服务器如何与客户端通信。 Then, use the RFC as a reference if you don't understand any of the commands going across the wire. 然后,如果您不理解通过网络传输的任何命令,请使用RFC作为参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM