Solved

Socket programming- broadcasting

Posted on 2000-04-12
6
767 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
linux rpm install 5 218
updating the repos in yum 4 112
Identify Linux loader 67 179
awk to variable in bash 2 43
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 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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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