完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
while (is_running) |
|
相关推荐
6个回答
|
|
哪儿来的第二个?你是说第二次吧,可能第一次没收完数据,又进去了一次,第二次因为有数据不需要等待就直接返回了嘛。
|
|
|
|
代码没贴全 while (is_running) { FD_ZERO(&readset); FD_SET(sock, &readset); LOG_I("Waiting for a new connection..."); /* Wait for read or write */ if (select(sock + 1, &readset, RT_NULL, RT_NULL, &timeout) == 0) continue; /* 接受一个客户端连接socket的请求,这个函数调用是阻塞式的 */ connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size); /* 返回的是连接成功的socket */ if (connected < 0) { LOG_E("accept connection failed! errno = %d", errno); continue; } /* 接受返回的client_addr指向了客户端的地址信息 */ LOG_I("I got a connection from (%s , %d)n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); /* 客户端连接的处理 */ while (is_running) { FD_ZERO(&readset_c); FD_SET(connected, &readset_c); /* Wait for read or write */ if (select(connected + 1, &readset_c, RT_NULL, RT_NULL, &timeout) == 0) continue; /* 从connected socket中接收数据,接收buffer是1024大小,但并不一定能够收到1024大小的数据 */ bytes_received = recv(connected, recv_data, BUFSZ, 0); if (bytes_received < 0) { LOG_E("Received error, close the connect."); closesocket(connected); connected = -1; break; } else if (bytes_received == 0) { /* 打印recv函数返回值为0的警告信息 */ LOG_W("Received warning, recv function return 0."); continue; } else { /* 有接收到数据,把末端清零 */ recv_data[bytes_received] = ' |