Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* gcc -g -Wall -o client.exe client2.c */
- #include <stdio.h>
- #include <string.h>
- #include <windows.h>
- void print_last_error(const char *fct)
- {
- char *buf;
- DWORD dw = GetLastError();
- FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- dw,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR) &buf,
- 0, NULL );
- // Display the error message and exit the process
- printf("%s failed with error %ld: %s\n", fct, dw, buf);
- LocalFree(buf);
- }
- typedef struct
- {
- OVERLAPPED ol;
- HANDLE pipe;
- /* in normal struct */
- void *data;
- int size;
- } Client;
- Client *
- client_new(const char *name)
- {
- char buf[256];
- Client *clt;
- if (!name)
- return NULL;
- printf("connecting to server...");
- snprintf(buf, sizeof(buf), "\\\\.\\pipe\\%s", name);
- clt = (Client *)calloc(1, sizeof(Client));
- if (!clt)
- return NULL;
- while (1)
- {
- clt->pipe = CreateFile(buf,
- GENERIC_READ | GENERIC_WRITE,
- 0,//FILE_SHARE_READ | FILE_SHARE_WRITE,
- NULL,
- OPEN_EXISTING,
- 0,
- NULL);
- if (clt->pipe != INVALID_HANDLE_VALUE)
- break;
- /* if pipe not busy, we exit */
- if (GetLastError() != ERROR_PIPE_BUSY)
- {
- print_last_error("CreateFile");
- goto free_clt;
- }
- /* pipe busy, so we wait for it */
- if (!WaitNamedPipe(buf, NMPWAIT_WAIT_FOREVER))
- {
- print_last_error("WaitNamedPipe");
- goto close_pipe;
- }
- }
- printf(" done\n");
- return clt;
- close_pipe:
- CloseHandle(clt->pipe);
- free_clt:
- free(clt);
- printf(" failed\n");
- return NULL;
- }
- void
- client_del(Client *clt)
- {
- if (!clt)
- return;
- CloseHandle(clt->pipe);
- free(clt);
- }
- void
- client_data_send(Client *clt, const void *data, int size)
- {
- DWORD to_write;
- DWORD written;
- if (!clt || !data || (size <= 0))
- return;
- while (size > 0)
- {
- if (size <= 65535)
- {
- to_write = size;
- size = 0;
- }
- else
- {
- to_write = 65535;
- size -= 65535;
- }
- if (!WriteFile(clt->pipe, data, to_write, &written, NULL))
- {
- print_last_error("WriteFile");
- break;
- }
- data += to_write;
- /* should never happen */
- if (written < to_write)
- {
- printf("WriteFile: insufficient buffer space\n");
- }
- }
- }
- void
- client_data_get(Client *clt)
- {
- #define BUFSIZE 5
- char buf[BUFSIZE];
- void *data = NULL;
- DWORD nbr_bytes_read;
- int size = 0;
- BOOL res;
- if (!clt)
- return;
- do
- {
- res = ReadFile(clt->pipe, buf, sizeof(buf), &nbr_bytes_read, NULL);
- if (!res)
- {
- if (GetLastError() == ERROR_MORE_DATA)
- {
- printf("more data\n");
- continue;
- }
- else
- {
- print_last_error("ReadFile");
- /* we keep current read data ? (i.e. no free(data) ? ) */
- break;
- }
- }
- if (res) printf("nbr : %ld\n", nbr_bytes_read);
- if (res && (nbr_bytes_read > 0))
- {
- if (!data)
- {
- data = malloc(nbr_bytes_read);
- if (!data)
- break;
- memcpy(data, buf, nbr_bytes_read);
- size = nbr_bytes_read;
- }
- else
- {
- data = realloc(data, size + nbr_bytes_read);
- if (!data)
- {
- size = 0;
- break;
- }
- memcpy(data + size, buf, nbr_bytes_read);
- size += nbr_bytes_read;
- }
- }
- }
- while (!res);
- clt->data = data;
- clt->size = size;
- }
- int main(void)
- {
- Client *clt;
- clt = client_new("toto");
- if (!clt)
- return -1;
- client_data_send(clt, "salut, c'est le client !", strlen("salut, c'est le client !"));
- while (1)
- {
- client_data_get(clt);
- if (clt->data)
- {
- char *buf;
- buf = malloc(clt->size + 1);
- memcpy(buf, clt->data, clt->size);
- buf[clt->size] = '\0';
- printf("client (%d) : %s\n", clt->size, buf);
- free(clt->data);
- clt->data = NULL;
- Sleep(3000);
- client_data_send(clt, "encore des donnees", strlen("encore des donnees"));
- Sleep(2000);
- break;
- }
- }
- client_del(clt);
- return 0;
- }
Add Comment
Please, Sign In to add comment