As both C and C++ standards require applications running under an OS to
return 'int', adapt that for Zephyr to align with those standard. This also
eliminates errors when building with clang when not using -ffreestanding,
and reduces the need for compiler flags to silence warnings for both clang
and gcc.
Most of these changes were automated using coccinelle with the following
script:
@@
@@
- void
+ int
main(...) {
...
- return;
+ return 0;
...
}
Approximately 40 files had to be edited by hand as coccinelle was unable to
fix them.
Signed-off-by: Keith Packard <keithp@keithp.com>
103 lines
1.9 KiB
C
103 lines
1.9 KiB
C
/*
|
|
* Copyright (c) 2017 Linaro Limited
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <errno.h>
|
|
|
|
#ifndef __ZEPHYR__
|
|
|
|
#include <netinet/in.h>
|
|
#include <sys/socket.h>
|
|
#include <arpa/inet.h>
|
|
#include <unistd.h>
|
|
|
|
#else
|
|
|
|
#include <zephyr/net/socket.h>
|
|
#include <zephyr/kernel.h>
|
|
|
|
#endif
|
|
|
|
#define BIND_PORT 4242
|
|
|
|
int main(void)
|
|
{
|
|
int serv;
|
|
struct sockaddr_in bind_addr;
|
|
static int counter;
|
|
|
|
serv = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
if (serv < 0) {
|
|
printf("error: socket: %d\n", errno);
|
|
exit(1);
|
|
}
|
|
|
|
bind_addr.sin_family = AF_INET;
|
|
bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
bind_addr.sin_port = htons(BIND_PORT);
|
|
|
|
if (bind(serv, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) < 0) {
|
|
printf("error: bind: %d\n", errno);
|
|
exit(1);
|
|
}
|
|
|
|
if (listen(serv, 5) < 0) {
|
|
printf("error: listen: %d\n", errno);
|
|
exit(1);
|
|
}
|
|
|
|
printf("Single-threaded TCP echo server waits for a connection on "
|
|
"port %d...\n", BIND_PORT);
|
|
|
|
while (1) {
|
|
struct sockaddr_in client_addr;
|
|
socklen_t client_addr_len = sizeof(client_addr);
|
|
char addr_str[32];
|
|
int client = accept(serv, (struct sockaddr *)&client_addr,
|
|
&client_addr_len);
|
|
|
|
if (client < 0) {
|
|
printf("error: accept: %d\n", errno);
|
|
continue;
|
|
}
|
|
|
|
inet_ntop(client_addr.sin_family, &client_addr.sin_addr,
|
|
addr_str, sizeof(addr_str));
|
|
printf("Connection #%d from %s\n", counter++, addr_str);
|
|
|
|
while (1) {
|
|
char buf[128], *p;
|
|
int len = recv(client, buf, sizeof(buf), 0);
|
|
int out_len;
|
|
|
|
if (len <= 0) {
|
|
if (len < 0) {
|
|
printf("error: recv: %d\n", errno);
|
|
}
|
|
break;
|
|
}
|
|
|
|
p = buf;
|
|
do {
|
|
out_len = send(client, p, len, 0);
|
|
if (out_len < 0) {
|
|
printf("error: send: %d\n", errno);
|
|
goto error;
|
|
}
|
|
p += out_len;
|
|
len -= out_len;
|
|
} while (len);
|
|
}
|
|
|
|
error:
|
|
close(client);
|
|
printf("Connection from %s closed\n", addr_str);
|
|
}
|
|
return 0;
|
|
}
|