Skip to content

Commit

Permalink
examples/sock_tcp_echo_client: add TCP echo client
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Sep 6, 2021
1 parent 61d9d20 commit 7350366
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
33 changes: 33 additions & 0 deletions examples/sock_tcp_echo_client/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# name of your application
APPLICATION = tcp_echo_client

# If no BOARD is found in the environment, use this default:
BOARD ?= native

# default to using GNRC
LWIP ?= 0

# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/../..

USEMODULE += sock_tcp
USEMODULE += netdev_default

USEMODULE += shell
USEMODULE += netutils

ifeq (1, $(LWIP))
USEMODULE += ipv6_addr
USEMODULE += lwip_ipv6_autoconfig
USEMODULE += lwip_netdev
else
USEMODULE += auto_init_gnrc_netif
USEMODULE += gnrc_ipv6
endif

# Comment this out to disable code in RIOT that does safety checking
# which is not needed in a production environment but helps in the
# development process:
DEVELHELP ?= 1

include $(RIOTBASE)/Makefile.include
31 changes: 31 additions & 0 deletions examples/sock_tcp_echo_client/Makefile.ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
BOARD_INSUFFICIENT_MEMORY := \
arduino-duemilanove \
arduino-leonardo \
arduino-mega2560 \
arduino-nano \
arduino-uno \
atmega328p \
atmega328p-xplained-mini \
atxmega-a1u-xpro \
bluepill-stm32f030c8 \
i-nucleo-lrwan1 \
msb-430 \
msb-430h \
nucleo-f030r8 \
nucleo-f031k6 \
nucleo-f042k6 \
nucleo-f303k8 \
nucleo-f334r8 \
nucleo-l011k4 \
nucleo-l031k6 \
nucleo-l053r8 \
samd10-xmini \
slstk3400a \
stk3200 \
stm32f030f4-demo \
stm32f0discovery \
stm32l0538-disco \
telosb \
waspmote-pro \
z1 \
#
88 changes: 88 additions & 0 deletions examples/sock_tcp_echo_client/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (C) 2016 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @{
*
* @file
* @author Martine Lenders <[email protected]>
* @author Benjamin Valentin <[email protected]>
*/

#include <stdio.h>

#include "net/sock/tcp.h"
#include "net/ipv6/addr.h"
#include "net/utils.h"
#include "shell.h"

static int _cmd_tcp_send(int argc, char **argv)
{
int res;
sock_tcp_t sock;
sock_tcp_ep_t remote;
netif_t *netif;
uint8_t buf[128];

if (argc < 4) {
printf("usage: %s <addr> <port> <data>\n", argv[0]);
return -1;
}

if (netutils_get_ipv6((ipv6_addr_t *)&remote.addr,
&netif, argv[1]) < 0) {
printf("can't resolve %s\n", argv[1]);
return -1;
}

remote.family = AF_INET6;
remote.netif = netif ? netif_get_id(netif) : 0;
remote.port = atoi(argv[2]);

if ((res = sock_tcp_connect(&sock, &remote, 0, 0)) < 0) {
printf("Error connecting sock: %s\n", strerror(-res));
return -1;
}

if ((res = sock_tcp_write(&sock, argv[3], strlen(argv[3]))) < 0) {
printf("Errored on write: %s\n", strerror(-res));
}
else {
if ((res = sock_tcp_read(&sock, &buf, sizeof(buf),
SOCK_NO_TIMEOUT)) <= 0) {
puts("Disconnected");
}
printf("Read: \"");
for (int i = 0; i < res; i++) {
printf("%c", buf[i]);
}
puts("\"");
}
sock_tcp_disconnect(&sock);

return 0;
}

static const shell_command_t shell_commands[] = {
{ "send", "send data over TCP", _cmd_tcp_send },
{ NULL, NULL, NULL }
};

int main(void)
{
puts("RIOT TCP client example application");

/* start shell */
puts("All up, running the shell now");
char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);

/* should be never reached */
return 0;
}
/** @} */

0 comments on commit 7350366

Please sign in to comment.