0
I have a server (remote) where redirects your 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, but I am having some problems when it comes to receiving the data from the server, they are coming incomplete, and also I can’t send commands more than 2 times to the server.
the application must send remote commands to the shell (send), and receive the data (recv) until the client wishes to quit. the problem is to receive 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;
}
}
}
Try to use the code available at this link: The overdue Netsock release
– lsalamon