Hello,
I'm trying to setup a TCP connection to a remote TCP echo server (echo.u-blox.com : 195.34.89.241, port 7), for testing.
The socket is being opened, and I receive a welcome message. However, it seems as if the connection is closed afterwards:
Sent: AT+CFUN=1 Received: OK tcp_socket: 2 connect to 195.34.89.241 err: 0 Received: U-Blox AG TCP/UDP test service [31] Sent: Hello U-Blox [Bytes: 13] [Error: 0] Receive failed: [Bytes: 0] [Error: 0] Send failed: 54 Receive failed: [Bytes: -1] [Error: 54]
With UDP, everything is working just fine. What am I missing?
The proj.conf:
# # Copyright (c) 2019 Nordic Semiconductor ASA # # SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic # # General config CONFIG_ASSERT=y # Network CONFIG_NETWORKING=y CONFIG_NET_NATIVE=n CONFIG_NET_SOCKETS=y CONFIG_NET_SOCKETS_OFFLOAD=y CONFIG_NET_SOCKETS_POSIX_NAMES=y # BSD library CONFIG_BSD_LIBRARY=y # AT host library CONFIG_AT_HOST_LIBRARY=y CONFIG_UART_INTERRUPT_DRIVEN=y # Stacks and heaps CONFIG_MAIN_STACK_SIZE=4096 CONFIG_HEAP_MEM_POOL_SIZE=32768 # Testing changes CONFIG_GPIO=y
The code to set it up is as follows:
printk("Starting socket\n"); int at_socket_fd = socket(AF_LTE, SOCK_DGRAM, NPROTO_AT); if (at_socket_fd < 0) { printk("Socket err: %d, errno: %d\r\n", at_socket_fd, errno); } char *initCmds[1] = { "AT+CFUN=1" }; send(at_socket_fd, initCmds[0], strlen(initCmds[0]), 0); printk("Sent: %s\n", initCmds[0]); recv(at_socket_fd, recv_buf, sizeof(recv_buf), 0); printk("Received: %s\n", recv_buf); k_msleep(15000); int tcp_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // SOCK_DGRAM, IPPROTO_UDP printk("tcp_socket: %i\n", tcp_socket); struct sockaddr_in remote_addr; remote_addr.sin_family = AF_INET; remote_addr.sin_port = htons(TCP_PORT); inet_pton(AF_INET, TCP_HOST, &(remote_addr.sin_addr)); int err = connect(tcp_socket, (struct sockaddr *) &remote_addr, sizeof(remote_addr)); printk("connect to %s err: %i\n", TCP_HOST, err); int bytes_received = recv(tcp_socket, recv_buf, sizeof(recv_buf), 0); if (bytes_received > 0) { printk("Received: %s [%i]\n", recv_buf, bytes_received); } else { printk("Receive failed: %i\n", errno); } for (;;) { memset(recv_buf, 0, sizeof(recv_buf)); uint8_t cmd[] = "Hello U-Blox"; int bytes_sent = send(tcp_socket, cmd, sizeof(cmd), 0); if (bytes_sent < 0) { printk("Send failed: %i\n", errno); } else { printk("Sent: %s [Bytes: %i] [Error: %i]\n", cmd, bytes_sent, errno); } bytes_received = recv(tcp_socket, recv_buf, sizeof(recv_buf), 0); if (bytes_received > 0) { printk("Received: %s [Bytes: %i]\n", recv_buf, bytes_received); } else { printk("Receive failed: [Bytes: %i] [Error: %i]\n", bytes_received, errno); } k_msleep(5000); }