-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminiserver.c
More file actions
90 lines (77 loc) · 2.65 KB
/
Copy pathminiserver.c
File metadata and controls
90 lines (77 loc) · 2.65 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
/*
* miniserver - a simple server using the messaging module
*
* This simple server receives and prints (to stdout) a message and
* expects the user to enter one line of stdin, which it sends back;
* each printed message is surrounded by 'quotes'.
*
* This program is an atypical use of the message module because it
* blocks on input from stdin during a call to handleMessage, rather
* than using handleInput; this approach allows it to respond to each
* message from each correspondent, one by one.
*
* David Kotz - June 2021
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "message.h"
/**************** file-local functions ****************/
static bool handleMessage(void* arg, const addr_t from, const char* message);
/***************** main *******************************/
int
main(const int argc, char* argv[])
{
// initialize the message module (without logging)
int myPort = message_init(NULL);
if (myPort == 0) {
return 2; // failure to initialize message module
} else {
printf("serverPort=%d\n", myPort);
}
// check arguments (there should be none)
const char* program = argv[0];
if (argc != 1) {
fprintf(stderr, "usage: %s\n", program);
return 3; // bad commandline
}
// Loop, waiting for input or for messages; provide callback functions.
// We use the 'arg' parameter to carry a pointer to 'server'.
bool ok = message_loop(NULL, 0, NULL, NULL, handleMessage);
// shut down the message module
message_done();
return ok? 0 : 1; // status code depends on result of message_loop
}
/**************** handleMessage ****************/
/* Datagram received; print it, read a line from stdin, and use it as reply.
* We ignore 'arg' here.
* Return true if EOF on stdin or any fatal error.
*/
static bool
handleMessage(void* arg, const addr_t from, const char* message)
{
// print the message and a prompt
printf("'%s'\n", message);
printf("> ");
fflush(stdout);
// allocate a buffer into which we can read a line of input
// (it can't be any longer than a message)!
char line[message_MaxBytes];
// read a line from stdin: this is VERY unusual for a handleMessage()
if (fgets(line, message_MaxBytes, stdin) == NULL) {
// EOF case: stop looping
putchar('\n');
return true;
} else {
// strip trailing newline
const int len = strlen(line);
if (len > 0 && line[len-1] == '\n') {
line[len-1] = '\0';
}
// send as message back to client
message_send(from, line);
// normal case: keep looping
return false;
}
}