Guest User

Untitled

a guest
Dec 25th, 2016
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.10 KB | None | 0 0
  1. /* gcc -g -Wall -o client.exe client2.c */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. #include <windows.h>
  7.  
  8. void print_last_error(const char *fct)
  9. {
  10.     char *buf;
  11.     DWORD dw = GetLastError();
  12.  
  13.     FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  14.                   FORMAT_MESSAGE_FROM_SYSTEM |
  15.                   FORMAT_MESSAGE_IGNORE_INSERTS,
  16.                   NULL,
  17.                   dw,
  18.                   MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  19.                   (LPTSTR) &buf,
  20.                   0, NULL );
  21.  
  22.     // Display the error message and exit the process
  23.  
  24.     printf("%s failed with error %ld: %s\n", fct, dw, buf);
  25.     LocalFree(buf);
  26. }
  27.  
  28.  
  29. typedef struct
  30. {
  31.     OVERLAPPED ol;
  32.     HANDLE pipe;
  33.  
  34.     /* in normal struct */
  35.     void *data;
  36.     int size;
  37. } Client;
  38.  
  39. Client *
  40. client_new(const char *name)
  41. {
  42.     char buf[256];
  43.     Client *clt;
  44.  
  45.     if (!name)
  46.         return NULL;
  47.  
  48.     printf("connecting to server...");
  49.  
  50.     snprintf(buf, sizeof(buf), "\\\\.\\pipe\\%s", name);
  51.  
  52.     clt = (Client *)calloc(1, sizeof(Client));
  53.     if (!clt)
  54.         return NULL;
  55.  
  56.     while (1)
  57.     {
  58.         clt->pipe = CreateFile(buf,
  59.                                GENERIC_READ | GENERIC_WRITE,
  60.                                0,//FILE_SHARE_READ | FILE_SHARE_WRITE,
  61.                                NULL,
  62.                                OPEN_EXISTING,
  63.                                0,
  64.                                NULL);
  65.         if (clt->pipe != INVALID_HANDLE_VALUE)
  66.             break;
  67.  
  68.         /* if pipe not busy, we exit */
  69.         if (GetLastError() != ERROR_PIPE_BUSY)
  70.         {
  71.             print_last_error("CreateFile");
  72.             goto free_clt;
  73.         }
  74.  
  75.         /* pipe busy, so we wait for it */
  76.         if (!WaitNamedPipe(buf, NMPWAIT_WAIT_FOREVER))
  77.         {
  78.             print_last_error("WaitNamedPipe");
  79.             goto close_pipe;
  80.         }
  81.     }
  82.  
  83.     printf(" done\n");
  84.  
  85.     return clt;
  86.  
  87.   close_pipe:
  88.     CloseHandle(clt->pipe);
  89.   free_clt:
  90.     free(clt);
  91.  
  92.     printf(" failed\n");
  93.     return NULL;
  94. }
  95.  
  96. void
  97. client_del(Client *clt)
  98. {
  99.     if (!clt)
  100.         return;
  101.  
  102.     CloseHandle(clt->pipe);
  103.     free(clt);
  104. }
  105.  
  106. void
  107. client_data_send(Client *clt, const void *data, int size)
  108. {
  109.     DWORD to_write;
  110.     DWORD written;
  111.  
  112.     if (!clt || !data || (size <= 0))
  113.         return;
  114.  
  115.     while (size > 0)
  116.     {
  117.         if (size <= 65535)
  118.         {
  119.             to_write = size;
  120.             size = 0;
  121.         }
  122.         else
  123.         {
  124.             to_write = 65535;
  125.             size -= 65535;
  126.         }
  127.  
  128.         if (!WriteFile(clt->pipe, data, to_write, &written, NULL))
  129.         {
  130.             print_last_error("WriteFile");
  131.             break;
  132.         }
  133.  
  134.         data += to_write;
  135.  
  136.         /* should never happen */
  137.         if (written < to_write)
  138.         {
  139.             printf("WriteFile: insufficient buffer space\n");
  140.         }
  141.     }
  142. }
  143.  
  144. void
  145. client_data_get(Client *clt)
  146. {
  147. #define BUFSIZE 5
  148.     char buf[BUFSIZE];
  149.     void *data = NULL;
  150.     DWORD nbr_bytes_read;
  151.     int size = 0;
  152.     BOOL res;
  153.  
  154.     if (!clt)
  155.         return;
  156.  
  157.     do
  158.     {
  159.         res = ReadFile(clt->pipe, buf, sizeof(buf), &nbr_bytes_read, NULL);
  160.         if (!res)
  161.         {
  162.             if (GetLastError() == ERROR_MORE_DATA)
  163.             {
  164.                 printf("more data\n");
  165.                 continue;
  166.             }
  167.             else
  168.             {
  169.                 print_last_error("ReadFile");
  170.                 /* we keep current read data ? (i.e. no free(data) ? ) */
  171.                 break;
  172.             }
  173.         }
  174.         if (res) printf("nbr : %ld\n", nbr_bytes_read);
  175.         if (res && (nbr_bytes_read > 0))
  176.         {
  177.             if (!data)
  178.             {
  179.                 data = malloc(nbr_bytes_read);
  180.                 if (!data)
  181.                     break;
  182.                 memcpy(data, buf, nbr_bytes_read);
  183.                 size = nbr_bytes_read;
  184.             }
  185.             else
  186.             {
  187.                 data = realloc(data, size + nbr_bytes_read);
  188.                 if (!data)
  189.                 {
  190.                     size = 0;
  191.                     break;
  192.                 }
  193.                 memcpy(data + size, buf, nbr_bytes_read);
  194.                 size += nbr_bytes_read;
  195.             }
  196.         }
  197.     }
  198.     while (!res);
  199.  
  200.     clt->data = data;
  201.     clt->size = size;
  202. }
  203.  
  204. int main(void)
  205. {
  206.     Client *clt;
  207.  
  208.     clt = client_new("toto");
  209.     if (!clt)
  210.         return -1;
  211.  
  212.     client_data_send(clt, "salut, c'est le client !", strlen("salut, c'est le client !"));
  213.  
  214.     while (1)
  215.     {
  216.         client_data_get(clt);
  217.         if (clt->data)
  218.         {
  219.             char *buf;
  220.             buf = malloc(clt->size + 1);
  221.             memcpy(buf, clt->data, clt->size);
  222.             buf[clt->size] = '\0';
  223.             printf("client (%d) : %s\n", clt->size, buf);
  224.             free(clt->data);
  225.             clt->data = NULL;
  226.             Sleep(3000);
  227.             client_data_send(clt, "encore des donnees", strlen("encore des donnees"));
  228.             Sleep(2000);
  229.             break;
  230.         }
  231.     }
  232.  
  233.     client_del(clt);
  234.  
  235.     return 0;
  236. }
Add Comment
Please, Sign In to add comment