-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDPServer.c
164 lines (132 loc) · 4.03 KB
/
UDPServer.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <stdio.h>
#define BUFFSIZE 1024
//Uptime function.
char *getUpTime()
{
FILE * uptimefile;
char uptime_chr[28]; // 28 ud fra eksemplet
long uptime = 0;
if((uptimefile = fopen("/proc/uptime", "r")) == NULL)
perror("supt"), exit(EXIT_FAILURE);
bzero(uptime_chr, sizeof(uptime_chr));
fgets(uptime_chr, 12, uptimefile);
fclose(uptimefile);
uptime = strtol(uptime_chr, NULL, 10);
sprintf(uptime_chr, "%02ld:%02ld:%02ld", uptime/3600, ((uptime % 3600) / 60), ((uptime % 3600) - ((uptime % 3600) / 60) * 60));
return(strdup(uptime_chr));
}
char* getLoadAvg()
{
// Variables
FILE* file;
char loadAvg_chr[50];
// Open file
if((file = fopen("/proc/loadavg", "rt")) == NULL) // Open and check if error
perror("loadAvg"), exit(EXIT_FAILURE);
// Read file and save in buffer
bzero(loadAvg_chr, sizeof(loadAvg_chr));
fgets(loadAvg_chr, 50, file);
// Clean up
fclose(file); // Closes file
return(strdup(loadAvg_chr));
}
// Main
int main(int argc, char *argv[])
{
/////////////////////////////////////////////
//Definition of structures and variables.
int sock, length, n;
socklen_t fromlen;
struct sockaddr_in server;
struct sockaddr_in from;
char buf[BUFFSIZE+1];
if (argc < 2) {
fprintf(stderr, "ERROR: No port provided\n");
exit(0);
}
/////////////////////////////////////////////
//Create socket with protocol UDP
sock=socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
{
printf("Error: Opening socket.\n");
exit(1);
}
////////////////////////////////////////////
//Theese lines initializes server to zeros.
length = sizeof(server);
bzero(&server,length);
////////////////////////////////////////////
//Use INTERNET domain (internet sockets).
server.sin_family=AF_INET;
//As server, use own IP(From all interfaces)
server.sin_addr.s_addr=INADDR_ANY;
//Convert port number to network byte order and save it to structure.
server.sin_port=htons(atoi(argv[1]));
///////////////////////////////////////////////
//The bind() system calls binds a socket to an address (address of the current host and port number).
if (bind(sock,(struct sockaddr *)&server,length)<0)
{
printf("Error: Binding.\n");
exit(1);
}
fromlen = sizeof(struct sockaddr_in);
printf("Server started with port %s\n", argv[1]);
//Continuesly run and accept commands.
while (1) {
memset(&buf, 0, sizeof(buf));
//Recieve data from client
puts("Waiting for client...\n");
n = recvfrom(sock,buf,BUFFSIZE,0,(struct sockaddr *)&from,&fromlen);
if (n < 0)
{
printf("Error: Recieving.\n");
exit(1);
}
//Write what was recieved.
write(1,"Received a datagram: ",21);
write(1,buf,n);
char* tmpBuf;
// Send data back if ASCII 'u' or 'U'
if(*buf == 'u' || *buf == 'U')
{
memset(&buf, 0, sizeof(buf));
tmpBuf = getUpTime();
strcpy(buf, tmpBuf);
n = sendto(sock,buf,strlen(buf) + 1,0,(struct sockaddr *)&from,fromlen); // Add +1 because of 0-termination
if (n < 0)
{
printf("Error: Sending.\n");
exit(1);
}
free(tmpBuf);
}
// Send data back if ASCII 'L' or 'l'
else if(*buf == 'l' || *buf == 'L')
{
memset(&buf, 0, sizeof(buf));
tmpBuf = getLoadAvg();
strcpy(buf, tmpBuf);
n = sendto(sock,buf,strlen(buf) + 1, 0,(struct sockaddr *)&from,fromlen);
if (n < 0)
{
printf("Error: Sending.\n");
exit(1);
}
free(tmpBuf);
}
else
{
strcpy(buf, "Screw you\n");
sendto(sock,buf,strlen(buf) + 1, 0,(struct sockaddr *)&from,fromlen);
}
}
return 0;
}