Facebook
From Rosidul, 1 Week ago, written in Plain Text.
This paste is a reply to Re: Untitled from Rosidul - view diff
Embed
Download Paste or View Raw
Hits: 180
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <winsock2.h>
  5. #include <windows.h>
  6.  
  7. #define PORT 12345
  8. #define BUFFER_SIZE 1024
  9. #define MAX_CLIENTS 5
  10.  
  11. // Structure to hold client information
  12. struct ClientInfo {
  13.     SOCKET socket;
  14.     struct sockaddr_in address;
  15. };
  16.  
  17. // Function to handle each client
  18. DWORD WINAPI handleClient(LPVOID lpParam) {
  19.     struct ClientInfo *clientInfo = (struct ClientInfo *)lpParam;
  20.     SOCKET clientSocket = clientInfo->socket;
  21.  
  22.     // Open and send file
  23.     FILE *fileToSend = fopen&#40;"file_to_send.txt", "rb"&#41;;
  24.     if (!fileToSend) {
  25.         printf("Error opening file\n");
  26.         closesocket(clientSocket);
  27.         free(clientInfo);
  28.         return 1;
  29.     }
  30.  
  31.     char buffer[BUFFER_SIZE];
  32.  
  33.     while (1) {
  34.         int bytesRead = fread(buffer, 1, sizeof(buffer), fileToSend);
  35.         if (bytesRead <= 0) {
  36.             break; // End of file or error
  37.         }
  38.         send(clientSocket, buffer, bytesRead, 0);
  39.     }
  40.  
  41.     printf("File sent to %s:%d\n", inet_ntoa(clientInfo->address.sin_addr), ntohs(clientInfo->address.sin_port));
  42.  
  43.     // Close the socket and file
  44.     fclose(fileToSend);
  45.     closesocket(clientSocket);
  46.     free(clientInfo);
  47.     return 0;
  48. }
  49.  
  50. int main() {
  51.     WSADATA wsaData;
  52.     SOCKET serverSocket, clientSocket;
  53.     struct sockaddr_in serverAddr, clientAddr;
  54.     int addrSize = sizeof(struct sockaddr_in);
  55.  
  56.     // Initialize Winsock
  57.     if (WSAStartup(MAKEWORD(2, 2), &wsaData;) != 0) {
  58.         printf("WSAStartup failed\n");
  59.         return 1;
  60.     }
  61.  
  62.     // Create socket
  63.     if ((serverSocket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
  64.         printf("Error creating socket: %d\n", WSAGetLastError());
  65.         WSACleanup();
  66.         return 1;
  67.     }
  68.  
  69.     // Configure server address
  70.     memset(&serverAddr;, 0, sizeof(serverAddr));
  71.     serverAddr.sin_family = AF_INET;
  72.     serverAddr.sin_port = htons(PORT);
  73.     serverAddr.sin_addr.s_addr = INADDR_ANY;
  74.  
  75.     // Bind the socket
  76.     if (bind(serverSocket, (struct sockaddr *)&serverAddr;, sizeof(serverAddr)) == SOCKET_ERROR) {
  77.         printf("Error binding socket: %d\n", WSAGetLastError());
  78.         closesocket(serverSocket);
  79.         WSACleanup();
  80.         return 1;
  81.     }
  82.  
  83.     // Listen for incoming connections
  84.     if (listen(serverSocket, MAX_CLIENTS) == SOCKET_ERROR) {
  85.         printf("Error listening for connections: %d\n", WSAGetLastError());
  86.         closesocket(serverSocket);
  87.         WSACleanup();
  88.         return 1;
  89.     }
  90.  
  91.     printf("Server listening on port %d...\n", PORT);
  92.  
  93.     while (1) {
  94.         // Accept a connection
  95.         if ((clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddr;, &addrSize;)) == INVALID_SOCKET) {
  96.             printf("Error accepting connection: %d\n", WSAGetLastError());
  97.             closesocket(serverSocket);
  98.             WSACleanup();
  99.             return 1;
  100.         }
  101.  
  102.         // Create a new thread to handle the client
  103.         struct ClientInfo *clientInfo = (struct ClientInfo *)malloc(sizeof(struct ClientInfo));
  104.         clientInfo->socket = clientSocket;
  105.         clientInfo->address = clientAddr;
  106.  
  107.         HANDLE hThread = CreateThread(NULL, 0, handleClient, (LPVOID)clientInfo, 0, NULL);
  108.         if (hThread == NULL) {
  109.             printf("Error creating thread\n");
  110.             closesocket(clientSocket);
  111.             free(clientInfo);
  112.         } else {
  113.             CloseHandle(hThread);
  114.         }
  115.     }
  116.  
  117.     // Close the server socket
  118.     closesocket(serverSocket);
  119.     WSACleanup();
  120.  
  121.     return 0;
  122. }
  123.