forked from pratyushmp/code_opensource_2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathechoserveri.c
More file actions
47 lines (37 loc) · 1018 Bytes
/
Copy pathechoserveri.c
File metadata and controls
47 lines (37 loc) · 1018 Bytes
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
/*
* echoserveri.c - An iterative echo server
*/
/* $begin echoserverimain */
#include "csapp.h"
void echo(int connfd);
int main(int argc, char **argv)
{
int listenfd, connfd;
socklen_t clientlen;
struct sockaddr_storage clientaddr; /* Enough space for any address */ //line:netp:echoserveri:sockaddrstorage
char client_hostname[MAXBUF], client_port[MAXBUF];
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(0);
}
listenfd = Open_listenfd(argv[1]);
while (1) {
clientlen = sizeof(struct sockaddr_storage);
fprintf(stdout, "clientlen %d", clientlen);
connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);
fprintf(stdout, "connfd %d", connfd);
echo(connfd);
Close(connfd);
}
exit(0);
}
/* $end echoserverimain */
void echo(int connfd)
{
size_t n;
char buf[MAXBUF];
while((n = read(connfd, buf, MAXBUF)) != 0) { //line:netp:echo:eof
printf("server received %d bytes\n", (int)n);
write(connfd, buf, n);
}
}