- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <winsock2.h>
- #include <windows.h>
- #define PORT 12345
- #define BUFFER_SIZE 1024
- #define MAX_CLIENTS 5
- // Structure to hold client information
- struct ClientInfo {
- SOCKET socket;
- struct sockaddr_in address;
- };
- // Function to handle each client
- DWORD WINAPI handleClient(LPVOID lpParam) {
- struct ClientInfo *clientInfo = (struct ClientInfo *)lpParam;
- SOCKET clientSocket = clientInfo->socket;
- // Open and send file
- FILE *fileToSend = fopen("file_to_send.txt", "rb");
- if (!fileToSend) {
- printf("Error opening file\n");
- closesocket(clientSocket);
- free(clientInfo);
- return 1;
- }
- char buffer[BUFFER_SIZE];
- while (1) {
- int bytesRead = fread(buffer, 1, sizeof(buffer), fileToSend);
- if (bytesRead <= 0) {
- break; // End of file or error
- }
- send(clientSocket, buffer, bytesRead, 0);
- }
- printf("File sent to %s:%d\n", inet_ntoa(clientInfo->address.sin_addr), ntohs(clientInfo->address.sin_port));
- // Close the socket and file
- fclose(fileToSend);
- closesocket(clientSocket);
- free(clientInfo);
- return 0;
- }
- int main() {
- WSADATA wsaData;
- SOCKET serverSocket, clientSocket;
- struct sockaddr_in serverAddr, clientAddr;
- int addrSize = sizeof(struct sockaddr_in);
- // Initialize Winsock
- if (WSAStartup(MAKEWORD(2, 2), &wsaData;) != 0) {
- printf("WSAStartup failed\n");
- return 1;
- }
- // Create socket
- if ((serverSocket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
- printf("Error creating socket: %d\n", WSAGetLastError());
- WSACleanup();
- return 1;
- }
- // Configure server address
- memset(&serverAddr;, 0, sizeof(serverAddr));
- serverAddr.sin_family = AF_INET;
- serverAddr.sin_port = htons(PORT);
- serverAddr.sin_addr.s_addr = INADDR_ANY;
- // Bind the socket
- if (bind(serverSocket, (struct sockaddr *)&serverAddr;, sizeof(serverAddr)) == SOCKET_ERROR) {
- printf("Error binding socket: %d\n", WSAGetLastError());
- closesocket(serverSocket);
- WSACleanup();
- return 1;
- }
- // Listen for incoming connections
- if (listen(serverSocket, MAX_CLIENTS) == SOCKET_ERROR) {
- printf("Error listening for connections: %d\n", WSAGetLastError());
- closesocket(serverSocket);
- WSACleanup();
- return 1;
- }
- printf("Server listening on port %d...\n", PORT);
- while (1) {
- // Accept a connection
- if ((clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddr;, &addrSize;)) == INVALID_SOCKET) {
- printf("Error accepting connection: %d\n", WSAGetLastError());
- closesocket(serverSocket);
- WSACleanup();
- return 1;
- }
- // Create a new thread to handle the client
- struct ClientInfo *clientInfo = (struct ClientInfo *)malloc(sizeof(struct ClientInfo));
- clientInfo->socket = clientSocket;
- clientInfo->address = clientAddr;
- HANDLE hThread = CreateThread(NULL, 0, handleClient, (LPVOID)clientInfo, 0, NULL);
- if (hThread == NULL) {
- printf("Error creating thread\n");
- closesocket(clientSocket);
- free(clientInfo);
- } else {
- CloseHandle(hThread);
- }
- }
- // Close the server socket
- closesocket(serverSocket);
- WSACleanup();
- return 0;
- }