我怎样才能使用C /目标C的telnet的结果? [英] How can I get the telnet result using C / Objective C?

查看:155
本文介绍了我怎样才能使用C /目标C的telnet的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个远程登录网站:

Here is a telnet site:

远程登录://202.85.101.136:86​​04 /

telnet://202.85.101.136:8604/

这是香港公共图书馆,我可以写一些程序来获取字符串/从telnet服务的结果,从发送C /目标C的要求?太赫兹û。

It is from Hong Kong public library, can I write some programme to get the string / result from the telnet service, and send the request from C / Objective C? thz u.

推荐答案

如果你有一个telnet程序已经在你的系统上,你可以用它来完成所有的连接为你工作。下面是GNU / Linux的一个程序,你可以作为一个起点使用。

If you have a telnet program already on your system, you can use it to do all the connection work for you. Here's a program for gnu/Linux that you can use as a starting point.

它使用的popen 执行系统的telnet命令。然后,它只是从管道(标准输出,如果你只是自己从shell中执行telnet命令),并打印它读取的所有数据。当没有更多的数据看,它退出。

It uses popen to execute the system's telnet command. Then it just reads all data from the pipe (stdout if you just executed the telnet command by itself from the shell) and prints it. When there's no more data to read, it exits.

您可以通过在 RW​​ 模式打开,而不是研究管道中,然后写像将数据发送到服务器你将任何其他文件。你可以有条件做的东西一样扫描您的输入用户名:,然后发送一个用户名串过,比如

You can send data to the server by opening the pipe in rw mode instead of r and then writing like you would any other file. You could conditionally do stuff like scan your input for Username: and then send a username string too, for instance.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
        const char *cmd = "telnet 202.85.101.136 8604";
        char buffer[256];

        FILE *pipe = popen(cmd, "r");
        if( !pipe ) { perror("popen"); exit(-1); }

        while( fgets(buffer, sizeof(buffer), pipe) != NULL &&
               !feof(pipe) )
        {
                if( ferror(pipe) ) { perror("fgets"); break; }

                /* Here you do whatever you want with the data. */
                printf("%s", buffer);
        }

        pclose(pipe);

        return 0;
}

如果您正在使用Windows,<一个href=\"http://stackoverflow.com/questions/450865/what-is-the-equivalent-to-posix-popen-in-the-win32-api\">this链接解释了替代popen方法。

If you're using Windows, this link explains the alternative to popen.

还有一个名为计划预计,可以帮助你的自动化这样的东西。

There's also a program called Expect that can help you automate stuff like this.

这篇关于我怎样才能使用C /目标C的telnet的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