-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.cpp
90 lines (76 loc) · 2.02 KB
/
monitor.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <iostream>
#include <strings.h>
#include <sstream>
#include <iomanip>
#define buff_size 256 //max length for the name passed
#define mntrPort 25543
#define sa struct sockaddr
using namespace std;
void mntrMain(int sock);
int main() {
int soc_o, conn_o, flag, mdynPort;
struct sockaddr_in mntrAddr;
socklen_t ms_len;
// TCP socket creation and verification
soc_o = socket(AF_INET, SOCK_STREAM, 0); //TCP connection
if (soc_o == -1) {
cout<<"Socket creation failed."<<endl;
exit(0);
}
/*else
cout<<"Socket successfully created."<<endl;*/
bzero(&mntrAddr, sizeof(mntrAddr));
// assign IP, PORT
mntrAddr.sin_family = AF_INET;
mntrAddr.sin_addr.s_addr = htonl(INADDR_ANY);
mntrAddr.sin_port = htons(mntrPort);
// Binding newly created socket to given IP and its verification
if ((bind(soc_o, (sa*)&mntrAddr, sizeof(mntrAddr))) != 0) {
cout<<"Socket binding failed."<<endl;
exit(0);
}
/*else
cout<<"Socket successfully binded."<<endl;*/
// Server listening and its verification
if ((listen(soc_o, 20)) != 0) {
cout<<"Listen failed."<<endl;
exit(0);
}
/*else
cout<<"Monitor listening."<<endl;*/
//Dynamic PORT assignment
ms_len=sizeof(mntrAddr);
flag=getsockname(soc_o, (sa*)&mntrAddr, &ms_len);
if (flag==-1) {
perror("getsockname() failed");
exit(1);
}
else
mdynPort=ntohs(mntrAddr.sin_port);
conn_o = accept(soc_o, (sa*)&mntrAddr, &ms_len);
cout<<"The monitor is up and running."<<endl;
while(1)
mntrMain(conn_o);
return 0;
}
void mntrMain(int sock){
char buf[buff_size];
bzero(buf, sizeof(buf));
read(sock, buf, sizeof(buf));
cout<<buf<<endl;
if ((strncmp(buf, "exit", 4)) == 0) {
cout<<"Monitor Exit."<<endl;
exit(1); //break;
}
}