Solved

Socket programming- broadcasting

Posted on 2000-04-12
6
757 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now