-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.c
48 lines (37 loc) · 1.02 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include "socket-lib.h"
#define HOST "localhost"
#define PORT 1234
int main() {
if(socket_init() != 0) {
fprintf(stderr, "Socket initialization failed\n");
return 1;
}
SOCKET_T socket = socket_create();
if(socket == 0) {
fprintf(stderr, "Failed to create socket\n");
return 1;
}
if(socket_connect(socket, HOST, PORT) < 0) {
fprintf(stderr, "Failed to connect socket\n");
return 1;
}
printf("Connected!\n");
// Read user input
char input[256];
memset(input, 0, sizeof(char) * 256);
printf("Input: ");
scanf("%s", input);
int sent = send(socket, input, strlen(input) * sizeof(char), 0);
socket_close_send(socket);
printf("%d bytes sent\n", sent);
char output[256];
int read_size = socket_read_all(socket, output, 256);
printf("%s\n", output);
socket_close(socket);
if(socket_quit() != 0) {
fprintf(stderr, "Socket finalization failed\n");
return 1;
}
return 0;
}