This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

recv() blocks forever

Dear community

In an attempt to implement an LTE TCP client, I'm stuck at the problem, that `recv()` doesn't return after the server has closed the connection.

My test implementation does a simple http request (source code attached at the end of the message):

  1. Connect to a web server
  2. Send "GET / HTTP/1.0\r\n\r\n"
  3. Shutdown (SHUT_WR)
  4. Receive while return value of `recv()` > 0.
  5. Close

It does everything nicely except it never arrives at stage 5.

According to https://pubs.opengroup.org/onlinepubs/9699919799/functions/recv.html I would expect it to return '0' after the server has shut down the connection.

Any suggestions on what could be the problem here would be greatly appreciated!

Environment:
-- Zephyr version: 2.3.0-rc1
-- Board: nrf9160dk_nrf9160
-- West version: 0.7.2
-- DTC version: 1.4.7
-- Toolchain: gcc-arm-none-eabi-7-2018q2

Code used for testing:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int test_tcp(void) {
int ret = -1;
int fd = -1;
struct addrinfo *addr_res;
char recv_buf[10];
/**
* Init modem & link control
*/
ret = at_cmd_write("AT+CMEE=1", NULL, 0, NULL);
if (ret != 0) {
printk("at_cmd_write(AT+CMEE=1) failed with %d\n", ret);
return 0;
}
ret = lte_lc_init();
if (ret != 0) {
printk("lte_lc_init() failed with %d\n", ret);
return 0;
}
/**
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

*** Booting Zephyr OS build v2.3.0-rc1-ncs1 ***
+CEREG: 2,"9D08","013A2305",7,0,0,"11100000","11100000"
+CEREG: 5,"9D08","013A2305",7,,,"11100000","11100000"
--------- LTE CONNECTED ---------
--------- TCP CONNECTED ---------
--------- START RECEIVING ---------
HTTP/1.1 302 Moved Temporarily
Server: awselb/2.0
Date: Fri, 07 Aug 2020 17:31:50 GMT
Content-Type: text/html
Content-Length: 126
Connection: close
Location: prod-swagger-oss-1833484297.us-east-1.elb.amazonaws.com:443/

<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
</body>
</html>