SHARE
TWEET

Untitled

a guest Jun 25th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <winsock2.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <process.h>
  7. #include <windows.h>
  8. void fileReceive(void *param);
  9. HANDLE semaphore;
  10. HANDLE threadHandle;
  11.  
  12.     int main(int argc, char *argv[])
  13.     {
  14.     // Some stuff
  15.     sock0 = socket(AF_INET, SOCK_STREAM, 0);
  16.         if (sock0 == INVALID_SOCKET) {
  17.             goto l_socket_error;
  18.         }
  19.         // associates a local address with a socket
  20.         if (bind(sock0, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
  21.             goto l_bind_error;
  22.         }
  23.  
  24.         while (1) {
  25.  
  26.             // places a socket in a state in which it is listening for an incoming connection
  27.             if (listen(sock0, 1) != 0) {
  28.                 goto l_socket_conn_setup_error;
  29.             }
  30.  
  31.             len = sizeof(client);
  32.             // The accept function permits an incoming connection attempt on a socket.
  33.             sock = accept(sock0, (struct sockaddr *)&client, &len);
  34.             if (sock == INVALID_SOCKET) {
  35.                 goto l_error_accpet;
  36.             }
  37.             semaphore = CreateSemaphore(0, 1, 1, 0);
  38.  
  39.             threadHandle = (HANDLE)_beginthread(&fileReceive, 0, &sock);
  40.             if (threadHandle == 0) {
  41.                 printf("Thread handle error");
  42.                 return 1;
  43.             }
  44.             CloseHandle(semaphore);
  45.         }
  46.  
  47.         WSACleanup();
  48.         return 0;
  49.     }
  50.  
  51. void fileReceive(void *param) {
  52.     int n = 0;
  53.     int sock = *((int *)param);
  54.     unsigned char buf[1];
  55.     unsigned char buff[256] = { 0 };
  56.     FILE *fp = NULL;
  57.     memset(buff, 0, sizeof(buff));
  58.     WaitForSingleObject(semaphore, INFINITE);
  59.     // Receive file name
  60.     int recvFile = recv(sock, buff, 255, 0);
  61.     ReleaseSemaphore(semaphore, 1, 0);
  62.     if ((recvFile == 0) || (recvFile == -1)) {
  63.         goto l_recv_error;
  64.     }
  65.     fp = fopen(buff, "wb+");
  66.     if (fp != NULL) {
  67.  
  68.         printf("file name (%s)n", buff);
  69.  
  70.         while (n = recv(sock, &buf[0], 1, 0) > 0) {
  71.  
  72.             size_t written = fwrite(&buf, sizeof(buf), 1, fp);
  73.  
  74.             if (written != 1) {
  75.                 goto l_write_error;
  76.             }
  77.         }
  78.         printf("(%s) completen", buff);
  79.  
  80.     }
  81.     else {
  82.         goto l_fp_error;
  83.     }
  84.  
  85.     fclose(fp);
  86.     closesocket(sock);
  87.     _endthread();
  88.     CloseHandle(threadHandle);
  89. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top