Winsock remote data reading (CMD)

0

I have a (remote) server where it redirects its shell to the client (remote), the client side is using netcat or telnet for communication. I am developing the client side for use with Windows, however I am having some problems when receiving the data from the server, they are coming incomplete, and I also can not send commands more than 2 times to the server.

The application should send remote commands to the shell (send), and receive the data (recv) until the client wishes to exit. the problem is getting the complete data from the server ...

What should be done to improve this application?

while (1)
{
    //READ
    memset(pbuf, 0, sizeof(pbuf));
    iResult = recv(client[iD].socket, pbuf, sizeof(pbuf) - 1, 0);
    if (iResult < 0)
    {
        if (WSAGetLastError() == WSAEWOULDBLOCK)
        {
            continue;
        }

        printf("recv error: %d\n", WSAGetLastError());
        return SOCKET_ERROR;
    }
    else if (iResult == 0)
    {
        printf("disconnected\n");
        return 0;
    }
    else
    {
        for (int i = 0; i < iResult; i++)
        {
            std::cout << pbuf[i];
        }
        memset(pbuf, 0, sizeof(pbuf));

        //SEND
        memset(key_buffer, 0, sizeof(key_buffer));
        fgets(key_buffer, sizeof(key_buffer) - 1, stdin);
        if (!strcmp(key_buffer, "exit"))
            break;
        iResult = send(client[iD].socket, key_buffer, sizeof(key_buffer) - 1, 0);
        if (iResult == SOCKET_ERROR) {
            printf("send failed with error: %d\n", WSAGetLastError());
            CLEAN_UP();
            return 1;
        }

    }
}
    
asked by anonymous 12.01.2015 / 12:17

1 answer

0

"Receiving full server data", the problem might be in the buffer you are using "pbuf" , or it may not be handled properly during the process . tries to create a function to receive and send data to that Shell.

Example:

while (1)
{
    if (ReadShell(client[iD].socket) == 3)
        break;
}

attempts to use this for data read:

char chunk[1024] = "";

    memset(chunk, 0, sizeof(chunk));
    if ((iResult = recv(client[iD].socket, chunk, 1024, 0)) < 0)
    {   
        std::cout << "Falha ao receber dados do servidor" << std::endl;
        getchar();
        return 3;
    }
    else
    {
        for (int i = 0; i < iResult; i++)
        {
            if (chunk[i] != NULL)
                printf("%c", chunk[i]);
        }
    }

And this to send the data:

  char message[DEFAULT_BUFLEN] = "";
        Sleep(500);
        memset(message, 0, sizeof(message));
        fgets(message, sizeof(message), stdin);
        if (send(client[iD].socket, message, strlen(message), 0) < 0)
        {
            puts("Falha ao enviar dados.");
            getchar();
            return 3;
        }

        if (!strcmp(message, "exit\n"))
        {   
            std::cout << "\nFinalizando Shell..." << std::endl;
            Sleep(1000);
            return 3;
        }
    
16.01.2015 / 07:11