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

Function tcpecho_socket_entry

examples/test/net_test.c:204–283  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

202#define TCP_SOCKET_BUFFER_SIZE 4096
203rt_thread_t tcpecho_socket_tid = RT_NULL;
204void tcpecho_socket_entry(void *parameter)
205{
206 char *recv_data;
207 rt_uint32_t sin_size;
208 int sock = -1, connected, bytes_received;
209 struct sockaddr_in server_addr, client_addr;
210
211 recv_data = rt_malloc(TCP_SOCKET_BUFFER_SIZE);
212 if (recv_data == RT_NULL)
213 {
214 rt_kprintf("no memory\n");
215 return;
216 }
217
218 /* create a TCP socket */
219 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
220 {
221 rt_kprintf("create socket error\n");
222 goto _exit;
223 }
224
225 /* initialize server address */
226 server_addr.sin_family = AF_INET;
227 server_addr.sin_port = htons(TCP_SOCKET_ECHO_PORT);
228 server_addr.sin_addr.s_addr = INADDR_ANY;
229 rt_memset(&(server_addr.sin_zero),0, sizeof(server_addr.sin_zero));
230
231 /* bind to server address */
232 if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
233 {
234 rt_kprintf("bind address failed\n");
235 goto _exit;
236 }
237
238 /* listen */
239 if (listen(sock, 5) == -1)
240 {
241 rt_kprintf("listen error\n");
242 goto _exit;
243 }
244
245 sin_size = sizeof(struct sockaddr_in);
246 while(1)
247 {
248 /* accept client connected */
249 connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
250 if (connected > 0)
251 {
252 int timeout;
253
254 /* set timeout option */
255 timeout = 5000; /* 5second */
256 setsockopt(connected, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
257
258 /* handle this client */
259 while (1)
260 {
261 /* receive data from this connection */

Callers

nothing calls this directly

Calls 13

rt_mallocFunction · 0.85
rt_kprintfFunction · 0.85
socketFunction · 0.85
rt_memsetFunction · 0.85
bindFunction · 0.85
listenFunction · 0.85
acceptFunction · 0.85
setsockoptFunction · 0.85
rt_get_errnoFunction · 0.85
sendFunction · 0.85
rt_freeFunction · 0.85
recvFunction · 0.50

Tested by

no test coverage detected