example/nettest: add receive broadcast example
add new example for showing how to receive broadcast data
@@ -261,6 +261,71 @@ return_with_close:
close(sock);
}
+/* ------------------------------------------------------------ */
+/* */
+/* Receive Broadcast example. */
+/* */
+/* ------------------------------------------------------------ */
+void broadcast_receive(uint32_t num_packets)
+{
+ int sockfd;
+ int is_broadcast = 1;
+ int count;
+ int nbytes;
+ char databuf[256];
+ struct sockaddr_in recvaddr;
+ struct sockaddr_in sendaddr;
+ socklen_t addrlen = sizeof(struct sockaddr_in);
+
+ printf("[BRSERVER] broadcast receive start!\n");
+
+ sockfd = socket(AF_INET, SOCK_DGRAM, 0);
+ if (sockfd < 0) {
+ printf("[BRSERVER] socket create fail err(%d)\n", errno);
+ return;
+ }
+
+ if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &is_broadcast, sizeof(is_broadcast)) < 0) {
+ printf("[BRSERVER] set socket option fail err(%d)\n", errno);
+ goto return_with_close;
+ }
+
+ memset(&recvaddr, 0, sizeof(recvaddr));
+ recvaddr.sin_family = AF_INET;
+ recvaddr.sin_addr.s_addr = htonl(INADDR_ANY);
+ recvaddr.sin_port = htons(g_app_target_port);
+
+ if (bind(sockfd, (struct sockaddr *)&recvaddr, sizeof(recvaddr)) < 0) {
+ printf("[BRSERVER] bind fail err(%d)\n", errno);
+ goto return_with_close;
+ }
+
+ count = 0;
+
+ while (1) {
+ memset(databuf, 0, sizeof(databuf));
+
+ nbytes = recvfrom(sockfd, databuf, sizeof(databuf), 0, (struct sockaddr *)&sendaddr, (socklen_t *)&addrlen);
+ if (nbytes <= 0) {
+ printf("[BRSERVER] receive data fail err(%d)\n", errno);
+ goto return_with_close;
+ }
+ count++;
+ printf("[BRSERVER] received data from %s #%d : %s\n", inet_ntoa(sendaddr.sin_addr), count, databuf);
+
+ if (num_packets == 0) {
+ continue;
+ }
+ if (count >= num_packets) {
+ printf("[BRSERVER] exit BRSERVER as received sufficient packets for testing\n");
+ break;
+ }
+ }
+
+return_with_close:
+ close(sockfd);
+ return;
+}
/* ------------------------------------------------------------ */
/* */
@@ -807,6 +872,8 @@ int nettest_main(int argc, char *argv[])
tcp_server_thread(num_packets_to_process);
} else if (proto == NT_UDP) {
udp_server_thread(num_packets_to_process);
+ } else if (proto == NT_BROADCAST) {
+ broadcast_receive(num_packets_to_process);
} else if (proto == NT_MULTICAST) {
if (argc != 7) {
goto err_with_input;
