-
Notifications
You must be signed in to change notification settings - Fork 0
/
soc_p2.c
72 lines (55 loc) · 1.38 KB
/
soc_p2.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
72
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/un.h>
#define SOCK_PATH "echo_socket"
// This is socket P2
int main(void)
{
int soc;
struct sockaddr_un rm;
char str[100];
if ((soc = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
printf("Trying to connect...\n");
rm.sun_family = AF_UNIX;
strcpy(rm.sun_path, SOCK_PATH);
int len;
len = strlen(rm.sun_path) + sizeof(rm.sun_family);
if (connect(soc, (struct sockaddr *)&rm, len) == -1) {
perror("connect");
exit(1);
}
int rcvid;
int hid = 0;
int sndid;
while (hid < 49)
{
char str[5][6];
int id[5];
if ((rcvid = recv(soc,str,sizeof(str),0) )== -1)
{
printf("rcvid\n");
}
if ((rcvid=recv(soc, &id, sizeof(id), 0) )==-1 )
{
perror("rcvid\n");
}
hid = id[4];
for (int i = 0; i < 5; i++)
{
printf("ID: %d, string %s\n",id[i],str[i]);
}
if ((sndid=send(soc, &hid, sizeof(int), 0)) < 0) {
perror("sndid");
}
}
close(soc);
return 0;
}