如何通过c ++程序与telnet通信?

问题描述 投票:0回答:1

此刻,我只想做:

telnet www.opera.com 80
GET / HTTP/1.1
Host: www.opera.com

但来自Mac计算机上的c ++程序。

我了解到可以使用system()调用原则,但似乎看不到要多读一行。我已经尝试了多线程(在一个线程中执行system(telnet www.opera.com 80);,然后等待几秒钟,然后执行cout << "GET / HTTP/1.1\n" << "Host: www.opera.com\n\n";),但是似乎cout和telnet调用无法相互通信。我还尝试将所有内容都放在一个system()行(system(telnet www.opera.com 80\nGET / HTTP/1.1\nHost: www.opera.com\n\n);)中,但随后似乎读取了“ telnet www.opera.com 80”,其余的都搁置了,直到我退出telnet。

#include<iostream>
#include<ctime>
#include<cstdlib>
#include<thread>
using namespace std;


void wait(int sec)
{
    time_t start;
    time(&start);
    time_t end;
    for(time(&end); difftime(end, start) < sec; time(&end));
}

void activate_Telnet()
{
    system("telnet www.opera.com 80");
    return;
}


int main()
{
    thread t1(activate_Telnet);
    wait(2);

    cout << "GET / HTTP/1.1\n";
    cout << "Host: www.opera.com\n\n";
    t1.join();
    return 0;
}

我期望服务器发出HTTP/1.1 200 OK类的响应,但我的终端仅显示:

Connected to front-tls-geo.opcom-prod.route53.opera.com.
Escape character is '^]'.
GET / HTTP/1.1
Host: www.opera.com

(尽管在这种情况下并不需要,但也希望在telnet输出中读取程序方面有所帮助。)>

目前,我只想做:telnet www.opera.com 80 GET / HTTP / 1.1主机:www.opera.com,但来自Mac计算机上的c ++程序。我了解到可以使用system()调用原则,但是我无法...

c++ macos telnet
1个回答
0
投票

Telnet用于终端访问。通常用于检查端口是否打开。打开端口后,它将返回“已连接”。与HTTP不同,我们无法通过Telnet发送Get,并且期望200 OK。

© www.soinside.com 2019 - 2024. All rights reserved.