Saturday, September 10, 2011

udp broadcast client server example

udp broadcast client server example : ccplusplus.com

Demonstrating the udp client server Broadcasts

With the server and client programs compiled, you are ready to begin. The first example sessions should work for everyone, with or without a network established. The demonstration will make use
of the local loopback interface that every Linux system should have available unless it has been disabled.

Files required:
  1. udp-broadcast-server.c
  2. udp-broadcast-client.c
  3. mkaddr.c 
The first step is to start the broadcast server:

Output

[root@rhel54x64 socket]#
[root@rhel54x64 socket]# ./udp-broadcast-server 127.255.255.255:9097 &
[1] 10658
[root@rhel54x64 socket]#

The session shows the starting of the stksrv server program on the host system pepper. The client session on the same host looked like this:

Output

[root@rhel54x64 socket]# ./udp-broadcast-client 127.255.255.255:9097

DJIA 10302.06 -1.24
NASDAQ 2766.86 +5.11
S&P 500 1285.48 +2.17
TSE 300 6897.99 +2.27
DJIA 10302.06 -1.24
NASDAQ 2766.86 +5.11
S&P 500 1285.73 +2.42
TSE 300 6897.99 +2.27
DJIA 10302.06 -1.24
NASDAQ 2766.86 +5.11
S&P 500 1286.00 +2.69
TSE 300 6897.99 +2.27
[CTRL+C]
[root@rhel54x64 socket]#

In the client session shown, the program was allowed to provide three quote updates before CTRL+C was typed to end its execution (your interrupt character might be different). Note that the broadcast address and port number must agree for both the client and server.

To prove to yourself that a broadcast is being performed, and not simply a point-to-point communication, you can start multiple instances of the client program. When this is done, they will
all update at approximately the same time.






-----------------------------------------------------------------
See Also:
-----------------------------------------------------------------
-----------------------------------------------------------------

15 comments:

  1. Hi, I am not able run your example I am always getting Success:Bad broadcast address

    ReplyDelete
  2. Hi, if anyone also facing the same issue then please correct *sv_addr = "127.0.0.:*" to
    *sv_addr = "127.0.0:*"
    in udp-broadcast-server.c

    ReplyDelete
    Replies
    1. Hi Yuvi,

      We will check and re verify the code. If there is any code change we will provide your the updated code link.

      Delete
  3. I am trying to run the client but it gives an error "cannot assign requested address :bind(2)"...
    how should i proceed?
    please if anyone could help me out.. its urgent...thanks..

    ReplyDelete
  4. I am trying to run the client code after the server is already running but if gives an error "can't assign requested address:bind(2)"..how should i proceed...if anyone could help me out..its urgent..thanks a lot...

    ReplyDelete
    Replies
    1. Hi Amol,

      The bind error shows that the address is already in use. You check with few commands that whether that IP address is already in use or not. In other you can change the IP address too. better if you can change the port also.

      Thanks
      ccplusplus-support

      Delete
  5. When i am compiling the code, do i compile the server and the client in separatre terminals or am i compiling them in the same terminal

    ReplyDelete
    Replies
    1. gcc udp_broadcast_server.c mkaddr.c -o udpServer
      gcc udp_broadcast_client.c mkaddr.c -o udpClient

      Delete
  6. I use CentOS 6. I made the changes listed below and now everything is bubbafied a-ok. The two errors being addressed here are "Success: Bad server address" and "sento(): Invalid argument".

    ( broadcast server C )
    z = mkaddr(
    &adr_srvr, /* Returned address */
    &len_srvr, /* Returned length */
    sv_addr, /* Input string addr */
    "udp"); /* UDP protocol */
    + adr_srvr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    ...
    z = mkaddr(
    &adr_bc, /* Returned address */
    &len_bc, /* Returned length */
    bc_addr, /* Input string addr */
    "udp"); /* UDP protocol */
    + adr_bc.sin_addr.s_addr = htonl(INADDR_BROADCAST);
    + adr_bc.sin_port = (in_port_t)htons(9097);

    ( broadcast client C )
    z = mkaddr(&adr,
    &len_inet,
    bc_addr,
    "udp");
    + adr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
    + adr.sin_port = (in_port_t)htons(9097);

    ReplyDelete
    Replies
    1. Thanks very much for code changes....

      Delete
    2. The server does not run and shows "invalid argument : sendto()". How to solve this problem?

      Delete
  7. I altered the initial "adr_srvr" setting in the patch above to the following, and I am now able to receive broadcasts from/to different hosts on a LAN (using the same subnet).

    The code above will restrict the broadcast to the local machine due to the 'source address' being bound to the local loopback device. Note that a INADDR_ANY alone will not suffice for LAN broadcast without also ensuring family is AF_INET and port is set.

    ( broadcast server C )
    z = mkaddr(
    &adr_srvr, /* Returned address */
    &len_srvr, /* Returned length */
    sv_addr, /* Input string addr */
    "udp"); /* UDP protocol */
    +adr_srvr.sin_family = AF_INET;
    +adr_srvr.sin_port = (in_port_t)htons(9097);
    +adr_srvr.sin_addr.s_addr = INADDR_ANY;

    ReplyDelete
  8. I'm glad I found this web site, I couldn't find any knowledge on this matter prior to.Also operate a site and if you are ever interested in doing some visitor writing for me if possible feel free to let me know, im always look for people to check out my web site. npb중계

    ReplyDelete
  9. I don't understand how this code was never fixed
    both these lines are erroneous in the broadcast server
    *sv_addr = "127.0.0.:*",
    *bc_addr = "127.255.255.2 9097";

    *sv_addr = "127.0.0.1:*",
    *bc_addr = "127.255.255.2:9097";

    are syntactically correct. for the mkaddr function they wrote.
    I love it when people have so much hubris they don't even bother testing their example code.

    ReplyDelete