Download presentation
Presentation is loading. Please wait.
Published byDiana Hudson Modified over 9 years ago
1
Broadcasting and UDP Network Lab. 남 재 준
2
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 2 목 차 Broadcast UDP 소켓 설치 소켓을 사용하여 broadcast message 보내기 소켓을 사용하여 broadcast message 받기
3
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 3 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 # Ethernet Interface 를 위한 broadcast address
4
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 4 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 Network IDHost ID - C Class address
5
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 5 Broadcast Address 255.255.255.255 broadcasting 을 위해서 사용될 수 있다. 대량의 broadcast 를 제한하고 있다. 개념정의가 미비한 상태 모든 network interface 들을 위해 사용 여러 interface 중 단 하나를 선택 일반적인 broadcast address 사용이 제지 된다.
6
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 6 Broadcast 하기 위해 Interface 이름 결정 interface의 broadcast address 결정 broadcast address를 broadcast 추가된 interface에 반복
7
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 7 mkaddr.c 확장 $ diff../ch.11/mkaddr.c mkaddr.c 99,102c99,100 sin_addr.s_addr = <inet_addr(host_part); sin_addr.s_addr == INADDR_NONE).... >if (!inet_aton(host_part, > &ap->sin_addr) ) $ General broadcast address (255.255.255.255) 와 bad IP address 의 구별을 위해 Invalid address INADDR_NONE 255.255.255.255 (32bit convert) INADDR_NONE
8
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 8 Broadcasting from a Server Command-line Create Server address Create Broadcast address Bind serve address Create Socket Initialize Index Index Buffer Update by gen_quote( ) Send by sendto( ) Time delay Sleep ( )
9
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 9 Broadcasting from a Server Socket은 SO_BROADCAST option을 가 지고 있어야 한다. sendto( ) 함수의 destination address Broadcast address
10
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 10 Receiving Broadcasting Command-line Bind Broadcast address Create Socket Receive broadcast Begin broadcast receiving loop Write Broadcast Info. Broadcast address is Formed
11
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 11 Receiving Broadcasting Broadcast를 수신하기 위해서 client program은 Socket에 bind된 address를 가지고 있어야 한다. SO_REUSEADDR multiple client 가 같은 host 에 같은 broadcast address 로부터 수신하도록 허락
12
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 12 Broadcast Demo. @pepper $./stksrv 127.255.255.255:9097 & [1] 756 @pepper $
13
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 13 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 $
14
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 14 Broadcast Demo. (to network) @pepper $./stksrv 192.168.0.255:9097 '192.168.0.1:*' & [2] 815 @pepper $
15
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 15 Broadcast Demo. (to network) @pepper (192.168.0.1) $./gquotes 192.168.0.255:9097 DJIA10304.73 +1.43 NASDAQ 2761.19 -0.56 S&P 500 1283.31 +0.00 TSE 300 6895.82 +0.10 DJIA10304.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
16
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 16 Server program /* stksrv.c: * * Example Stock Index Broadcast : */ #include #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); 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 구성
17
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 17 Server program /* stksrv.c: * * Example Stock Index Broadcast : */ #include #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); Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server 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 산출
18
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 18 Server program /* stksrv.c: * * Example Stock Index Broadcast : */ #include #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); Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server 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
19
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 19 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 program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server *sv_addr = "127.0.0.1:*", *bc_addr = "127.255.255.255:9097"; Default address 선언 No command-line argument
20
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 20 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 program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server if ( argc > 2 ) sv_addr = argv[2]; if ( argc > 1 ) bc_addr = argv[1]; Two command-line argument 2 번째 변수로 server address 를 취함 One or more command-line argument 1 번째 변수로 broadcast address 를 취함
21
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 21 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 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"); Server address 형성
22
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 22 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 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"); Broadcast address 형성
23
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 23 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 program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server 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
24
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 24 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 program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server z = bind(s, (struct sockaddr *)&adr_srvr, len_srvr); if ( z == -1 ) bail("bind()"); Server address bind
25
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 25 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 program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server initialize(); for (;;) { Stock market index 초기화 Server loop 시작
26
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 26 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 program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server 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 로 추출
27
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 27 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 program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server z = sendto(s, bcbuf, strlen(bcbuf), 0, (struct sockaddr *)&adr_bc, len_bc); if ( z == -1 ) bail("sendto()"); sendto( ) 호출 broadcast adr_bc 가 broadcast address 를 내포
28
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 28 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 program Listing 13.1 : stksrv.c-The Stock Market Index Broadcasting Server sleep(4); Time delay For quote update
29
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 29 Client program /* gquotes.c: * * Get datagram stock market * quotes from UDP broadcast : */ #include #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; Listing 13.2 gqoutes.c - The Stock Market Index Client Program *bc_addr = "127.255.255.255:9097"; Default Broadcast address UDP port number
30
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 30 Client program /* gquotes.c: * * Get datagram stock market * quotes from UDP broadcast : */ #include #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; Listing 13.2 gqoutes.c - The Stock Market Index Client Program s = socket(AF_INET,SOCK_DGRAM,0); if ( s == -1 ) bail("socket()"); UDB Socket 생성 len_inet = sizeof adr; z = mkaddr(&adr, &len_inet, bc_addr, "udp"); if ( z == -1 ) bail("Bad broadcast address"); Broadcast address 형성
31
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 31 Client program /* gquotes.c: * * Get datagram stock market * quotes from UDP broadcast : */ #include #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; Listing 13.2 gqoutes.c - The Stock Market Index Client Program z = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &so_reuseaddr, sizeof so_reuseaddr); if ( z == -1 ) bail("setsockopt(SO_REUSEADDR)"); SO_REUSEADDR enable
32
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 32 Client program /* gquotes.c: * * Get datagram stock market * quotes from UDP broadcast : */ #include #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; Listing 13.2 gqoutes.c - The Stock Market Index Client Program z = bind(s, (struct sockaddr *)&adr, len_inet); if ( z == -1 ) bail("bind(2)"); for (;;) { Broadcast address bind Broadcast 수신 loop 시작
33
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 33 Client program /* gquotes.c: * * Get datagram stock market * quotes from UDP broadcast : */ #include #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; Listing 13.2 gqoutes.c - The Stock Market Index Client Program 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 수신
34
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 34 Client program /* gquotes.c: * * Get datagram stock market * quotes from UDP broadcast : */ #include #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; Listing 13.2 gqoutes.c - The Stock Market Index Client Program fwrite(dgram,z,1,stdout); putchar(''); fflush(stdout); standard output
35
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 35 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)
36
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 36 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
37
http://netwk.hannam.ac.kr HANNAM UNIVERSITY 37 요약
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.