Linux Socket Programming by Example - Warren Gay

< BACKCONTINUE >
159231153036212041242100244042145096184016146223183074028121223008110133020068228062210116144

Broadcasting Quotes via broadcast()

The server calls upon a function named broadcast() to share the information it has managed to obtain from Yahoo! with all interested local area network clients. The listing for this program is shown in Listing 18.4.

Example 18.4. bcast.cóThe broadcast() Function in bcast.c
 <$nopage>
001 1:   /* bcast.c:
002 2:    *
003 3:    * Broadcast Ticker Updates
004 4:    */
005 5:   #include "quotes.h"
006 6:
007 7:   void
008 8:   broadcast(
009 9:     int s,                  /* Socket */
010 10:    TickReq *quote,          /* Quote */
011 11:    struct sockaddr *bc_addr, /* addr */  <$nopage>
012 12:    socklen_t bc_len) {  /* addr len. */
013 13:      int z;                /* Status */
014 14:      char buf[2048];       /* Buffer */
015 15:      char *cp = buf;     /* Buf. ptr */
016 16:      int msglen;   /* Message length */
017 17:
018 18:      /*
019 19:       * Format a datagram for broadcast:
020 20:       */
021 21:      strcpy(buf,quote->ticker);
022 22:      cp = buf + strlen(buf) + 1;
023 23:      sprintf(cp,"%E",quote->last_trade);
024 24:      cp += strlen(cp) + 1;
025 25:      strcpy(cp,quote->date);
026 26:      cp += strlen(cp) + 1;
027 27:      strcpy(cp,quote->time);
028 28:      cp += strlen(cp) + 1;
029 29:      sprintf(cp,"%E",quote->change);
030 30:      cp += strlen(cp) + 1;
031 31:      sprintf(cp,"%E",quote->open_price);
032 32:      cp += strlen(cp) + 1;
033 33:      sprintf(cp,"%E",quote->high);
034 34:      cp += strlen(cp) + 1;
035 35:      sprintf(cp,"%E",quote->low);
036 36:      cp += strlen(cp) + 1;
037 37:      sprintf(cp,"%E",quote->volume);
038 38:      cp += strlen(cp) + 1;
039 39:
040 40:      msglen = cp - buf;
041 41:
042 42:      /*
043 43:       * Broadcast the datagram:
044 44:       */
045 45:      z = sendto(s,buf,msglen,0,bc_addr,bc_len);
046 46:      if ( z == -1 )
047 47:          msgf('e',"%s: sendto(2)",
048 48:              strerror(errno));
049 49:  }

The broadcast() function uses the following basic steps:

  1. The buffer buf[2048] is used to format the broadcast datagram (line 14). Pointer variable cp is used to build this datagram in stages (line 15).

  2. Line 21 starts building the datagram by copying the null terminated ticker symbol into buf[]. A new pointer for cp is computed to point after the null byte that follows the ticker symbol in buf[] (line 22).

  3. The last_trade value is formatted and placed after the null byte in the datagram (line 23). Again, cp is computed to point past the null byte that follows (line 24).

  4. Lines 25 to 38 repeat this procedure for all other data components to be placed into the datagram. Note that each component is a null-terminated string unto itself, within this datagram.

  5. The final length of the datagram is computed (line 40) and stored into msglen.

  6. The sendto(2) function is called upon to broadcast the datagram out to the socket s, to the broadcast address provided in bc_addr. If an error occurs, it is logged to the syslog facility (lines 47 to 48).

That covers the interesting network code for the server program. Now, let's take a look at the client side of things.

< BACKCONTINUE >

Index terms contained in this section

etching
     stock market quotes
            broadcast() function 2nd 3rd 4th
roadcast() function 2nd 3rd 4th
      examples 2nd 3rd
tock market quotes
      broadcast() function 2nd 3rd 4th
unctions
      broadcast() 2nd 3rd 4th
uotes (stock market)
      broadcast() function 2nd 3rd 4th
xamples
      broadcast() function 2nd 3rd