370 likes | 531 Views
Broadcasting and UDP. Network Lab. 남 재 준. 목 차. Broadcast UDP 소켓 설치 소켓을 사용하여 broadcast message 보내기 소켓을 사용하여 broadcast message 받기. Ethernet Interface 를 위한 broadcast address. Broadcast Address. ifconfig eth0. # ifconfig eth0 eth0 Link encap:Ehternet Hwaddr 00:A0:4B:06:F4:8D
E N D
Broadcasting and UDP Network Lab. 남 재 준
목 차 • Broadcast UDP 소켓 설치 • 소켓을 사용하여 broadcast message 보내기 • 소켓을 사용하여 broadcast message 받기
Ethernet Interface를 위한 broadcast address Broadcast Address • ifconfig eth0 # ifconfig eth0 eth0 Link encap:Ehternet Hwaddr 00:A0:4B:06:F4:8D inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0 UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1 RX packets:1955 error:0 dropped:0 overruns:0 frame:31 collision:0 txqueuelen:100 Interrupt:9 Bass address:0xe400 #
Network ID Host ID Broadcast Address • ifconfig eth0 # ifconfig eth0 eth0 Link encap:Ehternet Hwaddr 00:A0:4B:06:F4:8D inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0 UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1 RX packets:1955 error:0 dropped:0 overruns:0 frame:31 collision:0 txqueuelen:100 Interrupt:9 Bass address:0xe400 # 192.168.0.255 - C Class address
Broadcast Address • 255.255.255.255 • broadcasting을 위해서 사용될 수 있다. • 대량의 broadcast를 제한하고 있다. • 개념정의가 미비한 상태 • 모든 network interface들을 위해 사용 • 여러 interface 중 단 하나를 선택 • 일반적인 broadcast address 사용이 제지 된다.
Broadcast 하기 위해 • Interface 이름 결정 • interface의 broadcast address 결정 • broadcast address를 broadcast • 추가된 interface에 반복
mkaddr.c 확장 $ diff ../ch.11/mkaddr.c mkaddr.c 99,102c99,100 < ap.>sin_addr.s_addr = < inet_addr(host_part); < if (ap->sin_addr.s_addr == INADDR_NONE) .... > if (!inet_aton(host_part, > &ap->sin_addr) ) $ Invalid address INADDR_NONE 255.255.255.255 (32bit convert) INADDR_NONE General broadcast address (255.255.255.255)와 bad IP address의 구별을 위해
Broadcasting from a Server Command-line Create Server address Create Broadcast address Update by gen_quote( ) Create Socket Index Buffer Bind serve address Send by sendto( ) Initialize Index Time delay Sleep ( )
Broadcasting from a Server • Socket은 SO_BROADCAST option을 가지고 있어야 한다. • sendto( ) 함수의 destination address Broadcast address
Receiving Broadcasting Command-line Create Socket Broadcast address is Formed Begin broadcast receiving loop Bind Broadcast address Receive broadcast Write Broadcast Info.
Receiving Broadcasting • Broadcast를 수신하기 위해서 client program은 Socket에 bind된 address를 가지고 있어야 한다. • SO_REUSEADDR • multiple client가 같은 host에 같은 broadcast address로부터 수신하도록 허락
Broadcast Demo. @pepper $ ./stksrv 127.255.255.255:9097 & [1] 756 @pepper $
Broadcast Demo. $ ./gquotes 127.255.255.255:9097 DJIA 10302.06 -1.24 NASDQ 2766.86 +5.11 S&P 500 1285.48 +2.17 TSE 300 6897.99 +2.27 DJIA 10302.06 -1.24 NASDQ 2766.86 +5.11 S&P 500 1285.48 +2.17 TSE 300 6897.99 +2.27 DJIA 10302.06 -1.24 NASDQ 2766.86 +5.11 S&P 500 1285.48 +2.17 TSE 300 6897.99 +2.27 [CTRL+C] @pepper $
Broadcast Demo. (to network) @pepper $ ./stksrv 192.168.0.255:9097 '192.168.0.1:*' & [2] 815 @pepper $
Broadcast Demo. (to network) @pepper (192.168.0.1) $ ./gquotes 192.168.0.255:9097 DJIA 10304.73 +1.43 NASDAQ 2761.19 -0.56 S&P 500 1283.31 +0.00 TSE 300 6895.82 +0.10 DJIA 10304.73 +1.43 NASDAQ 2761.19 -0.56 S&P 500 1283.31 +0.00 TSE 300 6896.35 +0.63 @slug (192.168.0.2) $ ./gquotes 192.168.0.255:9097 DJIA 10309.80 +6.50 NASDQ 2767.29 +5.54 S&P 500 1286.06 +2.75 TSE 300 6897.75 +2.03 DJIA 10309.80 +6.50 NASDQ 2767.29 +5.54 S&P 500 1286.06 +2.75 TSE 300 6897.67 +1.95
/* stksrv.c: * * Example Stock Index Broadcast : */ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <time.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #ifndef TRUE #define TRUE 1 #define FALSE 0 #endif extern int mkaddr( void *addr, int *addrlen, char *str_addr, char *protocol); #define MAXQ 4 static struct { char *index; int start; int volit; int current; } quotes[ ] = { { "DJIA", 1030330, 375 }, { "NASDAQ", 276175, 125} , { "S&P 500", 128331, 50 }, { "TSE 300", 689572, 75 }, } ; /* * Initialize : */ static void initialize(void) short x; time_t td; /* * Seed the random number generator : */ time(&td); srand((int)td); for ( x=0; x < MAXQ; ++x ) quotes[x].current = quotes[x].start; /* * Randomly change one index quotation : */ static void gen_quote(void) short x; /* Index */ short v; /* Volatility of index */ short h; /* Half of v */ short r; /* Random change */ x = rand() % MAXQ; v = quotes[x].volit; h = (v / 2) - 2; r = rand() % v; if ( r < h ) r = -r; quotes[x].current += r; /* * This function reports the error and * exits back to the shell : */ static void bail(const char *on_what) fputs(strerror(errno),stderr); fputs(": ",stderr); fputs(on_what,stderr); fputc('',stderr); exit(1); Server program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server #define MAXQ 4 static struct { char *index; int start; int volit; int current; } quotes[ ] = { { "DJIA", 1030330, 375 }, { "NASDAQ", 276175, 125} , { "S&P 500", 128331, 50 }, { "TSE 300", 689572, 75 }, } ; Stock Market Index table Simulation을 위한 data 구성
Server program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server /* stksrv.c: * * Example Stock Index Broadcast : */ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <time.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #ifndef TRUE #define TRUE 1 #define FALSE 0 #endif extern int mkaddr( void *addr, int *addrlen, char *str_addr, char *protocol); #define MAXQ 4 static struct { char *index; int start; int volit; int current; } quotes[ ] = { { "DJIA", 1030330, 375 }, { "NASDAQ", 276175, 125} , { "S&P 500", 128331, 50 }, { "TSE 300", 689572, 75 }, } ; /* * Initialize : */ static void initialize(void) short x; time_t td; /* * Seed the random number generator : */ time(&td); srand((int)td); for ( x=0; x < MAXQ; ++x ) quotes[x].current = quotes[x].start; /* * Randomly change one index quotation : */ static void gen_quote(void) short x; /* Index */ short v; /* Volatility of index */ short h; /* Half of v */ short r; /* Random change */ x = rand() % MAXQ; v = quotes[x].volit; h = (v / 2) - 2; r = rand() % v; if ( r < h ) r = -r; quotes[x].current += r; /* * This function reports the error and * exits back to the shell : */ static void bail(const char *on_what) fputs(strerror(errno),stderr); fputs(": ",stderr); fputs(on_what,stderr); fputc('',stderr); exit(1); static void initialize(void) { short x; time_t td; time(&td); srand((int)td); for ( x=0; x < MAXQ; ++x ) quotes[x].current = quotes[x].start; } Simulation 초기화 Random number 산출
Server program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server /* stksrv.c: * * Example Stock Index Broadcast : */ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <time.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #ifndef TRUE #define TRUE 1 #define FALSE 0 #endif extern int mkaddr( void *addr, int *addrlen, char *str_addr, char *protocol); #define MAXQ 4 static struct { char *index; int start; int volit; int current; } quotes[ ] = { { "DJIA", 1030330, 375 }, { "NASDAQ", 276175, 125} , { "S&P 500", 128331, 50 }, { "TSE 300", 689572, 75 }, } ; /* * Initialize : */ static void initialize(void) short x; time_t td; /* * Seed the random number generator : */ time(&td); srand((int)td); for ( x=0; x < MAXQ; ++x ) quotes[x].current = quotes[x].start; /* * Randomly change one index quotation : */ static void gen_quote(void) short x; /* Index */ short v; /* Volatility of index */ short h; /* Half of v */ short r; /* Random change */ x = rand() % MAXQ; v = quotes[x].volit; h = (v / 2) - 2; r = rand() % v; if ( r < h ) r = -r; quotes[x].current += r; /* * This function reports the error and * exits back to the shell : */ static void bail(const char *on_what) fputs(strerror(errno),stderr); fputs(": ",stderr); fputs(on_what,stderr); fputc('',stderr); exit(1); static void gen_quote(void) { short x; /* Index */ short v; /* Volatility of index */ short h; /* Half of v */ short r; /* Random change */ x = rand() % MAXQ; v = quotes[x].volit; h = (v / 2) - 2; r = rand() % v; if ( r < h ) r = -r; quotes[x].current += r; } gen_quote( ) 호출 Stock index change
Server program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server int main(int argc,char **argv) short x; /* index of Stock Indexes */ double I0; /* Initial index value */ double I; /* Index value */ char bcbuf[512], *bp;/* Buffer and ptr */ int z; /* Status return code */ int s; /* Socket */ struct sockaddr_in adr_srvr;/* AF_INET */ int len_srvr; /* length */ struct sockaddr_in adr_bc; /* AF_INET */ int len_bc; /* length */ static int so_broadcast = TRUE; static char *sv_addr = "127.0.0.1:*", *bc_addr = "127.255.255.255:9097"; /* * Form a server address : */ if ( argc > 2 ) /* Server address : */ sv_addr = argv[2]; if ( argc > 1 ) /* Broadcast address : */ bc_addr = argv[1]; /* * Form the server address : */ len_srvr = sizeof adr_srvr; z = mkaddr( &adr_srvr, /* Returned address */ &len_srvr, /* Returned length */ sv_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad server address"); /* * Form the broadcast address : */ len_bc = sizeof adr_bc; z = mkaddr( &adr_bc, /* Returned address */ &len_bc, /* Returned length */ bc_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad broadcast address"); /* * Create a UDP socket to use : */ s = socket(AF_INET,SOCK_DGRAM,0); if ( s == -1 ) bail("socket()"); /* * Allow broadcasts : */ z = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &so_broadcast, sizeof so_broadcast); if ( z == -1 ) bail("setsockopt(SO_BROADCAST)"); /* * Bind a address to our socket, so that * client programs can listen to this * server: */ z = bind(s, (struct sockaddr *)&adr_srvr, len_srvr); if ( z == -1 ) bail("bind()"); /* * Now start serving quotes : */ initialize(); for (;;) /* * Update one quote in the list: */ gen_quote(); /* * Form a packet to send out : */ bp = bcbuf; for ( x=0; x<MAXQ; ++x ) I0 = quotes[x].start / 100.0; I = quotes[x].current / 100.0; sprintf(bp, "%-7.7s %8.2f %+.2f", quotes[x].index, I, I - I0); bp += strlen(bp); /* * Broadcast the updated info: */ z = sendto(s, bcbuf, strlen(bcbuf), 0, (struct sockaddr *)&adr_bc, len_bc); if ( z == -1 ) bail("sendto()"); sleep(4); return 0; } Default address 선언 No command-line argument *sv_addr = "127.0.0.1:*", *bc_addr = "127.255.255.255:9097";
Server program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server int main(int argc,char **argv) short x; /* index of Stock Indexes */ double I0; /* Initial index value */ double I; /* Index value */ char bcbuf[512], *bp;/* Buffer and ptr */ int z; /* Status return code */ int s; /* Socket */ struct sockaddr_in adr_srvr;/* AF_INET */ int len_srvr; /* length */ struct sockaddr_in adr_bc; /* AF_INET */ int len_bc; /* length */ static int so_broadcast = TRUE; static char *sv_addr = "127.0.0.1:*", *bc_addr = "127.255.255.255:9097"; /* * Form a server address : */ if ( argc > 2 ) /* Server address : */ sv_addr = argv[2]; if ( argc > 1 ) /* Broadcast address : */ bc_addr = argv[1]; /* * Form the server address : */ len_srvr = sizeof adr_srvr; z = mkaddr( &adr_srvr, /* Returned address */ &len_srvr, /* Returned length */ sv_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad server address"); /* * Form the broadcast address : */ len_bc = sizeof adr_bc; z = mkaddr( &adr_bc, /* Returned address */ &len_bc, /* Returned length */ bc_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad broadcast address"); /* * Create a UDP socket to use : */ s = socket(AF_INET,SOCK_DGRAM,0); if ( s == -1 ) bail("socket()"); /* * Allow broadcasts : */ z = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &so_broadcast, sizeof so_broadcast); if ( z == -1 ) bail("setsockopt(SO_BROADCAST)"); /* * Bind a address to our socket, so that * client programs can listen to this * server: */ z = bind(s, (struct sockaddr *)&adr_srvr, len_srvr); if ( z == -1 ) bail("bind()"); /* * Now start serving quotes : */ initialize(); for (;;) /* * Update one quote in the list: */ gen_quote(); /* * Form a packet to send out : */ bp = bcbuf; for ( x=0; x<MAXQ; ++x ) I0 = quotes[x].start / 100.0; I = quotes[x].current / 100.0; sprintf(bp, "%-7.7s %8.2f %+.2f", quotes[x].index, I, I - I0); bp += strlen(bp); /* * Broadcast the updated info: */ z = sendto(s, bcbuf, strlen(bcbuf), 0, (struct sockaddr *)&adr_bc, len_bc); if ( z == -1 ) bail("sendto()"); sleep(4); return 0; } Two command-line argument 2번째 변수로 server address를 취함 if ( argc > 2 ) sv_addr = argv[2]; if ( argc > 1 ) bc_addr = argv[1]; One or more command-line argument 1번째 변수로 broadcast address를 취함
Server program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server len_srvr = sizeof adr_srvr; z = mkaddr( &adr_srvr, /* Returned address */ &len_srvr, /* Returned length */ sv_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad server address"); int main(int argc,char **argv) short x; /* index of Stock Indexes */ double I0; /* Initial index value */ double I; /* Index value */ char bcbuf[512], *bp;/* Buffer and ptr */ int z; /* Status return code */ int s; /* Socket */ struct sockaddr_in adr_srvr;/* AF_INET */ int len_srvr; /* length */ struct sockaddr_in adr_bc; /* AF_INET */ int len_bc; /* length */ static int so_broadcast = TRUE; static char *sv_addr = "127.0.0.1:*", *bc_addr = "127.255.255.255:9097"; /* * Form a server address : */ if ( argc > 2 ) /* Server address : */ sv_addr = argv[2]; if ( argc > 1 ) /* Broadcast address : */ bc_addr = argv[1]; /* * Form the server address : */ len_srvr = sizeof adr_srvr; z = mkaddr( &adr_srvr, /* Returned address */ &len_srvr, /* Returned length */ sv_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad server address"); /* * Form the broadcast address : */ len_bc = sizeof adr_bc; z = mkaddr( &adr_bc, /* Returned address */ &len_bc, /* Returned length */ bc_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad broadcast address"); /* * Create a UDP socket to use : */ s = socket(AF_INET,SOCK_DGRAM,0); if ( s == -1 ) bail("socket()"); /* * Allow broadcasts : */ z = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &so_broadcast, sizeof so_broadcast); if ( z == -1 ) bail("setsockopt(SO_BROADCAST)"); /* * Bind a address to our socket, so that * client programs can listen to this * server: */ z = bind(s, (struct sockaddr *)&adr_srvr, len_srvr); if ( z == -1 ) bail("bind()"); /* * Now start serving quotes : */ initialize(); for (;;) /* * Update one quote in the list: */ gen_quote(); /* * Form a packet to send out : */ bp = bcbuf; for ( x=0; x<MAXQ; ++x ) I0 = quotes[x].start / 100.0; I = quotes[x].current / 100.0; sprintf(bp, "%-7.7s %8.2f %+.2f", quotes[x].index, I, I - I0); bp += strlen(bp); /* * Broadcast the updated info: */ z = sendto(s, bcbuf, strlen(bcbuf), 0, (struct sockaddr *)&adr_bc, len_bc); if ( z == -1 ) bail("sendto()"); sleep(4); return 0; } Server address 형성
Server program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server len_bc = sizeof adr_bc; z = mkaddr( &adr_bc, /* Returned address */ &len_bc, /* Returned length */ bc_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad broadcast address"); int main(int argc,char **argv) short x; /* index of Stock Indexes */ double I0; /* Initial index value */ double I; /* Index value */ char bcbuf[512], *bp;/* Buffer and ptr */ int z; /* Status return code */ int s; /* Socket */ struct sockaddr_in adr_srvr;/* AF_INET */ int len_srvr; /* length */ struct sockaddr_in adr_bc; /* AF_INET */ int len_bc; /* length */ static int so_broadcast = TRUE; static char *sv_addr = "127.0.0.1:*", *bc_addr = "127.255.255.255:9097"; /* * Form a server address : */ if ( argc > 2 ) /* Server address : */ sv_addr = argv[2]; if ( argc > 1 ) /* Broadcast address : */ bc_addr = argv[1]; /* * Form the server address : */ len_srvr = sizeof adr_srvr; z = mkaddr( &adr_srvr, /* Returned address */ &len_srvr, /* Returned length */ sv_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad server address"); /* * Form the broadcast address : */ len_bc = sizeof adr_bc; z = mkaddr( &adr_bc, /* Returned address */ &len_bc, /* Returned length */ bc_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad broadcast address"); /* * Create a UDP socket to use : */ s = socket(AF_INET,SOCK_DGRAM,0); if ( s == -1 ) bail("socket()"); /* * Allow broadcasts : */ z = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &so_broadcast, sizeof so_broadcast); if ( z == -1 ) bail("setsockopt(SO_BROADCAST)"); /* * Bind a address to our socket, so that * client programs can listen to this * server: */ z = bind(s, (struct sockaddr *)&adr_srvr, len_srvr); if ( z == -1 ) bail("bind()"); /* * Now start serving quotes : */ initialize(); for (;;) /* * Update one quote in the list: */ gen_quote(); /* * Form a packet to send out : */ bp = bcbuf; for ( x=0; x<MAXQ; ++x ) I0 = quotes[x].start / 100.0; I = quotes[x].current / 100.0; sprintf(bp, "%-7.7s %8.2f %+.2f", quotes[x].index, I, I - I0); bp += strlen(bp); /* * Broadcast the updated info: */ z = sendto(s, bcbuf, strlen(bcbuf), 0, (struct sockaddr *)&adr_bc, len_bc); if ( z == -1 ) bail("sendto()"); sleep(4); return 0; } Broadcast address 형성
Server program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server int main(int argc,char **argv) short x; /* index of Stock Indexes */ double I0; /* Initial index value */ double I; /* Index value */ char bcbuf[512], *bp;/* Buffer and ptr */ int z; /* Status return code */ int s; /* Socket */ struct sockaddr_in adr_srvr;/* AF_INET */ int len_srvr; /* length */ struct sockaddr_in adr_bc; /* AF_INET */ int len_bc; /* length */ static int so_broadcast = TRUE; static char *sv_addr = "127.0.0.1:*", *bc_addr = "127.255.255.255:9097"; /* * Form a server address : */ if ( argc > 2 ) /* Server address : */ sv_addr = argv[2]; if ( argc > 1 ) /* Broadcast address : */ bc_addr = argv[1]; /* * Form the server address : */ len_srvr = sizeof adr_srvr; z = mkaddr( &adr_srvr, /* Returned address */ &len_srvr, /* Returned length */ sv_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad server address"); /* * Form the broadcast address : */ len_bc = sizeof adr_bc; z = mkaddr( &adr_bc, /* Returned address */ &len_bc, /* Returned length */ bc_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad broadcast address"); /* * Create a UDP socket to use : */ s = socket(AF_INET,SOCK_DGRAM,0); if ( s == -1 ) bail("socket()"); /* * Allow broadcasts : */ z = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &so_broadcast, sizeof so_broadcast); if ( z == -1 ) bail("setsockopt(SO_BROADCAST)"); /* * Bind a address to our socket, so that * client programs can listen to this * server: */ z = bind(s, (struct sockaddr *)&adr_srvr, len_srvr); if ( z == -1 ) bail("bind()"); /* * Now start serving quotes : */ initialize(); for (;;) /* * Update one quote in the list: */ gen_quote(); /* * Form a packet to send out : */ bp = bcbuf; for ( x=0; x<MAXQ; ++x ) I0 = quotes[x].start / 100.0; I = quotes[x].current / 100.0; sprintf(bp, "%-7.7s %8.2f %+.2f", quotes[x].index, I, I - I0); bp += strlen(bp); /* * Broadcast the updated info: */ z = sendto(s, bcbuf, strlen(bcbuf), 0, (struct sockaddr *)&adr_bc, len_bc); if ( z == -1 ) bail("sendto()"); sleep(4); return 0; } s = socket(AF_INET,SOCK_DGRAM,0); if ( s == -1 ) bail("socket()"); z = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &so_broadcast, sizeof so_broadcast); if ( z == -1 ) bail("setsockopt(SO_BROADCAST)"); Socket 생성 SO_BROADCAST enable
Server program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server int main(int argc,char **argv) short x; /* index of Stock Indexes */ double I0; /* Initial index value */ double I; /* Index value */ char bcbuf[512], *bp;/* Buffer and ptr */ int z; /* Status return code */ int s; /* Socket */ struct sockaddr_in adr_srvr;/* AF_INET */ int len_srvr; /* length */ struct sockaddr_in adr_bc; /* AF_INET */ int len_bc; /* length */ static int so_broadcast = TRUE; static char *sv_addr = "127.0.0.1:*", *bc_addr = "127.255.255.255:9097"; /* * Form a server address : */ if ( argc > 2 ) /* Server address : */ sv_addr = argv[2]; if ( argc > 1 ) /* Broadcast address : */ bc_addr = argv[1]; /* * Form the server address : */ len_srvr = sizeof adr_srvr; z = mkaddr( &adr_srvr, /* Returned address */ &len_srvr, /* Returned length */ sv_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad server address"); /* * Form the broadcast address : */ len_bc = sizeof adr_bc; z = mkaddr( &adr_bc, /* Returned address */ &len_bc, /* Returned length */ bc_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad broadcast address"); /* * Create a UDP socket to use : */ s = socket(AF_INET,SOCK_DGRAM,0); if ( s == -1 ) bail("socket()"); /* * Allow broadcasts : */ z = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &so_broadcast, sizeof so_broadcast); if ( z == -1 ) bail("setsockopt(SO_BROADCAST)"); /* * Bind a address to our socket, so that * client programs can listen to this * server: */ z = bind(s, (struct sockaddr *)&adr_srvr, len_srvr); if ( z == -1 ) bail("bind()"); /* * Now start serving quotes : */ initialize(); for (;;) /* * Update one quote in the list: */ gen_quote(); /* * Form a packet to send out : */ bp = bcbuf; for ( x=0; x<MAXQ; ++x ) I0 = quotes[x].start / 100.0; I = quotes[x].current / 100.0; sprintf(bp, "%-7.7s %8.2f %+.2f", quotes[x].index, I, I - I0); bp += strlen(bp); /* * Broadcast the updated info: */ z = sendto(s, bcbuf, strlen(bcbuf), 0, (struct sockaddr *)&adr_bc, len_bc); if ( z == -1 ) bail("sendto()"); sleep(4); return 0; } z = bind(s, (struct sockaddr *)&adr_srvr, len_srvr); if ( z == -1 ) bail("bind()"); Server address bind
Server program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server int main(int argc,char **argv) short x; /* index of Stock Indexes */ double I0; /* Initial index value */ double I; /* Index value */ char bcbuf[512], *bp;/* Buffer and ptr */ int z; /* Status return code */ int s; /* Socket */ struct sockaddr_in adr_srvr;/* AF_INET */ int len_srvr; /* length */ struct sockaddr_in adr_bc; /* AF_INET */ int len_bc; /* length */ static int so_broadcast = TRUE; static char *sv_addr = "127.0.0.1:*", *bc_addr = "127.255.255.255:9097"; /* * Form a server address : */ if ( argc > 2 ) /* Server address : */ sv_addr = argv[2]; if ( argc > 1 ) /* Broadcast address : */ bc_addr = argv[1]; /* * Form the server address : */ len_srvr = sizeof adr_srvr; z = mkaddr( &adr_srvr, /* Returned address */ &len_srvr, /* Returned length */ sv_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad server address"); /* * Form the broadcast address : */ len_bc = sizeof adr_bc; z = mkaddr( &adr_bc, /* Returned address */ &len_bc, /* Returned length */ bc_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad broadcast address"); /* * Create a UDP socket to use : */ s = socket(AF_INET,SOCK_DGRAM,0); if ( s == -1 ) bail("socket()"); /* * Allow broadcasts : */ z = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &so_broadcast, sizeof so_broadcast); if ( z == -1 ) bail("setsockopt(SO_BROADCAST)"); /* * Bind a address to our socket, so that * client programs can listen to this * server: */ z = bind(s, (struct sockaddr *)&adr_srvr, len_srvr); if ( z == -1 ) bail("bind()"); /* * Now start serving quotes : */ initialize(); for (;;) /* * Update one quote in the list: */ gen_quote(); /* * Form a packet to send out : */ bp = bcbuf; for ( x=0; x<MAXQ; ++x ) I0 = quotes[x].start / 100.0; I = quotes[x].current / 100.0; sprintf(bp, "%-7.7s %8.2f %+.2f", quotes[x].index, I, I - I0); bp += strlen(bp); /* * Broadcast the updated info: */ z = sendto(s, bcbuf, strlen(bcbuf), 0, (struct sockaddr *)&adr_bc, len_bc); if ( z == -1 ) bail("sendto()"); sleep(4); return 0; } initialize(); for (;;) { Stock market index 초기화 Server loop 시작
Server program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server int main(int argc,char **argv) short x; /* index of Stock Indexes */ double I0; /* Initial index value */ double I; /* Index value */ char bcbuf[512], *bp;/* Buffer and ptr */ int z; /* Status return code */ int s; /* Socket */ struct sockaddr_in adr_srvr;/* AF_INET */ int len_srvr; /* length */ struct sockaddr_in adr_bc; /* AF_INET */ int len_bc; /* length */ static int so_broadcast = TRUE; static char *sv_addr = "127.0.0.1:*", *bc_addr = "127.255.255.255:9097"; /* * Form a server address : */ if ( argc > 2 ) /* Server address : */ sv_addr = argv[2]; if ( argc > 1 ) /* Broadcast address : */ bc_addr = argv[1]; /* * Form the server address : */ len_srvr = sizeof adr_srvr; z = mkaddr( &adr_srvr, /* Returned address */ &len_srvr, /* Returned length */ sv_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad server address"); /* * Form the broadcast address : */ len_bc = sizeof adr_bc; z = mkaddr( &adr_bc, /* Returned address */ &len_bc, /* Returned length */ bc_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad broadcast address"); /* * Create a UDP socket to use : */ s = socket(AF_INET,SOCK_DGRAM,0); if ( s == -1 ) bail("socket()"); /* * Allow broadcasts : */ z = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &so_broadcast, sizeof so_broadcast); if ( z == -1 ) bail("setsockopt(SO_BROADCAST)"); /* * Bind a address to our socket, so that * client programs can listen to this * server: */ z = bind(s, (struct sockaddr *)&adr_srvr, len_srvr); if ( z == -1 ) bail("bind()"); /* * Now start serving quotes : */ initialize(); for (;;) /* * Update one quote in the list: */ gen_quote(); /* * Form a packet to send out : */ bp = bcbuf; for ( x=0; x<MAXQ; ++x ) I0 = quotes[x].start / 100.0; I = quotes[x].current / 100.0; sprintf(bp, "%-7.7s %8.2f %+.2f", quotes[x].index, I, I - I0); bp += strlen(bp); /* * Broadcast the updated info: */ z = sendto(s, bcbuf, strlen(bcbuf), 0, (struct sockaddr *)&adr_bc, len_bc); if ( z == -1 ) bail("sendto()"); sleep(4); return 0; } bp = bcbuf; for ( x=0; x<MAXQ; ++x ) { I0 = quotes[x].start / 100.0; I = quotes[x].current / 100.0; sprintf(bp, "%-7.7s %8.2f %+.2f", quotes[x].index, I, I - I0); bp += strlen(bp); } index를 format buffer로 추출
Server program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server int main(int argc,char **argv) short x; /* index of Stock Indexes */ double I0; /* Initial index value */ double I; /* Index value */ char bcbuf[512], *bp;/* Buffer and ptr */ int z; /* Status return code */ int s; /* Socket */ struct sockaddr_in adr_srvr;/* AF_INET */ int len_srvr; /* length */ struct sockaddr_in adr_bc; /* AF_INET */ int len_bc; /* length */ static int so_broadcast = TRUE; static char *sv_addr = "127.0.0.1:*", *bc_addr = "127.255.255.255:9097"; /* * Form a server address : */ if ( argc > 2 ) /* Server address : */ sv_addr = argv[2]; if ( argc > 1 ) /* Broadcast address : */ bc_addr = argv[1]; /* * Form the server address : */ len_srvr = sizeof adr_srvr; z = mkaddr( &adr_srvr, /* Returned address */ &len_srvr, /* Returned length */ sv_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad server address"); /* * Form the broadcast address : */ len_bc = sizeof adr_bc; z = mkaddr( &adr_bc, /* Returned address */ &len_bc, /* Returned length */ bc_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad broadcast address"); /* * Create a UDP socket to use : */ s = socket(AF_INET,SOCK_DGRAM,0); if ( s == -1 ) bail("socket()"); /* * Allow broadcasts : */ z = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &so_broadcast, sizeof so_broadcast); if ( z == -1 ) bail("setsockopt(SO_BROADCAST)"); /* * Bind a address to our socket, so that * client programs can listen to this * server: */ z = bind(s, (struct sockaddr *)&adr_srvr, len_srvr); if ( z == -1 ) bail("bind()"); /* * Now start serving quotes : */ initialize(); for (;;) /* * Update one quote in the list: */ gen_quote(); /* * Form a packet to send out : */ bp = bcbuf; for ( x=0; x<MAXQ; ++x ) I0 = quotes[x].start / 100.0; I = quotes[x].current / 100.0; sprintf(bp, "%-7.7s %8.2f %+.2f", quotes[x].index, I, I - I0); bp += strlen(bp); /* * Broadcast the updated info: */ z = sendto(s, bcbuf, strlen(bcbuf), 0, (struct sockaddr *)&adr_bc, len_bc); if ( z == -1 ) bail("sendto()"); sleep(4); return 0; } z = sendto(s, bcbuf, strlen(bcbuf), 0, (struct sockaddr *)&adr_bc, len_bc); if ( z == -1 ) bail("sendto()"); sendto( ) 호출 broadcast adr_bc가 broadcast address를 내포
Server program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server int main(int argc,char **argv) short x; /* index of Stock Indexes */ double I0; /* Initial index value */ double I; /* Index value */ char bcbuf[512], *bp;/* Buffer and ptr */ int z; /* Status return code */ int s; /* Socket */ struct sockaddr_in adr_srvr;/* AF_INET */ int len_srvr; /* length */ struct sockaddr_in adr_bc; /* AF_INET */ int len_bc; /* length */ static int so_broadcast = TRUE; static char *sv_addr = "127.0.0.1:*", *bc_addr = "127.255.255.255:9097"; /* * Form a server address : */ if ( argc > 2 ) /* Server address : */ sv_addr = argv[2]; if ( argc > 1 ) /* Broadcast address : */ bc_addr = argv[1]; /* * Form the server address : */ len_srvr = sizeof adr_srvr; z = mkaddr( &adr_srvr, /* Returned address */ &len_srvr, /* Returned length */ sv_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad server address"); /* * Form the broadcast address : */ len_bc = sizeof adr_bc; z = mkaddr( &adr_bc, /* Returned address */ &len_bc, /* Returned length */ bc_addr, /* Input string addr */ "udp"); /* UDP protocol */ if ( z == -1 ) bail("Bad broadcast address"); /* * Create a UDP socket to use : */ s = socket(AF_INET,SOCK_DGRAM,0); if ( s == -1 ) bail("socket()"); /* * Allow broadcasts : */ z = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &so_broadcast, sizeof so_broadcast); if ( z == -1 ) bail("setsockopt(SO_BROADCAST)"); /* * Bind a address to our socket, so that * client programs can listen to this * server: */ z = bind(s, (struct sockaddr *)&adr_srvr, len_srvr); if ( z == -1 ) bail("bind()"); /* * Now start serving quotes : */ initialize(); for (;;) /* * Update one quote in the list: */ gen_quote(); /* * Form a packet to send out : */ bp = bcbuf; for ( x=0; x<MAXQ; ++x ) I0 = quotes[x].start / 100.0; I = quotes[x].current / 100.0; sprintf(bp, "%-7.7s %8.2f %+.2f", quotes[x].index, I, I - I0); bp += strlen(bp); /* * Broadcast the updated info: */ z = sendto(s, bcbuf, strlen(bcbuf), 0, (struct sockaddr *)&adr_bc, len_bc); if ( z == -1 ) bail("sendto()"); sleep(4); return 0; } Time delay For quote update sleep(4);
/* gquotes.c: * * Get datagram stock market * quotes from UDP broadcast : */ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <time.h> #include <signal.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #ifndef TRUE #define TRUE 1 #define FALSE 0 #endif extern int mkaddr( void *addr, int *addrlen, char *str_addr, char *protocol); /* * This function reports the error and * exits back to the shell : */ static void bail(const char *on_what) fputs(strerror(errno),stderr); fputs(": ",stderr); fputs(on_what,stderr); fputc('',stderr); exit(1); int main(int argc,char **argv) int z; int x; struct sockaddr_in adr; /* AF_INET */ int len_inet; /* length */ int s; /* Socket */ char dgram[512]; /* Recv buffer */ static int so_reuseaddr = TRUE; static char *bc_addr = "127.255.255.255:9097"; /* * Use a server address from the command * line, if one has been provided. * Otherwise, this program will default * to using the arbitrary address * 127.0.0.23 : */ if ( argc > 1 ) /* Broadcast address : */ bc_addr = argv[1]; /* * Create a UDP socket to use : */ s = socket(AF_INET,SOCK_DGRAM,0); if ( s == -1 ) bail("socket()"); /* * Form the broadcast address : */ len_inet = sizeof adr; z = mkaddr(&adr, &len_inet, bc_addr, "udp"); if ( z == -1 ) bail("Bad broadcast address"); /* * Allow multiple listeners on the * broadcast address : */ z = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &so_reuseaddr, sizeof so_reuseaddr); if ( z == -1 ) bail("setsockopt(SO_REUSEADDR)"); /* * Bind our socket to the broadcast address: */ z = bind(s, (struct sockaddr *)&adr, len_inet); if ( z == -1 ) bail("bind(2)"); for (;;) /* * Wait for a broadcast message : */ z = recvfrom(s, /* Socket */ dgram, /* Receiving buffer */ sizeof dgram,/* Max rcv buf size */ 0, /* Flags: no options */ (struct sockaddr *)&adr, /* Addr */ &x); /* Addr len, in & out */ if ( z < 0 ) bail("recvfrom(2)"); /* else err */ fwrite(dgram,z,1,stdout); putchar(''); fflush(stdout); return 0; Client program Listing 13.2 gqoutes.c - The Stock Market Index Client Program Default Broadcast address UDP port number *bc_addr = "127.255.255.255:9097";
Client program Listing 13.2 gqoutes.c - The Stock Market Index Client Program s = socket(AF_INET,SOCK_DGRAM,0); if ( s == -1 ) bail("socket()"); /* gquotes.c: * * Get datagram stock market * quotes from UDP broadcast : */ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <time.h> #include <signal.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #ifndef TRUE #define TRUE 1 #define FALSE 0 #endif extern int mkaddr( void *addr, int *addrlen, char *str_addr, char *protocol); /* * This function reports the error and * exits back to the shell : */ static void bail(const char *on_what) fputs(strerror(errno),stderr); fputs(": ",stderr); fputs(on_what,stderr); fputc('',stderr); exit(1); int main(int argc,char **argv) int z; int x; struct sockaddr_in adr; /* AF_INET */ int len_inet; /* length */ int s; /* Socket */ char dgram[512]; /* Recv buffer */ static int so_reuseaddr = TRUE; static char *bc_addr = "127.255.255.255:9097"; /* * Use a server address from the command * line, if one has been provided. * Otherwise, this program will default * to using the arbitrary address * 127.0.0.23 : */ if ( argc > 1 ) /* Broadcast address : */ bc_addr = argv[1]; /* * Create a UDP socket to use : */ s = socket(AF_INET,SOCK_DGRAM,0); if ( s == -1 ) bail("socket()"); /* * Form the broadcast address : */ len_inet = sizeof adr; z = mkaddr(&adr, &len_inet, bc_addr, "udp"); if ( z == -1 ) bail("Bad broadcast address"); /* * Allow multiple listeners on the * broadcast address : */ z = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &so_reuseaddr, sizeof so_reuseaddr); if ( z == -1 ) bail("setsockopt(SO_REUSEADDR)"); /* * Bind our socket to the broadcast address: */ z = bind(s, (struct sockaddr *)&adr, len_inet); if ( z == -1 ) bail("bind(2)"); for (;;) /* * Wait for a broadcast message : */ z = recvfrom(s, /* Socket */ dgram, /* Receiving buffer */ sizeof dgram,/* Max rcv buf size */ 0, /* Flags: no options */ (struct sockaddr *)&adr, /* Addr */ &x); /* Addr len, in & out */ if ( z < 0 ) bail("recvfrom(2)"); /* else err */ fwrite(dgram,z,1,stdout); putchar(''); fflush(stdout); return 0; UDB Socket 생성 len_inet = sizeof adr; z = mkaddr(&adr, &len_inet, bc_addr, "udp"); if ( z == -1 ) bail("Bad broadcast address"); Broadcast address 형성
Client program Listing 13.2 gqoutes.c - The Stock Market Index Client Program /* gquotes.c: * * Get datagram stock market * quotes from UDP broadcast : */ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <time.h> #include <signal.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #ifndef TRUE #define TRUE 1 #define FALSE 0 #endif extern int mkaddr( void *addr, int *addrlen, char *str_addr, char *protocol); /* * This function reports the error and * exits back to the shell : */ static void bail(const char *on_what) fputs(strerror(errno),stderr); fputs(": ",stderr); fputs(on_what,stderr); fputc('',stderr); exit(1); int main(int argc,char **argv) int z; int x; struct sockaddr_in adr; /* AF_INET */ int len_inet; /* length */ int s; /* Socket */ char dgram[512]; /* Recv buffer */ static int so_reuseaddr = TRUE; static char *bc_addr = "127.255.255.255:9097"; /* * Use a server address from the command * line, if one has been provided. * Otherwise, this program will default * to using the arbitrary address * 127.0.0.23 : */ if ( argc > 1 ) /* Broadcast address : */ bc_addr = argv[1]; /* * Create a UDP socket to use : */ s = socket(AF_INET,SOCK_DGRAM,0); if ( s == -1 ) bail("socket()"); /* * Form the broadcast address : */ len_inet = sizeof adr; z = mkaddr(&adr, &len_inet, bc_addr, "udp"); if ( z == -1 ) bail("Bad broadcast address"); /* * Allow multiple listeners on the * broadcast address : */ z = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &so_reuseaddr, sizeof so_reuseaddr); if ( z == -1 ) bail("setsockopt(SO_REUSEADDR)"); /* * Bind our socket to the broadcast address: */ z = bind(s, (struct sockaddr *)&adr, len_inet); if ( z == -1 ) bail("bind(2)"); for (;;) /* * Wait for a broadcast message : */ z = recvfrom(s, /* Socket */ dgram, /* Receiving buffer */ sizeof dgram,/* Max rcv buf size */ 0, /* Flags: no options */ (struct sockaddr *)&adr, /* Addr */ &x); /* Addr len, in & out */ if ( z < 0 ) bail("recvfrom(2)"); /* else err */ fwrite(dgram,z,1,stdout); putchar(''); fflush(stdout); return 0; z = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &so_reuseaddr, sizeof so_reuseaddr); if ( z == -1 ) bail("setsockopt(SO_REUSEADDR)"); SO_REUSEADDR enable
Client program Listing 13.2 gqoutes.c - The Stock Market Index Client Program /* gquotes.c: * * Get datagram stock market * quotes from UDP broadcast : */ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <time.h> #include <signal.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #ifndef TRUE #define TRUE 1 #define FALSE 0 #endif extern int mkaddr( void *addr, int *addrlen, char *str_addr, char *protocol); /* * This function reports the error and * exits back to the shell : */ static void bail(const char *on_what) fputs(strerror(errno),stderr); fputs(": ",stderr); fputs(on_what,stderr); fputc('',stderr); exit(1); int main(int argc,char **argv) int z; int x; struct sockaddr_in adr; /* AF_INET */ int len_inet; /* length */ int s; /* Socket */ char dgram[512]; /* Recv buffer */ static int so_reuseaddr = TRUE; static char *bc_addr = "127.255.255.255:9097"; /* * Use a server address from the command * line, if one has been provided. * Otherwise, this program will default * to using the arbitrary address * 127.0.0.23 : */ if ( argc > 1 ) /* Broadcast address : */ bc_addr = argv[1]; /* * Create a UDP socket to use : */ s = socket(AF_INET,SOCK_DGRAM,0); if ( s == -1 ) bail("socket()"); /* * Form the broadcast address : */ len_inet = sizeof adr; z = mkaddr(&adr, &len_inet, bc_addr, "udp"); if ( z == -1 ) bail("Bad broadcast address"); /* * Allow multiple listeners on the * broadcast address : */ z = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &so_reuseaddr, sizeof so_reuseaddr); if ( z == -1 ) bail("setsockopt(SO_REUSEADDR)"); /* * Bind our socket to the broadcast address: */ z = bind(s, (struct sockaddr *)&adr, len_inet); if ( z == -1 ) bail("bind(2)"); for (;;) /* * Wait for a broadcast message : */ z = recvfrom(s, /* Socket */ dgram, /* Receiving buffer */ sizeof dgram,/* Max rcv buf size */ 0, /* Flags: no options */ (struct sockaddr *)&adr, /* Addr */ &x); /* Addr len, in & out */ if ( z < 0 ) bail("recvfrom(2)"); /* else err */ fwrite(dgram,z,1,stdout); putchar(''); fflush(stdout); return 0; z = bind(s, (struct sockaddr *)&adr, len_inet); if ( z == -1 ) bail("bind(2)"); for (;;) { Broadcast address bind Broadcast 수신 loop 시작
Client program Listing 13.2 gqoutes.c - The Stock Market Index Client Program /* gquotes.c: * * Get datagram stock market * quotes from UDP broadcast : */ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <time.h> #include <signal.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #ifndef TRUE #define TRUE 1 #define FALSE 0 #endif extern int mkaddr( void *addr, int *addrlen, char *str_addr, char *protocol); /* * This function reports the error and * exits back to the shell : */ static void bail(const char *on_what) fputs(strerror(errno),stderr); fputs(": ",stderr); fputs(on_what,stderr); fputc('',stderr); exit(1); int main(int argc,char **argv) int z; int x; struct sockaddr_in adr; /* AF_INET */ int len_inet; /* length */ int s; /* Socket */ char dgram[512]; /* Recv buffer */ static int so_reuseaddr = TRUE; static char *bc_addr = "127.255.255.255:9097"; /* * Use a server address from the command * line, if one has been provided. * Otherwise, this program will default * to using the arbitrary address * 127.0.0.23 : */ if ( argc > 1 ) /* Broadcast address : */ bc_addr = argv[1]; /* * Create a UDP socket to use : */ s = socket(AF_INET,SOCK_DGRAM,0); if ( s == -1 ) bail("socket()"); /* * Form the broadcast address : */ len_inet = sizeof adr; z = mkaddr(&adr, &len_inet, bc_addr, "udp"); if ( z == -1 ) bail("Bad broadcast address"); /* * Allow multiple listeners on the * broadcast address : */ z = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &so_reuseaddr, sizeof so_reuseaddr); if ( z == -1 ) bail("setsockopt(SO_REUSEADDR)"); /* * Bind our socket to the broadcast address: */ z = bind(s, (struct sockaddr *)&adr, len_inet); if ( z == -1 ) bail("bind(2)"); for (;;) /* * Wait for a broadcast message : */ z = recvfrom(s, /* Socket */ dgram, /* Receiving buffer */ sizeof dgram,/* Max rcv buf size */ 0, /* Flags: no options */ (struct sockaddr *)&adr, /* Addr */ &x); /* Addr len, in & out */ if ( z < 0 ) bail("recvfrom(2)"); /* else err */ fwrite(dgram,z,1,stdout); putchar(''); fflush(stdout); return 0; z = recvfrom(s, /* Socket */ dgram, /* Receiving buffer */ sizeof dgram,/* Max rcv buf size */ 0, /* Flags: no options */ (struct sockaddr *)&adr, /* Addr */ &x); /* Addr len, in & out */ if ( z < 0 ) bail("recvfrom(2)"); /* else err */ Broadcast 수신
Client program Listing 13.2 gqoutes.c - The Stock Market Index Client Program /* gquotes.c: * * Get datagram stock market * quotes from UDP broadcast : */ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <time.h> #include <signal.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #ifndef TRUE #define TRUE 1 #define FALSE 0 #endif extern int mkaddr( void *addr, int *addrlen, char *str_addr, char *protocol); /* * This function reports the error and * exits back to the shell : */ static void bail(const char *on_what) fputs(strerror(errno),stderr); fputs(": ",stderr); fputs(on_what,stderr); fputc('',stderr); exit(1); int main(int argc,char **argv) int z; int x; struct sockaddr_in adr; /* AF_INET */ int len_inet; /* length */ int s; /* Socket */ char dgram[512]; /* Recv buffer */ static int so_reuseaddr = TRUE; static char *bc_addr = "127.255.255.255:9097"; /* * Use a server address from the command * line, if one has been provided. * Otherwise, this program will default * to using the arbitrary address * 127.0.0.23 : */ if ( argc > 1 ) /* Broadcast address : */ bc_addr = argv[1]; /* * Create a UDP socket to use : */ s = socket(AF_INET,SOCK_DGRAM,0); if ( s == -1 ) bail("socket()"); /* * Form the broadcast address : */ len_inet = sizeof adr; z = mkaddr(&adr, &len_inet, bc_addr, "udp"); if ( z == -1 ) bail("Bad broadcast address"); /* * Allow multiple listeners on the * broadcast address : */ z = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &so_reuseaddr, sizeof so_reuseaddr); if ( z == -1 ) bail("setsockopt(SO_REUSEADDR)"); /* * Bind our socket to the broadcast address: */ z = bind(s, (struct sockaddr *)&adr, len_inet); if ( z == -1 ) bail("bind(2)"); for (;;) /* * Wait for a broadcast message : */ z = recvfrom(s, /* Socket */ dgram, /* Receiving buffer */ sizeof dgram,/* Max rcv buf size */ 0, /* Flags: no options */ (struct sockaddr *)&adr, /* Addr */ &x); /* Addr len, in & out */ if ( z < 0 ) bail("recvfrom(2)"); /* else err */ fwrite(dgram,z,1,stdout); putchar(''); fflush(stdout); return 0; fwrite(dgram,z,1,stdout); putchar(''); fflush(stdout); standard output
Demo. • $ ./stksrv 127.255.255.255:9097 & • $ ./gquotes 127.255.255.255:9097 • $ ./stksrv 192.168.0.255:9097 ‘192.168.0.1:*’ & • $ ./gqoutes 192.168.0.255 (pepper) • $ ./gqoutes 192.168.0.255 (slug)
Troubleshooting • Hub 탐색 • packet traffic 표시 LED flicker 확인 • tcpdump command 사용 @slug # tcpdump udp port 9097 tcpdump: listening on eth0 21:04:43,967482 pepper.ve3wwg.or.1032 > 192.168.0.255.9097: udp 96 21:04:47,977482 pepper.ve3wwg.or.1032 > 192.168.0.255.9097: udp 96 21:04:51,987482 pepper.ve3wwg.or.1032 > 192.168.0.255.9097: udp 96