-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
executable file
·190 lines (153 loc) · 3.4 KB
/
server.cpp
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <cassert>
const size_t k_msg_max = 4096;
static void msg( const char* msg);
static int32_t read_full( int connfd , char* buff , int n );
static int32_t write_all( int connfd , const char* buff , int n );
static int32_t one_request(int connfd)
{
//initializing buffer
char rbuf[4 + k_msg_max + 1 ];
//read the message header
errno = 0;
int32_t err = read_full(connfd,rbuf,4);
if( err )
{
if( errno == 0 )
{
msg("EOF");
}
else
{
msg("read() error" );
}
}
//extract and validate the message length
uint32_t len = 0;
memcpy(&len,rbuf,4);//extracts the lenght of message
if( len > k_msg_max )
{
msg("too long");
return -1;
}
//read the message body
err = read_full(connfd,&rbuf[4],len);
if( err )
{
msg("read() error");
return err;
}
//process the message
rbuf[4+len] = '\0';
printf("client says %s\n",&rbuf[4]);
//send a reply to the client
const char reply[] = "world";
char wbuf[4 + sizeof(reply)];
len = (uint32_t)strlen(reply);
memcpy(wbuf,&len,4);//length of message
memcpy(&wbuf[4],reply,len);
return write_all(connfd,wbuf,4 + len);
}
static void msg(const char *msg) {
fprintf(stderr, "%s\n", msg);
}
static void die(const char *msg) {
int err = errno;
fprintf(stderr, "[%d] %s\n", err, msg);
abort();
}
static void do_something(int connfd) {
char rbuf[64] = {};
ssize_t n = read(connfd, rbuf, sizeof(rbuf) - 1);
if (n < 0) {
msg("read() error");
return;
}
printf("client says: %s\n", rbuf);
char wbuf[] = "world";
write(connfd, wbuf, strlen(wbuf));
}
static int32_t read_full( int connfd , char* buff , int n )
{
while( n > 0 )
{
/*rv indicated the number of bytes successfully read from file descriptor*/
ssize_t rv = read(connfd,buff,n);
if( rv < 0 )
{
return -1;
}
assert((size_t)rv <= n );
n -= (size_t)rv;
buff += rv;
}
return 0;
}
static int32_t write_all( int connfd , const char* buff , int n )
{
while( n > 0 )
{
ssize_t rv = write(connfd,buff,n);
if( rv < 0 )
{
return -1;
}
assert((size_t)rv <= n );
n -= (size_t)rv;
buff += rv;
}
return 0;
}
int main() {
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
die("socket()");
}
// this is needed for most server applications
int val = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
// bind
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = ntohs(1234);
addr.sin_addr.s_addr = ntohl(0); // wildcard address 0.0.0.0
int rv = bind(fd, (const sockaddr *)&addr, sizeof(addr));
if (rv) {
die("bind()");
}
// listen
rv = listen(fd, SOMAXCONN);
if (rv) {
die("listen()");
}
while (true) {
// accept
struct sockaddr_in client_addr = {};
socklen_t socklen = sizeof(client_addr);
int connfd = accept(fd, (struct sockaddr *)&client_addr, &socklen);
if (connfd < 0) {
continue; // error
}
do_something(connfd);
//only serves one clint connection at once
while( true )
{
int32_t err = one_request(connfd);
if( err)
{
break;
}
}
close(connfd);
}
close(fd);
return 0;
}