-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
53 lines (32 loc) · 982 Bytes
/
server.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
//server - server.c
#include"server.h"
extern config_t config;
#include<stdio.h>
#include<unistd.h>
/* additional libraries here */
#define MAX 255
/* code here will run after server is binded to socket, but before reading any packets */
void init(int argc, char* argv[], char* env[], int sfd) {
fprintf("Starting server on port %d with pid %d\n", config.port, getpid());
//server setup code here
}
/* code here will run indefinitely; make sure to manage your memory */
void loop(int argc, char* argv[], char* env[], int sfd) {
char input[MAX];
char output[MAX];
if( !getpacket(sfd, input, MAX) ) {
goto EXCEPTION;
}
//echo server: (replace with real work here)
snprintf(output, MAX, "Echo: %s", input);
putpacketl(sfd, output);
if(0) {
EXCEPTION:
putpacketl(sfd, "Internal Server Error\n");
}
}
/* code here will be run upon exit() or SIGINT */
void quit(void) {
fprintf(stderr, "Exiting server\n");
//at_exit code here
}