?
Solved

Socket programming- broadcasting

Posted on 2000-04-12
6
Medium Priority
?
789 Views
Last Modified: 2008-03-04
Hello ,
    I am writing a client/server application using sockets on Linux
platform. I am using UDP/IP protocol. The server broadcasts message for
every
interval of time. I can run this server on any machine.
I have used "setsockopt" function with level "SOL_SOCKET" and optname as

"SO_BROADCAST". I am using the broadcast address as "255.255.255.255".
I am using "sendto" to send this address.

    I want to know how to receive the packet sent by this at the client
running on another machine. I will not know the server address, but i
have set the server and client port numbers as 2000 and 2001
respectively.
    I want to send a broadcast packet from the server to all the clients

who are listening at port 2000 and they should receive the packet sent
by the server at port 2001.
    Should i explicitly set up the ports using some functions?
I am referring W. Richard Stevens "Unix Network Programming". There is
no example that uses broadcasting.
0
Comment
Question by:ghins76
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 4

Expert Comment

by:kiffney
ID: 2708837
I don't quite understand about your ports - you mention the 'client port' is 2001 but you later say the clients will 'listen' on 2000.  This isn't really a programming answer - but it's how you can use the system to handle port listening for you.  Let's say you want your client to listen on 2000.  You don't have to do anything special to set up the port.  You can just edit /etc/services, and add a line like

2000       mylistener/udp

and mylistener will be the name of your client service.

Then in /etc/inetd.conf, add a line

mylistener dgram  udp  wait  root  /usr/sbin/myclient

which will make inetd call your program myclient when a UDP packet arrives on port 2000.  If you don't do this, your program will have to run as a daemon and listen on that port explicitly.

0
 
LVL 2

Expert Comment

by:munsie
ID: 2709815
well, in defense of the original question, it may be very well that he does want to sit on the port himself instead of having inetd call him everytime.

depending on the application, the latency of having inetd call you when a packet comes in may be too much --or-- the overhead of calling his program every single time a packet comes in may be too much.  inetd works fine for protocols that don't get much activity and aren't speed critical.

dennis
0
 
LVL 2

Expert Comment

by:munsie
ID: 2709921
now to answer your question.. hopefully

when you send your packet using sendto() you specify the address and port to send it to... in your case, your sending to the broadcast address and your port... since your server is listening on port 2000, you want to send to 2000.

i don't know where the 2001 port came in, but if you're sending to 2001 and listening on 2000, you won't get any data there.

as long as everything else is setup right, this should be the only change you need.

If you need some sample code, let me know

dennis

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:ghins76
ID: 2715055
Please post the sample code.
0
 
LVL 2

Expert Comment

by:munsie
ID: 2716395
my terminology may be a bit backwards from yours... but this should get you started.  Also, I broadcast over my internal network instead of globally as in your example.

here is my client program... it just broadcasts out once a second the value of a counter :

--------

#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

int main(int      argc,
       char      *argv[])
{
      int      sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
      if (!sock)
      {
            perror("socket");
            return -1;
      }

      struct sockaddr_in      servaddr;
      bzero(&servaddr, sizeof(servaddr));
      servaddr.sin_family = AF_INET;
      servaddr.sin_port = htons(2000);
      if (inet_pton(AF_INET, "192.168.242.255", &servaddr.sin_addr) < 0)
      {
            perror("inet_pton");
            return -1;
      }

      const int      on = 1;
      if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0)
      {
            perror("setsockopt");
            return -1;
      }

      for(unsigned long count = 0; ; count++)
      {
            if (sendto(sock, &count, sizeof(count), 0, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
                  perror("sendto");

            sleep(1);
      }

      return 0;

} // of main()

----

here is my server code... it just listens on port 2000 and prints the value it recieves out:

#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

int main(int      argc,
       char      *argv[])
{
      int      sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);

      if (!sock)
      {
            perror("socket");
            return -1;
      }

      struct sockaddr_in      servaddr;
      bzero(&servaddr, sizeof(servaddr));
      servaddr.sin_family = AF_INET;
      servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
      servaddr.sin_port = htons(2000);

      if(bind(sock, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
      {
            perror("bind");
            return -1;
      }

      for( ; ; )
      {
            unsigned long      count;
            struct sockaddr_in      cliaddr;

            if (recv(sock, (void *) &count, sizeof(count), 0) < 0)
                  perror("recv");

            else
                  printf("%u\n", count);
      }

      return 0;

} // of main()

----

hope this helps

dennis
0
 
LVL 2

Accepted Solution

by:
munsie earned 800 total points
ID: 2720801
did the my example above work for you?

dennis
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question