Repository navigation
Expand file tree
/
Copy pathcliMajor2.c
More file actions
126 lines (110 loc) · 2.41 KB
/
Copy pathcliMajor2.c
File metadata and controls
126 lines (110 loc) · 2.41 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
void error(const char *msg);
int getCpuUsage(){
float usage;
int cpu_load;
FILE *fp;
fp = fopen("/proc/loadavg","r");
if(fp == NULL){
perror("ERROR");
}
else{
fscanf(fp,"%f",&usage); // Read usage from file
fclose(fp);
cpu_load = (int)(usage * 100); // Typecast to int
return cpu_load;
}
}
int main(int argc, char *argv[]) {
int s, s2, portno, n;
int disconnect, nready, nread, maxfd;
struct sockaddr_in serv_addr, cli_addr;
fd_set fds;
int input;
int serv = 0;
int cpu_usage;
char buff[1000];
char *output;
if (argc < 6) {
error("Invalid Arguments");
}
portno = atoi(argv[2]);
memset(buff, '0', sizeof(buff));
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("socket error\n");
exit(EXIT_FAILURE);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
if (connect(s, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
serv = 1;
if (connect(s, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
error("Connect");
}
}
maxfd = s + 1;
while (1) {
/* Set up polling. */
FD_ZERO(&fds);
FD_SET(s,&fds);
FD_SET(0,&fds);
cpu_usage = getCpuUsage();
if (cpu_usage >= atoi(argv[5])) {
nread = sprintf(buff, "-1");
send(s, buff, nread, 0);
break;
}
/* Wait for some input. */
nready = select(maxfd, &fds, (fd_set *) 0, (fd_set *) 0,
(struct timeval *) 0);
if( FD_ISSET(s, &fds)) {
nread = recv(s, buff, sizeof(buff), 0);
/* If error or eof, terminate. */
if(nread < 1) {
close(s);
exit(0);
}
disconnect = atoi(buff);
if (disconnect == -1) {
n = 3;
printf("CLIENT Disconnected\n");
nread = sprintf(buff, "%d", n);
send(s, buff, nread, 0);
}
else {
printf("SERVER Total: %d\n", disconnect);
}
}
if( FD_ISSET(0, &fds)) {
nread = read(0, buff, sizeof(buff));
/* If error or eof, terminate. */
//if(nread < 1) {
//close(s);
//exit(0);
//}
disconnect = atoi(buff);
send( s, buff, nread, 0);
if (disconnect == 0) {
close(s);
}
}
}
close(s);
return 0;
}
void error(const char *msg) {
perror(msg);
exit(1);
}