好繼承人的事情:我想做一個遠程CMD,所以每當我在工作中,我可以從我的家用電腦上下載文件,somereason它不響應我想要的方式,當我發送dir命令它甚至不執行if語句。插座C++,奇怪的問題與接收信息
請原諒我,如果這是顯而易見的新的那些東西。
備註: 服務器連接並打印出發送的緩衝區。
那些由不同的程序v
Server.cpp
#include "FirstTouch.h"
#pragma comment(lib,"ws2_32")
void SendCommands()
{
while (1)
{
char SendBuffer[1500];
cin >> SendBuffer;
send(sConn, SendBuffer, 1500, 0);
if (SendBuffer == "dir")
{
printf("system_dir initiated\n");
Sleep(200);
char File[1500];
recv(sConn, File, strlen(File), 0);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
cout << File << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 5);
delete File;
}
}
}
int main()
{
ConnectToServer();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 5);
printf("connected");
system("cls");
SendCommands();
return 0;
}
Client.cpp
#include <winsock.h>
#include <iostream>
#pragma comment(lib,"ws2_32")
SOCKET s; //Socket handle
//CONNECTTOHOST – Connects to a remote host
bool ConnectToHost(int PortNo, char* IPAddress)
{
//Start up Winsock…
WSADATA wsadata;
int error = WSAStartup(0x0202, &wsadata);
//Did something happen?
if (error)
return false;
//Did we get the right Winsock version?
if(wsadata.wVersion != 0x0202)
{
WSACleanup(); //Clean up Winsock
return false;
}
//Fill out the information needed to initialize a socket…
SOCKADDR_IN target; //Socket address information
target.sin_family = AF_INET; // address family Internet
target.sin_port = htons(PortNo); //Port to connect on
target.sin_addr.s_addr = inet_addr(IPAddress); //Target IP
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //Create socket
if (s == INVALID_SOCKET)
{
return false; //Couldn't create the socket
}
//Try connecting...
if (connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
{
return false; //Couldn't connect
}
else
return true; //Success
}
//CLOSECONNECTION – shuts down the socket and closes any connection on it
void CloseConnection()
{
//Close the socket if it exists
if (s)
closesocket(s);
WSACleanup(); //Clean up Winsock
}
int main()
{
bool Conn = false;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
printf("Trying to make a reliable connection to server...");
while (!Conn)
{
Conn = ConnectToHost(444, "127.0.0.1");
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 5);
system("cls");
printf("Connected say hi!\n");
while (1)
{
char RecvBuffer[1500];
recv(s, RecvBuffer, 1500, 0);
printf(RecvBuffer);
std::cout << std::endl;
if (RecvBuffer == "dir")
{
printf("Executing...\n");
Sleep(100);
system("dir >> temp.txt");
HANDLE hFile = CreateFile("temp.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
LPVOID FileContent = nullptr;
LPDWORD NumOfBytes = nullptr;
ReadFile(hFile, FileContent, 1500, NumOfBytes, NULL);
CloseHandle(hFile);
delete[] NumOfBytes;
send(s, (char*)FileContent, 1500, 0);
delete FileContent;
}
}
return 0;
}
除了忽略recv()調用的結果(由@EJP回答)之外,下面的printf調用假定RecvBuffer'包含空終止的C字符串。這不是一個鎖。 – 2014-09-27 11:40:50
...,(在網絡代碼中似乎幾乎不可避免),濫用strlen:自動存儲var'char File [1500];'緊接着是'recv(sConn,File,strlen(File),0);'。 – 2014-09-27 11:44:07
約。 SO網絡代碼問題中的strlen()調用的1%是有效的。 – 2014-09-27 11:46:11