-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrv.c
66 lines (54 loc) · 1.34 KB
/
srv.c
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
/* srv.c */
#include <netinet/in.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define PORT 8181
#define QUEUE 5
int main() {
int serverfd, clientfd;
socklen_t addrlen;
struct sockaddr_in srv, cli;
char buf[512];
char *data;
addrlen = 0;
memset(&srv, 0, sizeof(srv));
memset(&cli, 0, sizeof(cli));
serverfd = socket(AF_INET, SOCK_STREAM, 0);
if (serverfd < 0) {
printf("socket()\n");
return -1;
}
srv.sin_family = AF_INET;
srv.sin_addr.s_addr = 0;
srv.sin_port = htons(PORT);
if (bind(serverfd, (struct sockaddr *)&srv, sizeof(srv)) != 0) {
printf("bind()\n");
close(serverfd);
return -1;
}
if (listen(serverfd, QUEUE) != 0) {
printf("listen()\n");
close(serverfd);
return -1;
}
printf("Listening on 0.0.0.0:%d\n", PORT);
clientfd = accept(serverfd, (struct sockaddr *)&srv, &addrlen);
if (clientfd < 0) {
printf("accept()\n");
close(serverfd);
return -1;
}
printf("Client connected\n");
read(clientfd, buf, 511);
data = "http v1.0\n";
write(clientfd, data, strlen(data));
close(clientfd);
close(serverfd);
return 0;
}