Merge pull request #135 from DavidLin1577/patch-6

Fixed some stackover flow bugs in  tcp_client.c/tcp_server.c
This commit is contained in:
Supowang1989
2020-04-13 10:19:42 +08:00
committed by GitHub
2 changed files with 14 additions and 6 deletions

View File

@@ -23,7 +23,11 @@ void application_entry(void *arg)
MX_LWIP_Init();
while (1) {
socketfd = socket(AF_INET, SOCK_STREAM, 0);
if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("create socket error %s errno: %d\n", strerror(errno), errno);
goto tcp_client_exit;
}
setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(struct timeval));
memset(&sockaddr, 0, sizeof(sockaddr));

View File

@@ -48,8 +48,13 @@ void application_entry(void *arg)
}
n = recv(connfd, buff, sizeof(buff), 0);
buff[n] = '\0';
printf("recv(%d) msg from client:%s\n", ++cnt, buff);
if(n > 0) {
buff[n] = '\0';
printf("recv(%d) msg from client:%s\n", ++cnt, buff);
} else {
printf("recv err(%d)\n", n);
}
if ((send(connfd, buff, strlen(buff), 0)) < 0) {
printf("send msg error: %s errno : %d\n", strerror(errno), errno);
break;
@@ -59,4 +64,3 @@ void application_entry(void *arg)
close(listenfd);
}