MCPcopy Create free account
hub / github.com/F-Stack/f-stack / echo_server

Function echo_server

adapter/micro_thread/echo.cpp:52–96  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

50}
51
52int echo_server()
53{
54 struct sockaddr_in addr;
55 addr.sin_family = AF_INET;
56 addr.sin_addr.s_addr = INADDR_ANY;
57 addr.sin_port = htons(80);
58
59 int fd = create_tcp_sock();
60 if (fd < 0) {
61 fprintf(stderr, "create listen socket failed\n");
62 return -1;
63 }
64
65 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
66 close(fd);
67 fprintf(stderr, "bind failed [%m]\n");
68 return -1;
69 }
70
71 if (listen(fd, 1024) < 0) {
72 close(fd);
73 fprintf(stderr, "listen failed [%m]\n");
74 return -1;
75 }
76 int clt_fd = 0;
77 int *p;
78 while (true) {
79 struct sockaddr_in client_addr;
80 int addr_len = sizeof(client_addr);
81
82 clt_fd = mt_accept(fd, (struct sockaddr*)&client_addr, (socklen_t*)&addr_len, -1);
83 if (clt_fd < 0) {
84 mt_sleep(1);
85 continue;
86 }
87 if (set_fd_nonblock(clt_fd) == -1) {
88 fprintf(stderr, "set clt_fd nonblock failed [%m]\n");
89 break;
90 }
91
92 p = new int(clt_fd);
93 mt_start_thread((void *)echo, (void *)p);
94 }
95 return 0;
96}
97
98int main(int argc, char *argv[])
99{

Callers 1

mainFunction · 0.85

Calls 8

create_tcp_sockFunction · 0.85
mt_acceptFunction · 0.85
mt_sleepFunction · 0.85
set_fd_nonblockFunction · 0.85
mt_start_threadFunction · 0.85
bindFunction · 0.70
closeFunction · 0.70
listenFunction · 0.70

Tested by

no test coverage detected