Broadcasting and UDP Network Lab. 남 재 준. HANNAM UNIVERSITY 2 목 차  Broadcast UDP 소켓 설치  소켓을 사용하여 broadcast message 보내기  소켓을.

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Socket Programming CS3320 Fall 2010.
Chapter 10 IPv4 and IPv6 Interoperability. contents Introduction IPv4 Client, IPv6 Server IPv6 Client, IPv4 Server IPv6 Address Testing Macros IPV6_ADDRFORM.
CSE 333 – SECTION 8 Networking and sockets. Overview Network Sockets IP addresses and IP address structures in C/C++ DNS – Resolving DNS names Demos.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
Lab tutorial Lab assignment 2 Fall 2006
Socket Programming Application Programming Interface.
Multicasting - Part II© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
UDP and Multi-thread Socket Programming
תקשורת באינטרנט Tutorial 8. 2 n Socket programming u What is socket ? u Sockets architecture u Types of Sockets u The Socket system calls u Data Transfer.
Tutorial 8 Socket Programming
1 Example (UDP Client) // This program sends UDP packets to the given address #include #define SERVER_ADDR " " #define SERVER_PORT 5555 void error(char.
The Basics of communication LectureII. Processing Techniques.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
Cs423-cotter1 Example Client Program Reference Comer & Stevens, Chapter 7.
Broadcast and Multicast. Unicast Host 2Host 1 Broadcast Packet received by every host on network (including the sender!)
UNIX Socket Programming CS 6378
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
CS1652 September 13th, 2012 The slides are adapted from the publisher’s material All material copyright J.F Kurose and K.W. Ross, All Rights.
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Class A Addresses The 1 st bit of a class A address is 0 The 1 st byte has a value from (128.x.x.x would not be a class A) 127.x.x.x is reserved.
The Application Layer Application Services (Telnet, FTP, , WWW) Reliable Stream Transport (TCP) Connectionless Packet Delivery Service (IP) Unreliable.
Sockets API Overview Sockets with UDP Sockets with TCP Fast Sockets (Fast UDP) IP Multicasting.
Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011.
Network Programming Eddie Aronovich mail:
Ports Port - A 16-bit number that identifies the application process that receives an incoming message. Reserved ports or well-known ports (0 to 1023)
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol ,
Socket Programming Lab 1 1CS Computer Networks.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
CS 6401 Introduction to Computer Networks 09/21/2010 Outline - UNIX sockets - A simple client-server program - Project 1 - LAN bridges and learning.
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
Socket Program Training 10/29/ TCP Client Socket ( ) Connect ( ) send ( ) Close ( ) send ( ) Read ( ) Accept ( ) recv ( ) Listen ( ) Bind ( ) Socket.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
UNIX Sockets Outline UNIX sockets CS 640.
Department of Computer Science Southern Illinois University Edwardsville Spring, 2010 Dr. Hiroshi Fujinoki CS 547/490 Network.
Socket programming in C. Socket programming with TCP Client must contact server server process must first be running server must have created socket (door)
1 COMP 431 Internet Services & Protocols The IP Internet Protocol Jasleen Kaur April 21, 2016.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
UDP. User Datagram Protocol (UDP)  Unreliable and unordered datagram service  Adds multiplexing  No flow control  Endpoints identified by ports 
UNIX Sockets COS 461 Precept 1.
CS 1652 Jack Lange University of Pittsburgh
Elementary UDP Sockets
Socket Programming in C
CSCD433 Advanced Networks Spring 2016 Lecture 16a
Transport layer API: Socket Programming
UNIX Sockets Outline Homework #1 posted by end of day
Chapter 07. Socket Options.
Socket Programming in C
Chapter 06. UDP Server/Client.
Socket Programming(1/2)
Socket Programming with UDP
Presentation transcript:

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 요약