gpt4 book ai didi

C++ WinSock 向 telnet 服务器发送命令

转载 作者:行者123 更新时间:2023-11-28 05:03:32 24 4
gpt4 key购买 nike

所以我正在制作一个通过 telnet 连接的 C++ 程序。连接似乎正常工作,因为我从服务器获得了输出。问题是我需要向 telnet 发送特定命令。例如命令:

login user password

只是通过 send() 发送它似乎没有做任何事情(或者我做错了什么,但没有看到)。页面上有一些信息ftp://ftp.rfc-editor.org/in-notes/rfc854.txt但我真的不明白。

有人可以给我看一个例子并解释我应该怎么做才能让它发挥作用吗?试图谷歌,但我并没有真正找到我想要的东西。我真的需要它来发送两个命令。

这是我尝试过的(不是完整代码):

int main() {
int iR;
std::string input;
std::cout << "Telnet ip: ";
std::getline(std::cin, input);
std::cout << std::endl;

u_long ip = 0;
u_short port = DEFAULT_PORT;
u_long * ptr = &ip

const char * cptr = input.c_str();
inet_pton(AF_INET, cptr, ptr);

std::cout << "Telnet port: ";
std::getline(std::cin, input);
std::cout << std::endl;
iR = std::stoi(input);
if((iR > SHRT_MAX) || (iR < 0)) {
std::cout << "Wrong port number, using default." << std::endl;
}
else {
port = iR;
}

WSADATA wsaData;
iR = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iR != 0) { return 1; }
SOCKET server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (server == INVALID_SOCKET) { WSACleanup(); return 1; }

sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = ip;
addr.sin_port = htons(port);

std::string user;
std::string pass;
std::cout << "Username: ";
std::getline(std::cin, user);
std::cout << std::endl; << "Password: ";
std::getline(std::cin, pass);
std::cout << std::endl;

std::cout << "Connecting..." << std::endl;
if ((connect(server, (SOCKADDR *)&addr, sizeof(addr))) == SOCKET_ERROR) {
std::cout << "Couldn't connect. << std::endl;
WSAcleanup();
Sleep(2000);
return 0;
}

int bRecv = 0;
char buff[2048];

bRecv = recv(server, buff, sizeof(buff), 0);
if ((bRecv == SOCKET_ERROR) || (bRecv == 0)) {
std::cout << "Disconnected from the server." << std::endl;
WSACleanup();
return 0;
}

buff[bRecv] = 0;
// When it connects successfully, a message appears on the screen from server
std::cout << buff << std::endl;

memset(buff, 0, sizeof(buff));
std::string command = "login ";
command.append(user);
command.append(" ");
command.append(pass);

cptr = login.c_str();
strcpy_s(buff, cptr);

iR = send(server, buff, (u_int)strlen(buff), 0);
if (iR == SOCKET_ERROR) { std::cout << "Couldn't send data." << std::endl; WSACleanup(); return 0;
while (true) {
bRecv = recv(server, buff, sizeof(buff), 0);
if ((bRecv == SOCKET_ERROR) || (bRecv === 0)) {
std::cout << "Disconnected from the server." << std::endl;
break;
}
buff[bRecv] = 0;
std::cout << buff << std::endl;
}
WSACleanup();
return 0;
}

我的意思是我希望我没有以错误的方式做这件事。我几乎是 C++ 的初学者。

最佳答案

您没有在命令后发送换行符。

std::string command = "login ";
command.append(user);
command.append(" ");
command.append(pass);
command.append("\r\n"); // <-- add this

你也不需要使用buff来持有命令,你可以直接发送command:

iR = send(server, command.c_str(), (u_int) command.length(), 0);

关于C++ WinSock 向 telnet 服务器发送命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45357819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com