// 发送端 02.#include 03.#include 04.#include<sys/socket.h>05.#include 06.#include<sys/types.h>07.#include 08.#include<netinet/in.h>09.#include<arpa/inet.h>10.#include 11. 12. 13.using namespace std; 14. 15.int main() 16.{ 17. setvbuf(stdout, NULL, _IONBF, 0); 18. fflush(stdout); 19. 20. int sock = -1; 21. if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) 22. { 23. cout<<"socket error"<<endl; 24. return false; 25. } 26. 27. const int opt = 1; 28. //设置该套接字为广播类型, 29. int nb = 0; 30. nb = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&opt, sizeof(opt)); 31. if(nb == -1) 32. { 33. cout<<"set socket error..."<<endl; 34. return false; 35. } 36. 37. struct sockaddr_in addrto; 38. bzero(&addrto, sizeof(struct sockaddr_in)); 39. addrto.sin_family=AF_INET; 40. addrto.sin_addr.s_addr=htonl(INADDR_BROADCAST); 41. addrto.sin_port=htons(6000); 42. int nlen=sizeof(addrto); 43. 44. while(1) 45. { 46. sleep(1); 47. //从广播地址发送消息 48. char smsg[] = {"abcdef"}; 49. int ret=sendto(sock, smsg, strlen(smsg), 0, (sockaddr*)&addrto, nlen); 50. if(ret<0) 51. { 52. cout<<"send error...."<<ret<<endl; 53. } 54. else 55. { 56. printf("ok "); 57. } 58. } 59. 60. return 0; 61.} [cpp] view plaincopy 01.// 接收端 http://blog.csdn.net/robertkun 02. 03.#include 04.#include 05.#include<sys/socket.h>06.#include 07.#include<sys/types.h>08.#include 09.#include<netinet/in.h>10.#include<arpa/inet.h>11.#include 12. 13. 14.using namespace std; 15. 16.int main() 17.{ 18. setvbuf(stdout, NULL, _IONBF, 0); 19. fflush(stdout); 20. 21. // 绑定地址 22. struct sockaddr_in addrto; 23. bzero(&addrto, sizeof(struct sockaddr_in)); 24. addrto.sin_family = AF_INET; 25. addrto.sin_addr.s_addr = htonl(INADDR_ANY); 26. addrto.sin_port = htons(6000); 27. 28. // 广播地址 29. struct sockaddr_in from; 30. bzero(&from, sizeof(struct sockaddr_in)); 31. from.sin_family = AF_INET; 32. from.sin_addr.s_addr = htonl(INADDR_ANY); 33. from.sin_port = htons(6000); 34. 35. int sock = -1; 36. if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) 37. { 38. cout<<"socket error"<<endl; 39. return false; 40. } 41. 42. const int opt = 1; 43. //设置该套接字为广播类型, 44. int nb = 0; 45. nb = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&opt, sizeof(opt)); 46. if(nb == -1) 47. { 48. cout<<"set socket error..."<<endl; 49. return false; 50. } 51. 52. if(bind(sock,(struct sockaddr *)&(addrto), sizeof(struct sockaddr_in)) == -1) 53. { 54. cout<<"bind error..."<<endl; 55. return false; 56. } 57. 58. int len = sizeof(sockaddr_in); 59. char smsg[100] = {0}; 60. 61. while(1) 62. { 63. //从广播地址接受消息 64. int ret=recvfrom(sock, smsg, 100, 0, (struct sockaddr*)&from,(socklen_t*)&len); 65. if(ret<=0) 66. { 67. cout<<"read error...."<<sock<<endl; 68. } 69. else 70. { 71. printf("%s\t", smsg); 72. } 73. 74. sleep(1); 75. } 76. 77. return 0; 78.}
自已在Linux虚拟机下测试可以成功, 前提是要把主机设置在同一网段内, 还有就是不要忘记关闭Linux的防火墙.. 可以使用setup命令进行设置。 (我在测试的时候只能发送不收接收,折磨了我半天,后来才想到是Linux防火墙的问题。。) 关于虚拟机的网卡配置,建议选择桥接模式。NAT的模式的话,是受限制的,可能会收不到广播消息。 具体的参考网上的文章吧。。 祝你成功。。
Linux系统下UDP发送和接收广播
时间: 2024-10-14 04:39:14