Solved

Socket programming- broadcasting

Posted on 2000-04-12
6
780 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
Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 

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 200 total points
ID: 2720801
did the my example above work for you?

dennis
0

Featured Post

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

728 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