-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetdclient.c
59 lines (46 loc) · 1.32 KB
/
netdclient.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
#include "define.h"
struct msg message;
int main(int argc, char const *argv[])
{
int consock;
struct sockaddr_in serveraddress;
int len;
int waittime = 20;
/* create message queue 1*/
key_t key;
int msgid;
key = ftok(PathName, ProjectID1);
msgid = msgget(key, 0666 | IPC_CREAT);
/* connect to netdserver */
consock = socket(AF_INET, SOCK_STREAM, 0);
if(consock < 0)
{
fprintf(stderr, "netdclient-socket() error !\n");
exit(1);
}
serveraddress.sin_family = AF_INET;
serveraddress.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddress.sin_port = htons(PORTSRV2);
bzero(&serveraddress.sin_zero, 8);
if(connect(consock, (struct sockaddr*)& serveraddress, sizeof(serveraddress)) < 0)
{
fprintf(stderr, "netdclient-connect() error!\n");
exit(1);
}
else
{
printf("Connected\n");
}
do
{
/* read data from messsage queue */
bzero(&message.mgs, BUFFER);
msgrcv(msgid, &message,sizeof(message), 1, 0);
printf("Data read from message queue 1: %s\n", message.mgs);
/* send to netdserver */
send(consock, message.mgs, BUFFER, 0);
} while (strcmp(message.mgs, "bye") != 0);
msgctl(msgid, IPC_RMID, NULL);
close(consock);
return 0;
}