diff --git a/examples/udp/Kconfig b/examples/udp/Kconfig index 62253544e..392ab6f73 100644 --- a/examples/udp/Kconfig +++ b/examples/udp/Kconfig @@ -511,4 +511,13 @@ config EXAMPLES_UDP_SERVERIPv6ADDR_8 This is the last of the 8-values. endif # EXAMPLES_UDP_IPv6 + +config EXAMPLES_SERVER_PORTNO + int "Server port number" + default 5471 + +config EXAMPLES_CLIENT_PORTNO + int "Client port number" + default 5472 + endif # EXAMPLES_UDP diff --git a/examples/udp/udp.h b/examples/udp/udp.h index 610da91c4..9316240be 100644 --- a/examples/udp/udp.h +++ b/examples/udp/udp.h @@ -83,7 +83,13 @@ # define PF_INETX PF_INET #endif -#define PORTNO 5471 +#ifndef CONFIG_EXAMPLES_SERVER_PORTNO +# define CONFIG_EXAMPLES_SERVER_PORTNO 5471 +#endif + +#ifndef CONFIG_EXAMPLES_CLIENT_PORTNO +# define CONFIG_EXAMPLES_CLIENT_PORTNO 5472 +#endif #define ASCIISIZE (0x7f - 0x20) #define SENDSIZE (ASCIISIZE+1) diff --git a/examples/udp/udp_client.c b/examples/udp/udp_client.c index 4d13a2649..18368c1a3 100644 --- a/examples/udp/udp_client.c +++ b/examples/udp/udp_client.c @@ -57,6 +57,59 @@ * Private Functions ****************************************************************************/ +static int create_socket(void) +{ + socklen_t addrlen; + int sockfd; + +#ifdef CONFIG_EXAMPLES_UDP_IPv4 + struct sockaddr_in addr; + + /* Create a new IPv4 UDP socket */ + + sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (sockfd < 0) + { + printf("client ERROR: client socket failure %d\n", errno); + return -1; + } + + /* Bind the UDP socket to a IPv4 port */ + + addr.sin_family = AF_INET; + addr.sin_port = HTONS(CONFIG_EXAMPLES_CLIENT_PORTNO); + addr.sin_addr.s_addr = HTONL(INADDR_ANY); + addrlen = sizeof(struct sockaddr_in); + +#else + struct sockaddr_in6 addr; + + /* Create a new IPv6 UDP socket */ + + sockfd = socket(AF_INET6, SOCK_DGRAM, 0); + if (sockfd < 0) + { + printf("client ERROR: client socket failure %d\n", errno); + return -1; + } + + /* Bind the UDP socket to a IPv6 port */ + + addr.sin6_family = AF_INET6; + addr.sin6_port = HTONS(CONFIG_EXAMPLES_CLIENT_PORTNO); + memset(addr.sin6_addr.s6_addr, 0, sizeof(struct in6_addr)); + addrlen = sizeof(struct sockaddr_in6); +#endif + + if (bind(sockfd, (FAR struct sockaddr *)&addr, addrlen) < 0) + { + printf("client ERROR: Bind failure: %d\n", errno); + return -1; + } + + return sockfd; +} + static inline void fill_buffer(unsigned char *buf, int offset) { int ch; @@ -92,10 +145,10 @@ void send_client(void) /* Create a new UDP socket */ - sockfd = socket(PF_INETX, SOCK_DGRAM, 0); + sockfd = create_socket(); if (sockfd < 0) { - printf("client socket failure %d\n", errno); + printf("client ERROR: create_socket failed %d\n"); exit(1); } @@ -111,12 +164,12 @@ void send_client(void) #ifdef CONFIG_EXAMPLES_UDP_IPv6 server.sin6_family = AF_INET6; - server.sin6_port = HTONS(PORTNO); + server.sin6_port = HTONS(CONFIG_EXAMPLES_SERVER_PORTNO); memcpy(server.sin6_addr.s6_addr16, g_server_ipv6, 8 * sizeof(uint16_t)); addrlen = sizeof(struct sockaddr_in6); #else server.sin_family = AF_INET; - server.sin_port = HTONS(PORTNO); + server.sin_port = HTONS(CONFIG_EXAMPLES_SERVER_PORTNO); server.sin_addr.s_addr = (in_addr_t)g_server_ipv4; addrlen = sizeof(struct sockaddr_in); #endif diff --git a/examples/udp/udp_cmdline.c b/examples/udp/udp_cmdline.c index 13695cf48..8eb93f8d8 100644 --- a/examples/udp/udp_cmdline.c +++ b/examples/udp/udp_cmdline.c @@ -50,7 +50,7 @@ ****************************************************************************/ #ifdef CONFIG_EXAMPLES_UDP_IPv6 -uint16_t g_server_ipv6[8] = +uint16_t g_server_ipv6[8] = { HTONS(CONFIG_EXAMPLES_UDP_SERVERIPv6ADDR_1), HTONS(CONFIG_EXAMPLES_UDP_SERVERIPv6ADDR_2), diff --git a/examples/udp/udp_server.c b/examples/udp/udp_server.c index f9fbbdc84..03233d68b 100644 --- a/examples/udp/udp_server.c +++ b/examples/udp/udp_server.c @@ -124,13 +124,13 @@ void recv_server(void) #ifdef CONFIG_EXAMPLES_UDP_IPv6 server.sin6_family = AF_INET6; - server.sin6_port = HTONS(PORTNO); + server.sin6_port = HTONS(CONFIG_EXAMPLES_SERVER_PORTNO); memset(&server.sin6_addr, 0, sizeof(struct in6_addr)); addrlen = sizeof(struct sockaddr_in6); #else server.sin_family = AF_INET; - server.sin_port = HTONS(PORTNO); + server.sin_port = HTONS(CONFIG_EXAMPLES_SERVER_PORTNO); server.sin_addr.s_addr = HTONL(INADDR_ANY); addrlen = sizeof(struct sockaddr_in);