Advertisement

01.10.2008 at 08:12PM PST, ID: 23074851
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Sending Broadcast packets using UDP in Linux/C

Tags: C, UDP Broadcasting
Hi, here is a code in which we are trying to send a simple broadcast packet to all the computers on the network. But this is not working properly. The sender and receiver programs seem to get struck inside sendto and recvfrom functions respectively.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
SENDER CODE:
____________________
#define PORT 2080
#define DEST_ADDR "255.255.255.255"
 
int main(int argc, char *argv[])
{
	int sockfd;
	int broadcast=1;
	struct sockaddr_in sendaddr;
	struct sockaddr_in recvaddr;
	int numbytes;
	
	if((sockfd = socket(PF_INET,SOCK_DGRAM,0)) == -1)
	{
		perror("sockfd");
		exit(1);
	}
	
	if((setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,
				&broadcast,sizeof broadcast)) == -1)
	{
		perror("setsockopt - SO_SOCKET ");
		exit(1);
	}
	
   /*if((setsockopt(sockfd,SOL_SOCKET,SO_DONTROUTE,
   			&broadcast,sizeof broadcast)) == -1)
   {
      perror("setsockopt - SO_DONTROUTE");
      exit(1);
   }*/
        	
	sendaddr.sin_family = AF_INET;
	sendaddr.sin_port = PORT;
	sendaddr.sin_addr.s_addr = INADDR_ANY;
	memset(sendaddr.sin_zero,'\0',sizeof sendaddr.sin_zero);
	
	if(bind(sockfd, (struct sockaddr*) &sendaddr, sizeof sendaddr) == -1)
	{
		perror("bind");
		exit(1);
	}
	
	recvaddr.sin_family = AF_INET;
	recvaddr.sin_port = PORT;
	recvaddr.sin_addr.s_addr = inet_addr(DEST_ADDR);
	memset(recvaddr.sin_zero,'\0',sizeof recvaddr.sin_zero);
	
	while((numbytes = sendto(sockfd, "abcd", 4 , 0,
        (struct sockaddr *)&recvaddr, sizeof recvaddr)) != -1) 
   {
   		printf("Sent a packet");
   		sleep(1000);
   }
        perror("sendto");
        exit(1);
   	
	
	close(sockfd);
	
	return 0;
}
____________________
RECEIVER CODE:
____________________
#include "packetize.h"
 
#define PORT 2080
#define DEST_ADDR "10.50.41.208" //Server addr
 
int main(int argc, char *argv[])
{
	int sockfd;
	char buf[10];
	struct hostent *he;
	struct sockaddr_in sendaddr;
	//struct sockaddr_in recvaddr;
	int numbytes;
	int addr_len;
	
	if((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
	{
		perror("socket");
		exit(1);
	}
	
	printf("Socket created\n");
	
	if ((he=gethostbyname(DEST_ADDR)) == NULL) 
	{   // get the host info
	   herror("gethostbyname");
   	exit(1);
   }
 
	printf("Host found\n");
 
	sendaddr.sin_family = AF_INET;
	sendaddr.sin_port = PORT;
	sendaddr.sin_addr = *((struct in_addr *)he->h_addr);
	memset(sendaddr.sin_zero,'\0', sizeof sendaddr.sin_zero);
	
	addr_len = sizeof sendaddr;
	if ((numbytes = recvfrom(sockfd, buf, sizeof buf, 0,
              (struct sockaddr *)&sendaddr, (socklen_t *)&addr_len)) == -1) 
	{
   	perror("recvfrom");
      exit(1);
   }
	
	printf("%s",buf);
	
	return 0;
}
____________________
Start your free trial to view this solution
Question Stats
Zone: Networking
Question Asked By: mgh_mgharish
Solution Provided By: duncan_roe
Participating Experts: 2
Solution Grade: A
Views: 284
Translate:
Loading Advertisement...
01.11.2008 at 01:47AM PST, ID: 20635109

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.11.2008 at 03:55AM PST, ID: 20635485

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.11.2008 at 05:53AM PST, ID: 20636182

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.13.2008 at 03:16AM PST, ID: 20647301

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.22.2008 at 08:48PM PST, ID: 20720884

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
01.11.2008 at 01:47AM PST, ID: 20635109

Rank: Guru

I don't think you want the sending address to be INADDR_ANY - I would think the broadcast address for the network would be more appropriate. You could hard-code it for testing (i.e. inet_addr(hardcoded dotted address)) and write the code to figure out what it is later.
 
01.11.2008 at 03:55AM PST, ID: 20635485
duncan_roe, thanks for looking into this.

sendaddr is used only for binding purpose. recvaddr is used for sending the packets using sendto().
 
01.11.2008 at 05:53AM PST, ID: 20636182

Rank: Guru

Ok I see DEST_ADDR now. But I think you should use the Broadcast Address for your network, as ifconfig will give you:

19:01:31$ ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:76:6D:BC:E1  
          inet addr:10.255.255.1  Bcast:10.255.255.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:38790 errors:0 dropped:0 overruns:0 frame:0
          TX packets:38520 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:8229514 (7.8 Mb)  TX bytes:3361119 (3.2 Mb)
          Interrupt:16

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:139 errors:0 dropped:0 overruns:0 frame:0
          TX packets:139 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:137520 (134.2 Kb)  TX bytes:137520 (134.2 Kb)

The broadcast address for my LAN is 10.255.255.255
Accepted Solution
 
01.13.2008 at 03:16AM PST, ID: 20647301

Rank: Guru

Most problems where in receiver code:
1) You should use htons() to defind sin_port port numbers
2) You don't have to use gethostname() when using IP address. Moreover you shouldn't use any predefined source IP address in the receiver.
3) Most impotent, you should 'bind' before 'recvfrom'

