-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_tcp_server.c
69 lines (66 loc) · 1.91 KB
/
simple_tcp_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Created by zhonghua dong on 2022/8/23.
//
#include <string.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
setbuf(stdout, NULL);
printf("This is server\n");
// socket
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd == -1) {
printf("Error: socket\n");
return 0;
}
// bind
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(8888);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(listenfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
printf("Error: bind\n");
return 0;
}
// listen
if (listen(listenfd, 5) == -1) {
printf("Error: listen\n");
return 0;
}
int conn;
char clientIP[INET_ADDRSTRLEN] = "";
struct sockaddr_in clientAddr;
socklen_t clientAddrLen = sizeof(clientAddr);
while (1) {
printf("...listening\n");
// accept
conn = accept(listenfd, (struct sockaddr *) &clientAddr, &clientAddrLen);
if (conn < 0) {
printf("Error: accept\n");
continue;
}
// 将IPv4或IPv6 Internet网络地址转换为 Internet标准格式的字符串。
inet_ntop(AF_INET, &clientAddr.sin_addr, clientIP, INET_ADDRSTRLEN);
printf("...connect %s %hu\n", clientIP, ntohs(clientAddr.sin_port));
char buf[255];
while (1) {
memset(buf, 0, sizeof(buf));
// read
int len = recv(conn, buf, sizeof(buf), 0);
buf[len] = '\0';
if (len == 0 || strcmp(buf, "exit") == 0) {
printf("...disconnect %s %hu\n", clientIP, ntohs(clientAddr.sin_port));
break;
}
printf("%s\n", buf);
// write
send(conn, buf, len, 0);
}
close(conn);
}
close(listenfd);
return 0;
}