-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
68 lines (51 loc) · 1.39 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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#define BSIZE 256
#define NSTRS 3
#define SOCKET_ADDRESS "mysocket"
/*
* This is the set of strings we will send through the socket for
* conversion
*/
char *strs[NSTRS] = {
"this is the first string from the client\n",
"this is the second string from the client\n",
"this is the third string from the client\n"
};
int main(int argc, char *argv[])
{
int sockfd, ret, i;
struct sockaddr_un saun;
char buf[BSIZE];
#if 1
/* Add Code: Populate the sockaddr_un struct */
strcpy(saun.sun_path, SOCKET_ADDRESS);
saun.sun_family = AF_UNIX;
/* Add Code: Create the client session socket */
sockfd = socket(PF_UNIX, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Error Opening Socket");
return EXIT_FAILURE;
}
/* Add Code: Connect the session socket to the server */
ret = connect(sockfd, (struct sockaddr *) &saun, sizeof(saun));
if (ret < 0) {
perror("Error Connecting Sockets");
return EXIT_FAILURE;
}
/* Add Code: Send the strs array, one string at a time, to the
* server. Read the converted string and print it out before sending
* the next string
*/
for (i = 0; i < NSTRS; i++) {
write(sockfd, strs[i], BSIZE);
printf("SENDING:\n%s", strs[i]);
read(sockfd, buf, BSIZE);
printf("RECEIVED:\n%s\n", buf);
}
close(sockfd);
#endif
}