Telnet Server

删除回忆录丶 提交于 2019-12-01 06:08:28

For very basic telnet (just telnet to a port and echo bytes), there's not much to do. 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.

But if you're really concerned about RFCs, RFC 854 might be a starting point.

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.

It is very expensive book but there are cheap paperback edition available on 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.

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).

If you are looking to do real telnet, and not just a simple TCP server, then indeed RFC 854 is your starting point. However there's stacks more relevant RFCs which describe those options mentioned above which are listed at http://en.wikipedia.org/wiki/Telnet

If you need help with socket programming etc.

checkout beej's guide: 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.

That said, you might want to use the event loop support provided by GLib and use the related networking library GNet.

Here's how to use GNet to open a socket on port 4000, then close every connection made to it. 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. 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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!