Hi im currently working on a program that sends a command to the telnet server.
Here is my code:
int _tmain(int argc, _TCHAR* argv[])
{
WSAData wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
addrinfo hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
addrinfo* result;
int res = getaddrinfo("192.168.56.101", "23", &hints, &result);
if (res)
{
std::cout << "failed to getaddrinfo" << std::endl;
}
SOCKET sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (connect(sock, result->ai_addr, (int)result->ai_addrlen) != SOCKET_ERROR && sock != INVALID_SOCKET)
{
std::cout << "connected";
char* buf = "md c:\testfolder
";
std::cout << send(sock, buf, sizeof("md c:\testfolder
"), 0);
const int size = 255;
char out[size];
memset(out, 0, size);
while (true)
{
res = recv(sock, out, size, 0);
std::cout << res << std::endl; // this outputs 21
}
}
else
{
std::cout << res;
}
std::cin.get();
return 0;
}
After I send the command, the telnet server should create a folder in C:\ directory
named testfolder
, but its not doing it. recv()
is sending me garbage values.
I have read RFC 854, but its really hard to read. So please, explain to me what I am doing wrong with my code.
See Question&Answers more detail:os