Small example of UDP sending and receiving broadcast messages under Linux system

Source: Internet
Author: User
Tags set socket socket error htons

[CPP]View Plaincopy
  1. Send Side
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <sys/socket.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <netdb.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <string.h>
  11. Using namespace std;
  12. int main ()
  13. {
  14. Setvbuf (stdout, NULL, _IONBF, 0);
  15. Fflush (stdout);
  16. int sock =-1;
  17. if ((sock = socket (af_inet, SOCK_DGRAM, 0)) = = = 1)
  18. {
  19. cout<<"Socket error" <<endl;
  20. return false;
  21. }
  22. const int opt = 1;
  23. //Set the socket to be a broadcast type,
  24. int nb = 0;
  25. NB = setsockopt (sock, Sol_socket, So_broadcast, (char *) &opt, sizeof (opt));
  26. if (nb = =-1)
  27. {
  28. cout<<"Set socket error ..." <<endl;
  29. return false;
  30. }
  31. struct sockaddr_in Addrto;
  32. Bzero (&addrto, sizeof (struct sockaddr_in));
  33. Addrto.sin_family=af_inet;
  34. Addrto.sin_addr.s_addr=htonl (Inaddr_broadcast);
  35. Addrto.sin_port=htons (6000);
  36. int nlen=sizeof (Addrto);
  37. While (1)
  38. {
  39. Sleep (1);
  40. //Send message from broadcast address
  41. char smsg[] = {"abcdef"};
  42. int ret=sendto (sock, smsg, strlen (smsg), 0, (sockaddr*) &addrto, Nlen);
  43. if (ret<0)
  44. {
  45. cout<<"Send error ..." <<ret<<endl;
  46. }
  47. Else
  48. {
  49. printf ("OK");
  50. }
  51. }
  52. return 0;
  53. }

[CPP]View Plaincopy
  1. Receiving End Http://blog.csdn.net/robertkun
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <sys/socket.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <netdb.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <string.h>
  11. Using namespace std;
  12. int main ()
  13. {
  14. Setvbuf (stdout, NULL, _IONBF, 0);
  15. Fflush (stdout);
  16. //Bind address
  17. struct sockaddr_in Addrto;
  18. Bzero (&addrto, sizeof (struct sockaddr_in));
  19. addrto.sin_family = af_inet;
  20. ADDRTO.SIN_ADDR.S_ADDR = htonl (Inaddr_any);
  21. Addrto.sin_port = htons (6000);
  22. //Broadcast address
  23. struct sockaddr_in from;
  24. Bzero (&from, sizeof (struct sockaddr_in));
  25. from.sin_family = af_inet;
  26. FROM.SIN_ADDR.S_ADDR = htonl (Inaddr_any);
  27. From.sin_port = htons (6000);
  28. int sock =-1;
  29. if ((sock = socket (af_inet, SOCK_DGRAM, 0)) = = = 1)
  30. {
  31. cout<<"Socket error" <<endl;
  32. return false;
  33. }
  34. const int opt = 1;
  35. //Set the socket to be a broadcast type,
  36. int nb = 0;
  37. NB = setsockopt (sock, Sol_socket, So_broadcast, (char *) &opt, sizeof (opt));
  38. if (nb = =-1)
  39. {
  40. cout<<"Set socket error ..." <<endl;
  41. return false;
  42. }
  43. if (bind (sock,struct sockaddr *) & (Addrto), sizeof (struct sockaddr_in)) = =-1)
  44. {
  45. cout<<"bind error ..." <<endl;
  46. return false;
  47. }
  48. int len = sizeof (sockaddr_in);
  49. char smsg[100] = {0};
  50. While (1)
  51. {
  52. //Receive messages from broadcast address
  53. int Ret=recvfrom (sock, smsg, 0, (struct sockaddr*) &from, (socklen_t*) &len);
  54. if (ret<=0)
  55. {
  56. cout<<"read error ..." <<sock<<endl;
  57. }
  58. Else
  59. {
  60. printf ("%s\t", smsg);
  61. }
  62. Sleep (1);
  63. }
  64. return 0;
  65. }


Since the test can be successful under the Linux virtual machine, the premise is to set the host in the same network segment, there is not forget to shut down the Linux firewall. You can set it using the Setup command.

(I can only send the test when the reception, tortured me for half a day, and then think of the Linux firewall problem.) )

For network card configuration of virtual machines, it is recommended to choose bridging mode. The NAT mode is restricted and may not receive broadcast messages.

Refer to the article online for specific reference.

I wish you success.

Small example of UDP sending and receiving broadcast messages under Linux system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.