#include #include #include #include #include #define PORT 8080 #define BUFFER_SIZE 1024 int main() { // Create socket int serverSocket = socket(AF_INET, SOCK_STREAM, 0); if (serverSocket == -1) { std::cerr << "Error creating socket\n"; return -1; } // Set up server address structure sockaddr_in serverAddress{}; serverAddress.sin_family = AF_INET; serverAddress.sin_addr.s_addr = INADDR_ANY; serverAddress.sin_port = htons(PORT); // Bind the socket if (bind(serverSocket, (struct sockaddr*)&serverAddress;, sizeof(serverAddress)) < 0) { std::cerr << "Error binding socket\n"; return -1; } // std::ifstream fileToSend("in.txt", std::ios::binary); std::cout << "Server listening on port " << PORT << "\n"; // Listen for incoming connections if (listen(serverSocket, 3) < 0) { std::cerr << "Error listening for connections\n"; return -1; } while(true){ // Accept a connection int clientSocket = accept(serverSocket, nullptr, nullptr); if (clientSocket < 0) { std::cerr << "Error accepting connection\n"; return -1; } // Open the file to send std::ifstream fileToSend("binaryfile.bin", std::ios::binary); if (!fileToSend.is_open()) { std::cerr << "Error opening file\n"; return -1; } char buffer[BUFFER_SIZE]; while (!fileToSend.eof()) { fileToSend.read(buffer, sizeof(buffer)); send(clientSocket, buffer, fileToSend.gcount(), 0); } fileToSend.close(); std::cout << "File sent successfully\n"; close(clientSocket); } // Close the file and sockets // fileToSend.close(); // close(clientSocket); close(serverSocket); // std::cout << "File sent successfully\n"; return 0; }