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;
}
}
}