-
Notifications
You must be signed in to change notification settings - Fork 0
/
pf_inet-sock_stream-server.c
55 lines (41 loc) · 1.03 KB
/
pf_inet-sock_stream-server.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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include "develop.h"
int main(int argc, char *argv[]) {
int fd, clifd;
struct sockaddr_in address;
int opt, addrlen;
char buffer[1024];
int ret;
if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) < 0) {
error("Failed to create socket!(error: %d)", fd);
exit(fd);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
address.sin_port = htons(9000);
if ((ret = bind(fd, (struct sockaddr *)&address, sizeof(address)))) {
error("Failed to bind address! (%d)", ret);
exit(0);
}
if (listen(fd, 3) < 0) {
error("Failed to listen address!");
exit(0);
}
while (1) {
if ((clifd = accept(fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0) {
error("Failed to accept!");
close(fd);
exit(-1);
}
read(clifd, buffer, sizeof(buffer));
info("receive from client[](%d bytes): %s",strlen(buffer), buffer);
close(clifd);
}
close(fd);
return 0;
}