server code: #define WINSOCKDEPRECATEDNOWARNINGS // Suppress deprecated functions…
- User
server code: #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")
const int DEFAULT_PORT = 8080;
int main() { WSADATA wsaData;
// Initialize Winsock int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != 0) { std::cerr << "WSAStartup failed with error: " << iResult << std::endl; return 1; } SOCKET ListenSocket = INVALID_SOCKET; SOCKET ClientSocket = INVALID_SOCKET; struct addrinfo* result = NULL; struct addrinfo hints; ZeroMemory(&hints, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; hints.ai_flags = AI_PASSIVE; // Resolve the server address and port iResult = getaddrinfo(NULL, std::to_string(DEFAULT_PORT).c_str(), &hints, &result); if (iResult != 0) { std::cerr << "getaddrinfo failed with error: " << iResult << std::endl; WSACleanup(); return 1; } // Create a SOCKET to listen for incoming connections ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); if (ListenSocket == INVALID_SOCKET) { std::cerr << "socket failed with error: " << WSAGetLastError() << std::endl; freeaddrinfo(result); WSACleanup(); return 1; } // Setup the TCP listening socket iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); if (iResult == SOCKET_ERROR) { std::cerr << "bind failed with error: " << WSAGetLastError() << std::endl; freeaddrinfo(result); closesocket(ListenSocket); WSACleanup(); return 1; } freeaddrinfo(result); iResult = listen(ListenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { std::cerr << "listen failed with error: " << WSAGetLastError() << std::endl; closesocket(ListenSocket); WSACleanup(); return 1; } // Accept a client socket ClientSocket = accept(ListenSocket, NULL, NULL); if (ClientSocket == INVALID_SOCKET) { std::cerr << "accept failed with error: " << WSAGetLastError() << std::endl; closesocket(ListenSocket); WSACleanup(); return 1; } std::cout << "Client connected" << std::endl; // No longer need server socket closesocket(ListenSocket); const int buf_len = 512; char recvbuf[buf_len]; while (true) { std::string message; std::cout << "Enter message to send to client: "; std::getline(std::cin, message); // Exit condition if (message == "exit") { break; } // 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; } // Clear the result for the new response std::string result; // Receive results from client int totalBytesReceived = 0; int bytesReceived; do { bytesReceived = recv(ClientSocket, recvbuf, buf_len, 0); if (bytesReceived > 0) { totalBytesReceived += bytesReceived; result.append(recvbuf, bytesReceived); // Log received part std::cout << "Bytes received: " << bytesReceived << std::endl; } else if (bytesReceived == 0) { std::cout << "Connection closed by client." << std::endl; break; } else { std::cerr << "recv failed with error: " << WSAGetLastError() << std::endl; closesocket(ClientSocket); WSACleanup(); return 1; } } while (bytesReceived == buf_len); // Continue receiving while buffer is full // Display full response std::cout << "Client result : \n" << result << std::endl; } // Shutdown the connection since we’re done iResult = shutdown(ClientSocket, SD_SEND); if (iResult == SOCKET_ERROR) { std::cerr << "shutdown failed with error: " << WSAGetLastError() << std::endl; closesocket(ClientSocket); WSACleanup(); return 1; } // Cleanup closesocket(ClientSocket); WSACleanup(); return 0; - Assistant