Broadcasting and UDP Network Lab. 남 재 준
HANNAM UNIVERSITY 2 목 차 Broadcast UDP 소켓 설치 소켓을 사용하여 broadcast message 보내기 소켓을 사용하여 broadcast message 받기
HANNAM UNIVERSITY 3 Broadcast Address ifconfig eth0 # ifconfig eth0 eth0 Link encap:Ehternet Hwaddr 00:A0:4B:06:F4:8D inet addr: Bcast: Mask: 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
HANNAM UNIVERSITY 4 Broadcast Address ifconfig eth0 # ifconfig eth0 eth0 Link encap:Ehternet Hwaddr 00:A0:4B:06:F4:8D inet addr: Bcast: Mask: 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 IDHost ID - C Class address
HANNAM UNIVERSITY 5 Broadcast Address broadcasting 을 위해서 사용될 수 있다. 대량의 broadcast 를 제한하고 있다. 개념정의가 미비한 상태 모든 network interface 들을 위해 사용 여러 interface 중 단 하나를 선택 일반적인 broadcast address 사용이 제지 된다.
HANNAM UNIVERSITY 6 Broadcast 하기 위해 Interface 이름 결정 interface의 broadcast address 결정 broadcast address를 broadcast 추가된 interface에 반복
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 ( ) 와 bad IP address 의 구별을 위해 Invalid address INADDR_NONE (32bit convert) INADDR_NONE
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 ( )
HANNAM UNIVERSITY 9 Broadcasting from a Server Socket은 SO_BROADCAST option을 가 지고 있어야 한다. sendto( ) 함수의 destination address Broadcast address
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
HANNAM UNIVERSITY 11 Receiving Broadcasting Broadcast를 수신하기 위해서 client program은 Socket에 bind된 address를 가지고 있어야 한다. SO_REUSEADDR multiple client 가 같은 host 에 같은 broadcast address 로부터 수신하도록 허락
HANNAM UNIVERSITY 12 Broadcast $./stksrv :9097 & [1] $
HANNAM UNIVERSITY 13 Broadcast Demo. $./gquotes :9097 DJIA NASDQ S&P TSE DJIA NASDQ S&P TSE DJIA NASDQ S&P TSE $
HANNAM UNIVERSITY 14 Broadcast Demo. (to $./stksrv :9097 ' :*' & [2] $
HANNAM UNIVERSITY 15 Broadcast Demo. (to ( ) $./gquotes :9097 DJIA NASDAQ S&P TSE DJIA NASDAQ S&P TSE ( ) $./gquotes :9097 DJIA NASDQ S&P TSE DJIA NASDQ S&P TSE
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", , 375 }, { "NASDAQ", , 125}, { "S&P 500", , 50 }, { "TSE 300", , 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", , 375 }, { "NASDAQ", , 125}, { "S&P 500", , 50 }, { "TSE 300", , 75 }, } ; Stock Market Index table Simulation 을 위한 data 구성
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", , 375 }, { "NASDAQ", , 125}, { "S&P 500", , 50 }, { "TSE 300", , 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 산출
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", , 375 }, { "NASDAQ", , 125}, { "S&P 500", , 50 }, { "TSE 300", , 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
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 = " :*", *bc_addr = " :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 = " :*", *bc_addr = " :9097"; Default address 선언 No command-line argument
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 = " :*", *bc_addr = " :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 를 취함
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 = " :*", *bc_addr = " :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 형성
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 = " :*", *bc_addr = " :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 형성
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 = " :*", *bc_addr = " :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
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 = " :*", *bc_addr = " :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
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 = " :*", *bc_addr = " :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 시작
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 = " :*", *bc_addr = " :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 로 추출
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 = " :*", *bc_addr = " :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 를 내포
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 = " :*", *bc_addr = " :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
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 = " :9097"; /* * Use a server address from the command * line, if one has been provided. * Otherwise, this program will default * to using the arbitrary address * : */ 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 = " :9097"; Default Broadcast address UDP port number
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 = " :9097"; /* * Use a server address from the command * line, if one has been provided. * Otherwise, this program will default * to using the arbitrary address * : */ 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 형성
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 = " :9097"; /* * Use a server address from the command * line, if one has been provided. * Otherwise, this program will default * to using the arbitrary address * : */ 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
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 = " :9097"; /* * Use a server address from the command * line, if one has been provided. * Otherwise, this program will default * to using the arbitrary address * : */ 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 시작
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 = " :9097"; /* * Use a server address from the command * line, if one has been provided. * Otherwise, this program will default * to using the arbitrary address * : */ 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 수신
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 = " :9097"; /* * Use a server address from the command * line, if one has been provided. * Otherwise, this program will default * to using the arbitrary address * : */ 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
HANNAM UNIVERSITY 35 Demo. $./stksrv :9097 & $./gquotes :9097 $./stksrv :9097 ‘ :* ’ & $./gqoutes (pepper) $./gqoutes (slug)
HANNAM UNIVERSITY 36 Troubleshooting Hub 탐색 packet traffic 표시 LED flicker 확인 tcpdump command # tcpdump udp port 9097 tcpdump: listening on eth0 21:04:43, pepper.ve3wwg.or.1032 > : udp 96 21:04:47, pepper.ve3wwg.or.1032 > : udp 96 21:04:51, pepper.ve3wwg.or.1032 > : udp 96
HANNAM UNIVERSITY 37 요약