473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

UDP Broadcast Issue

Hi,

I have a problem in receiving UDP broadcast data. We have a simple application which listens for the UDP broadcast data from the external servers. We can receive the UDP broadcast data from one of the servers. For the other external server, our program does not receive any data in the sockets, but we can see the broadcast data in tcpdump capture.

Is there any possible reason on why our sockets cannot receive the UDP broadcast, but the data can be seen in the capture?

I have given our sample application code below. Please help.

Thanks,
Ravi.
Expand|Select|Wrap|Line Numbers
  1. #define PORT 6515
  2.  
  3. int main(int argc, char *argv[])
  4. {
  5. int sockfd;
  6. char buf[30];
  7. struct sockaddr_in sendaddr;
  8. struct sockaddr_in recvaddr;
  9. int numbytes;
  10. socklen_t addr_len;
  11. int broadcast=1;
  12. int reuse=1;
  13.  
  14. if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
  15. perror("socket");
  16. exit(1);
  17. }
  18. if ((setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
  19. &reuse, sizeof reuse)) == -1) {
  20. perror("setsockopt - SO_SOCKET ");
  21. exit(1);
  22. }
  23. if ((setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST,
  24. &broadcast, sizeof broadcast)) == -1) {
  25. perror("setsockopt - SO_SOCKET ");
  26. exit(1);
  27. }
  28. printf("Socket created\n");
  29. memset(&recvaddr, 0, sizeof recvaddr);
  30. recvaddr.sin_family = AF_INET;
  31. recvaddr.sin_port = htons(PORT);
  32. recvaddr.sin_addr.s_addr = INADDR_ANY;
  33. if (bind(sockfd, (struct sockaddr*)&recvaddr, sizeof recvaddr) == -1) {
  34. perror("bind");
  35. exit(1);
  36. }
  37. for (;;) {
  38. int n;
  39. fd_set set;
  40. struct timeval time_500ms = { 0, 500*1000 };
  41. FD_ZERO(&set);
  42. FD_SET(sockfd, &set);
  43.  
  44. n = select(sockfd+1, &set, NULL, NULL, &time_500ms);
  45. if (n < 0) {
  46. perror("select");
  47. break;
  48. }
  49. else if (n == 0) {
  50. printf("sleep(5)\n");
  51. sleep(5);
  52. }
  53. else if (!FD_ISSET(sockfd, &set)) {
  54. perror("FD_ISSET");
  55. break;
  56. }
  57. else {
  58. addr_len = sizeof sendaddr;
  59. if ((numbytes = recvfrom(sockfd, buf, sizeof buf, 0,
  60. (struct sockaddr *)&sendaddr, &addr_len)) > 0)
  61. {
  62. time_t now = time(NULL);
  63. printf("recvfrom: '%.*s' at %s\n", numbytes, buf, ctime(&now));
  64. }
  65. else
  66. perror("recvfrom");
  67. }
  68. }
  69. close(sockfd);
  70. return 0;
  71. }
Feb 4 '09 #1
0 2502

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Damir Hakimov | last post by:
Hi All! I'm writing a small programm which must send and service UDP broadcast packets. As start of it I looked at /usr/share/doc/python2.4/Examples/Demo/sockets/broadcast.py ...
6
by: pekspro | last post by:
I need some code that gets the address from a server. I read somewhere that you could do this by starting some broadcast server using UDP. The client should send an broadcast message, and when the...
8
by: Frank Esser | last post by:
Hello! I have got machines that answer a certain UDP broadcast request with certain information about them. Some years ago I wrote a VB6 application that just sent out this UDP broadcast...
2
by: gregory_may | last post by:
First the research links: IPv6 spec (look for 'jumbo payload'): http://www.cs-ipv6.lancs.ac.uk/ipv6/documents/rfcs/archive/rfc1883.txt IPv6 Sample C# Client/Server...
2
by: Shawn G. | last post by:
I'm going around in circles here and need some help. Scenario: I am using (have used) either the UDPClient class or Sockets class to broadcast some bytes to 255.255.255.255 (udp protocol). Upon...
2
by: Gunnar_Frenzel | last post by:
Hello, this might be an easy question, but I don't have any handy solution at hand. I have an application that is supposed to send UDP broadcast. So far so easy, I did: Socket sockSendBroadcast...
3
Sagittarius
by: Sagittarius | last post by:
Hi there. I have a problem concerning an UDP socket in C++ (Winsock). The next paragraphs is merely to explain the system I am working on. If U want to skip it, I have marked the question in...
9
by: Irmen de Jong | last post by:
Hello Sorry this might be a bit offtopic but I don't really know where else to post this question. If you could point me in the right direction that is much appreciated. I'm running into a...
2
by: OBones | last post by:
Hi all, I'm currently trying to receive replies to a UDP broadcast that I sent and I can't get it to work. I know the datagrams are sent as I see them with WireShark, but my C# code does not see...
1
by: raviskar | last post by:
Hi, I have a problem in receiving UDP broadcast data. We have a simple application which listens for the UDP broadcast data from the external servers. We can receive the UDP broadcast data from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.