-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp_server.cc
92 lines (79 loc) · 1.69 KB
/
udp_server.cc
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
#include <stdlib.h>
#include <iostream>
#include <ctype.h>
#include "minet_socket.h"
using std::cout;
using std::cerr;
using std::endl;
void usage()
{
cerr << "udp_server k|u port\n";
}
int main(int argc, char *argv[])
{
char buf[512];
sockaddr_in server_sa;
int fd, rc;
if (argc!=3) {
usage();
exit(-1);
}
if (toupper(argv[1][0])=='K') {
cerr << "Using kernel stack.\n";
if (minet_init(MINET_KERNEL)<0) {
cerr << "Stack initialization failed.\n";
goto err;
} else {
cerr << "Stack initialized.\n";
}
} else {
cerr << "Using Minet User Level Stack.\n";
if (minet_init(MINET_USER)<0) {
cerr << "Stack initialization failed.\n";
goto err;
} else {
cerr << "Stack initialized.\n";
}
}
bzero(&server_sa,sizeof(server_sa));
server_sa.sin_family=AF_INET;
server_sa.sin_addr.s_addr=htonl(INADDR_ANY);
server_sa.sin_port=htons(atoi(argv[2]));
fd = minet_socket(SOCK_DGRAM);
if (fd<0) {
cerr << "Can't create socket.\n";
minet_perror("reason:");
goto err;
} else {
cerr << "Socket created.\n";
}
if (minet_bind(fd,&server_sa)<0) {
cerr << "Can't bind socket.\n";
minet_perror("reason:");
goto err;
} else {
cerr << "Socket bound.\n";
}
cout << "Beginning to echo text.\n";
while (1) {
if ((rc=minet_read(fd,buf,512))<0) {
cerr << "Read failed.\n";
minet_perror("reason:");
goto err;
}
if (rc==0) {
cerr << "Done.\n";
goto done;
}
if (write(fileno(stdout),buf,rc)<0) {
cerr << "Write failed.\n";
minet_perror("reason:");
goto err;
}
}
done:
err:
minet_close(fd);
minet_deinit();
return 0;
}