In sender code is also slightly fixed.

Working code is in attachment.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
sender.c:
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
 
 
#define PORT 2080
// #define SRC_ADDR "172.16.1.120"
#define DEST_ADDR "255.255.255.255"
 
int main(int argc, char *argv[])
{
        int sockfd;
        int broadcast=1;
        struct sockaddr_in sendaddr;
        struct sockaddr_in recvaddr;
        int numbytes;
 
        if((sockfd = socket(PF_INET,SOCK_DGRAM,0)) == -1)
        {
                perror("sockfd");
                exit(1);
        }
 
       if((setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,
                                &broadcast,sizeof broadcast)) == -1)
        {
                perror("setsockopt - SO_SOCKET ");
                exit(1);
        }
 
        sendaddr.sin_family = AF_INET;
        sendaddr.sin_port = htons(PORT);
        sendaddr.sin_addr.s_addr = INADDR_ANY;
        memset(sendaddr.sin_zero,'\0',sizeof sendaddr.sin_zero);
 
        if(bind(sockfd, (struct sockaddr*) &sendaddr, sizeof sendaddr) == -1)
        {
                perror("bind");
                exit(1);
        }
 
        recvaddr.sin_family = AF_INET;
        recvaddr.sin_port = htons(PORT);
        recvaddr.sin_addr.s_addr = inet_addr(DEST_ADDR);
        memset(recvaddr.sin_zero,'\0',sizeof recvaddr.sin_zero);
 
        while((numbytes = sendto(sockfd, "abcd", 4 , 0,
        (struct sockaddr *)&recvaddr, sizeof recvaddr)) != -1)
   {
                printf("Sent a packet");
                sleep(1000);
   }
        perror("sendto");
        exit(1);
 
 
        close(sockfd);
 
        return 0;
}
 
receiver.c:
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
 
#define PORT 2080
#define DEST_ADDR "255.255.255.255" //broadcast addr
 
int main(int argc, char *argv[])
{
        int sockfd;
        char buf[10];
        struct sockaddr_in sendaddr;
        struct sockaddr_in recvaddr;
        int numbytes;
        int addr_len;
        int broadcast=1;
 
        if((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
        {
                perror("socket");
                exit(1);
        }
       if((setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,
                                &broadcast,sizeof broadcast)) == -1)
        {
                perror("setsockopt - SO_SOCKET ");
                exit(1);
        }
 
        printf("Socket created\n");
 
        sendaddr.sin_family = AF_INET;
        sendaddr.sin_port = htons(PORT);
        sendaddr.sin_addr.s_addr = INADDR_ANY;
        memset(sendaddr.sin_zero,'\0', sizeof sendaddr.sin_zero);
 
 
        recvaddr.sin_family = AF_INET;
        recvaddr.sin_port = htons(PORT);
        recvaddr.sin_addr.s_addr = INADDR_ANY;
        memset(recvaddr.sin_zero,'\0',sizeof recvaddr.sin_zero);
        if(bind(sockfd, (struct sockaddr*) &recvaddr, sizeof recvaddr) == -1)
        {
                perror("bind");
                exit(1);
        }
 
 
        addr_len = sizeof sendaddr;
        if ((numbytes = recvfrom(sockfd, buf, sizeof buf, 0,
              (struct sockaddr *)&sendaddr, (socklen_t *)&addr_len)) == -1)
        {
        perror("recvfrom");
      exit(1);
   }
 
        printf("%s",buf);
 
        return 0;
}
Open in New Window
Assisted Solution
 
01.22.2008 at 08:48PM PST, ID: 20720884

Rank: Guru

Also check your firewall on both computers, it should allow broadcast UDP.
I have tested 'sender' on Linux, 'receiver' on FreeBSD plugged into the same switch and no firewall.
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628