Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Socket programming- broadcasting

Posted on 2000-04-12
6
Medium Priority
?
794 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

636 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