套接字c ++,接收信息的奇怪问题

时间:2014-09-27 09:48:19

标签: c++ sockets client system

嗯继承人的事情:我想创建一个远程cmd,所以每当我在工作中我可以从我的家用电脑下载文件,因为某些原因它不会以我想要的方式响应,当我发送dir命令它甚至不执行if语句。

请原谅我,如果这很明显我是那些新手。

注意: 服务器连接并且客户端打印出已发送的缓冲区。

那些是不同的程序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;
}

1 个答案:

答案 0 :(得分:0)

recv()无法保证填充缓冲区。你必须循环。