-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_server.cc
110 lines (95 loc) · 2.07 KB
/
tcp_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <stdlib.h>
#include <iostream>
#include <ctype.h>
#include "minet_socket.h"
using std::cout;
using std::cin;
using std::cerr;
using std::endl;
void usage()
{
cerr << "tcp_server k|u port\n";
}
int main(int argc, char *argv[])
{
char buf[512];
sockaddr_in server_sa, client_sa;
int cfd, 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]));
cfd = minet_socket(SOCK_STREAM);
if (cfd<0) {
cerr << "Can't create socket.\n";
minet_perror("reason:");
goto err;
} else {
cerr << "Socket created.\n";
}
if (minet_bind(cfd,&server_sa)<0) {
cerr << "Can't bind socket.\n";
minet_perror("reason:");
goto err;
} else {
cerr << "Socket bound.\n";
}
if (minet_listen(cfd,1)<0) {
cerr << "Can't listen on socket.\n";
minet_perror("reason:");
goto err;
} else {
cerr << "Socket listened.\n";
}
if ((fd=minet_accept(cfd,&client_sa))<0) {
cerr << "Accept failed.\n";
minet_perror("reason:");
goto err;
} else {
cerr << "Accept succeeded.\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_close(cfd);
minet_deinit();
return 0;
}