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

Replies to Re: Untitled rss

Title Name Language When
Re: Re: Untitled Rosidul text 7 Months ago.