-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
71 lines (58 loc) · 1.24 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "defines.h"
int main(void)
{
int s, t, len;
struct sockaddr_un remote;
char str[MAX_MSG_SIZE];
fd_set fds, tfds;
FD_ZERO(&fds);
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
remote.sun_family = AF_UNIX;
strcpy(remote.sun_path, SOCK_PATH);
len = strlen(remote.sun_path) + sizeof(remote.sun_family);
if (connect(s, (struct sockaddr *)&remote, len) == -1) {
perror("connect");
exit(1);
}
FD_SET(fileno(stdin), &fds);
FD_SET(s, &fds);
for (;;) {
tfds = fds;
printf("What are you doing right now?\n");
if (select(s + 1, &tfds, NULL, NULL, NULL) == -1) {
perror("select");
exit(1);
}
if (FD_ISSET(s, &tfds)) {
if ((t = recv(s, str, MAX_MSG_SIZE, 0)) > 0) {
str[t] = '\0';
printf("%s", str);
} else {
if (t < 0) perror("recv");
else printf("Server closed connection\n");
exit(1);
}
}
if (FD_ISSET(fileno(stdin), &tfds)) {
if (feof(stdin))
exit(0);
fgets(str, MAX_MSG_SIZE, stdin);
if (send(s, str, strlen(str), 0) == -1) {
perror("send");
exit(1);
}
}
}
close(s);
return 0;
}