MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / chargen_thread

Function chargen_thread

examples/network/chargen.c:56–174  ·  view source on GitHub ↗

* void chargen_thread(void *arg) * * chargen task. This server will wait for connections on well * known TCP port number: 19. For every connection, the server will * write as much data as possible to the tcp port. **************************************************************/

Source from the content-addressed store, hash-verified

54 * write as much data as possible to the tcp port.
55 **************************************************************/
56static void chargen_thread(void *arg)
57{
58 int listenfd;
59 struct sockaddr_in chargen_saddr;
60 fd_set readset;
61 fd_set writeset;
62 int i, maxfdp1;
63 struct charcb *p_charcb;
64
65 /* First acquire our socket for listening for connections */
66 listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
67 LWIP_ASSERT("chargen_thread(): Socket create failed.", listenfd >= 0);
68 memset(&chargen_saddr, 0, sizeof(chargen_saddr));
69 chargen_saddr.sin_family = AF_INET;
70 chargen_saddr.sin_addr.s_addr = htonl(INADDR_ANY);
71 chargen_saddr.sin_port = htons(19); // Chargen server port
72
73 if (bind(listenfd, (struct sockaddr *) &chargen_saddr, sizeof(chargen_saddr)) == -1)
74 LWIP_ASSERT("chargen_thread(): Socket bind failed.", 0);
75
76 /* Put socket into listening mode */
77 if (listen(listenfd, MAX_SERV) == -1)
78 LWIP_ASSERT("chargen_thread(): Listen failed.", 0);
79
80
81 /* Wait forever for network input: This could be connections or data */
82 for (;;)
83 {
84 maxfdp1 = listenfd + 1;
85
86 /* Determine what sockets need to be in readset */
87 FD_ZERO(&readset);
88 FD_ZERO(&writeset);
89 FD_SET(listenfd, &readset);
90 for (p_charcb = charcb_list; p_charcb; p_charcb = p_charcb->next)
91 {
92 if (maxfdp1 < p_charcb->socket + 1)
93 maxfdp1 = p_charcb->socket + 1;
94 FD_SET(p_charcb->socket, &readset);
95 FD_SET(p_charcb->socket, &writeset);
96 }
97
98 /* Wait for data or a new connection */
99 i = select(maxfdp1, &readset, &writeset, 0, 0);
100
101 if (i == 0) continue;
102
103 /* At least one descriptor is ready */
104 if (FD_ISSET(listenfd, &readset))
105 {
106 /* We have a new connection request!!! */
107 /* Lets create a new control block */
108 p_charcb = (struct charcb *)rt_calloc(1, sizeof(struct charcb));
109 if (p_charcb)
110 {
111 p_charcb->socket = accept(listenfd,
112 (struct sockaddr *) &p_charcb->cliaddr,
113 &p_charcb->clilen);

Callers

nothing calls this directly

Calls 11

socketFunction · 0.85
bindFunction · 0.85
listenFunction · 0.85
rt_callocFunction · 0.85
acceptFunction · 0.85
rt_freeFunction · 0.85
closesocketFunction · 0.85
do_readFunction · 0.85
close_chargenFunction · 0.85
selectFunction · 0.50
writeFunction · 0.50

Tested by

no test coverage detected