|
¡û ¤Ï¤¸¤á¤Ë
OSS ¤Î API ¤Î²òÀâ¤â¤¢¤ëÄøÅÙ½ª¤ï¤ê¤Þ¤·¤¿¡ÊËͤ¬»È¤¦ÄøÅ٤Τâ¤Î¤Ï¡£¤È¤¤¤¦°ÕÌ£¤Ç¤¹¡£¡£¡£¡Ë¤Î¤Ç¡¢º£²ó¤«¤é¤Ï¾¯¤·Í·¤Ù¤ë¤â¤Î¤òºî¤Ã¤Æ¹Ô¤¤Þ¤·¤ç¤¦¡ª
¤È¤¤¤¦¤ï¤±¤Ç¡¢º£²ó¤ª¤è¤Ó¼¡²ó¤Î 2 ²ó¤Ç IP ÅÅÏäâ¤É¤¤òºî¤Ã¤Æ¤ß¤¿¤¤¤È»×¤¤¤Þ¤¹¡£
²»À¼¤Î°µ½Ì¤ä¥Ñ¥±¥Ã¥È¤ÎÃÙ±ä¤Ê¤É¤Î¹Íθ¤ÏÁ´¤¯¹Ô¤¤¤Þ¤»¤ó¤Î¤Ç¡¢¤»¤¤¤¼¤¤ LAN Æâ¤Ç»ÈÍѤǤ¤ëÄøÅ٤Τâ¤Î¤Ç¤¹¡£¤È¤Ï¤¤¤Ã¤Æ¤â¡¢Î¥¤ì¤¿¾ì½ê¤ÇÄÌÏäǤ¤ë¤Î¤Ï·ë¹½¤ª¤â¤·¤í¤¤¤Î¤Ç¤¼¤Ò¤ä¤Ã¤Æ¤ß¤Æ²¼¤µ¤¤¡£
¤´¤¯´Êñ¤Ê¤â¤Î¤È¤¹¤ë¤¿¤á¤Ë¡¢Ãø¼Ô¤ÎÆÈÃǤÈÊи«¤Ë¤è¤Ã¤Æ°Ê²¼¤Î»ÅÍͤǺîÀ®¤·¤Þ¤¹¡£
- ¥¯¥é¥¤¥¢¥ó¥È¡¦¥µ¡¼¥Ð¹½À®¤È¤¹¤ë
- ¥¯¥é¥¤¥¢¥ó¥È¤Ï¥Þ¥¤¥¯¤«¤éÆþÎϤµ¤ì¤¿²»À¼¤ò¥µ¡¼¥Ð¤Ë UDP ¤ÇÁ÷¿®¤¹¤ë
- ¥µ¡¼¥Ð¤Ï¥¯¥é¥¤¥¢¥ó¥È¤«¤éÁ÷¤é¤ì¤ÆÍ褿²»À¼¤ò¥¹¥Ô¡¼¥«¤Ë½ÐÎϤ¹¤ë
- ²»À¼¥Õ¥©¡¼¥Þ¥Ã¥È¤Ï 16bit¡¢44.1KHz¡¢¥â¥Î¥é¥ë¤È¤¹¤ë
»ÅÍͤ«¤éʬ¤«¤ëÍͤËÄÌ¿®¤ÎÊý¸þ¤Ï¥¯¥é¥¤¥¢¥ó¥È¢ª¥µ¡¼¥Ð¤Î¤ß¤Ç¤¹¡£¤è¤Ã¤Æ 2Âæ¤Î PC ¤ÇÁÐÊý¸þ¤ËÄÌÏä·¤¿¤¤¾ì¹ç¤Ï¡¢¼«Ê¬¤Î PC ¤Ç¥µ¡¼¥Ð¤ò¼Â¹Ô¤·¤¿¾å¤Ç¡¢¥¯¥é¥¤¥¢¥ó¥È¤òÁê¼ê¤Î¥µ¡¼¥Ð¤ËÀܳ¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£
--- ¿Þ 1 ---
º£²ó¤Ï¥¯¥é¥¤¥¢¥ó¥È¤Î¥×¥í¥°¥é¥à¤Ë¤Ä¤¤¤Æ²òÀ⤷¤Þ¤¹¡£UDP ¥½¥±¥Ã¥ÈÄÌ¿®ÍÑ¥·¥¹¥Æ¥à¥³¡¼¥ë¤Ë¤Ä¤¤¤Æ¤â´Êñ¤Ë²òÀ⤷¤Æ¤¤¤Þ¤¹¤¬¡¢¾ÜºÙ¤Ï¥½¥±¥Ã¥È¥×¥í¥°¥é¥ß¥ó¥°´ØÏ¢¤Î½ñÀҤʤɤò»²¾È¤·¤Æ²¼¤µ¤¤¡£
¡û -¥¯¥é¥¤¥¢¥ó¥È- ¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à
¥¯¥é¥¤¥¢¥ó¥È¤Î¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à client.c ¤Ç¤¹¡£¤Ï¤¸¤á¤Ë ¤Î¥Ú¡¼¥¸¤«¤é¡¢Á´¤Æ¤Î¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ò¸Ç¤á¤¿ tar ¥Ü¡¼¥ë¤ò¥À¥¦¥ó¥í¡¼¥É¤¹¤ë¤³¤È¤â¤Ç¤¤Þ¤¹¡£
gcc -o client client.c
¤È¤·¤Æ¥³¥ó¥Ñ¥¤¥ë¤·¤Þ¤¹¡£¥³¥ó¥Ñ¥¤¥ë¥ª¥×¥·¥ç¥ó¤Ï³Æ¼«¤Î´Ä¶¤Ê¤É¤Ë±þ¤¸¤Æ½¤Àµ¤·¤Æ²¼¤µ¤¤¡£
¼¡²óÀâÌÀ¤¹¤ë¥µ¡¼¥Ð¤¬¼Â¹Ô¤µ¤ì¤Æ¤¤¤ë PC ¤Î IP ¥¢¥É¥ì¥¹ / ¤¢¤ë¤¤¤Ï̾Á°²ò·è¤¬½ÐÍè¤ë¥Û¥¹¥È̾¤ò»ØÄꤷ¤Æ¼Â¹Ô¤¹¤ë¤È¡¢UDP ¥Ý¡¼¥È¤Î 13000 ÈÖ¤ËÂФ·¤ÆÆþÎϤµ¤ì¤¿²»À¼¤òÁ÷¿®¤·»Ï¤á¤Þ¤¹¡£
--- client.c ---
1 #include <fcntl.h> 2 #include <stdio.h> 3 #include <string.h> 4 #include <sys/ioctl.h> 5 #include <sys/types.h> 6 #include <sys/socket.h> 7 #include <unistd.h> 8 #include <netdb.h> /* gethostbyname() ... */ 9 #include <netinet/in.h> /* htons() ... */ 10 #include <arpa/inet.h> /* inet_aton() ... */ 11 #include <linux/soundcard.h> 12 13 14 static int setup_dsp( int* dsp ); 15 16 static int setup_udp( 17 int* sock, struct sockaddr_in* server_addr, 18 char* host, unsigned short port ); 19 20 static int need_to_skip( unsigned char* buf, int len ); 21 22 23 /* 24 * ¥µ¡¼¥Ð¤Î UDP ¥Ý¡¼¥È 25 */ 26 #define PORT 13000 27 28 29 /* 30 * Î̻Ҳ½¥Ó¥Ã¥È¿ô : 16 bits 31 * ¥µ¥ó¥×¥ê¥ó¥°¼þÇÈ¿ô : 44.1 KHz 32 * ¥Á¥ã¥ó¥Í¥ë¿ô : 1 33 * 34 * ¥Õ¥é¥°¥á¥ó¥È¥µ¥¤¥º¤ò 100 ms ʬ¤ÎÎΰè 35 * 36 * 16 (bits) * 44100 (Hz) * 0.1 (sec) = 8820 (bytes) 37 * 38 * ¤ò±Û¤¨¤Ê¤¤ºÇÂç¤Î 2 ¤Î¶Ò¾è¿ô¤È¤¹¤ë 39 * ( SNDCTL_DSP_SETFRAGMENT ¤Î»ÅÍͤˤè¤ë ) 40 */ 41 #define FRAGSIZE 8192 42 43 44 int 45 main( int argc, char** argv ) 46 { 47 struct sockaddr_in server_addr; 48 int sock; 49 int dsp; 50 int len; 51 52 unsigned char buf[ FRAGSIZE ]; 53 54 if ( argc != 2 ) { 55 fprintf( stderr, "usage : client host\n" ); 56 return 1; 57 } 58 59 60 /* /dev/dsp ¤ÎÀßÄê */ 61 if ( setup_dsp( &dsp ) != 0 ) { 62 fprintf( stderr, "Setup /dev/dsp failed.\n" ); 63 return 1; 64 } 65 66 /* UDP ¥½¥±¥Ã¥È¤ÎÀßÄê */ 67 if ( setup_udp( &sock, &server_addr, argv[1], PORT ) != 0 ) { 68 fprintf( stderr, "Setup UDP socket failed.\n" ); 69 close( dsp ); 70 return 1; 71 } 72 73 74 /* 75 * ¥¨¥é¡¼¤¬µ¯¤³¤ë¤«¶¯À©½ªÎ»¤µ¤ì¤ë¤Þ¤Ç¥ë¡¼¥×¤·¤Þ¤¹¡£ 76 */ 77 while ( 1 ) { 78 /* 79 * FRAGSIZE ¤ÎÆþÎϤòÆɤ߹þ¤à¤Þ¤Ç¥Ö¥í¥Ã¥¯ 80 */ 81 if ( ( len = read( dsp, buf, sizeof( buf ) ) ) == -1 ) { 82 perror( "read()" ); 83 close( dsp ); 84 close( sock ); 85 return 1; 86 } 87 88 if ( ! need_to_skip( buf, len ) ) { 89 /* 90 * Æɤ߹þ¤ó¤ÀÆþÎϤò UDP ¤Ç¥µ¡¼¥Ð¤ËÁ÷¿® 91 */ 92 if ( sendto( sock, buf, len, 0, 93 ( struct sockaddr* )&server_addr, 94 sizeof( struct sockaddr_in ) ) == -1 ) { 95 96 perror( "sendto()" ); 97 close( dsp ); 98 close( sock ); 99 return 1; 100 } 101 } 102 } 103 104 /* 105 * while() ¥ë¡¼¥×¤Ï½ª¤ï¤é¤Ê¤¤¤Î¤Ç¤³¤³¤Ë¤ÏÍè¤Þ¤»¤ó¡£¡£¡£ 106 */ 107 close( dsp ); 108 close( sock ); 109 110 return 0; 111 } 112 113 114 /* 115 * /dev/dsp ¤òÀßÄꤹ¤ë 116 */ 117 static 118 int setup_dsp( int* dsp ) 119 { 120 int fmt = AFMT_S16_LE; 121 int freq = 44100; 122 int channel = 1; 123 124 int bit; 125 int i; 126 int fragarg; 127 128 129 if ( ( *dsp = open( "/dev/dsp", O_RDONLY ) ) == -1 ) { 130 perror( "open()" ); 131 return -1; 132 } 133 134 /* 135 * ¥Õ¥é¥°¥á¥ó¥È¥µ¥¤¥º¤ÎÀßÄê 136 * 137 * 0xMMMMSSSS ( MMMM ¤Ï¥Õ¥é¥°¥á¥ó¥È¿ô¡¢SSSS ¤Ï 138 * ¥Õ¥é¥°¥á¥ó¥È¥µ¥¤¥º = 2^{SSSS} ¤È¤Ê¤ë¿ô ) 139 * 140 * ¤Î·Á¼°¤ÇÀßÄꤹ¤ë¤¿¤á¡¢¤Þ¤º SSSS ¤òµá¤á¤ë¡£ 141 * 142 * ( 0 <= SSSS <= 65535, ¤¿¤À¤·¥Õ¥é¥°¥á¥ó¥È¥µ¥¤¥º 143 * ¤ÏºÇÂç¤Ç¤âÁ´¥Ð¥Ã¥Õ¥¡¥µ¥¤¥º / 2 ¤Þ¤Ç¤·¤« 144 * ÀßÄê¤Ç¤¤Ê¤¤ ) 145 * 146 * ¤Þ¤¿ MMMM ¤ÏÆäËÀ©¸Â¤òÀߤ±¤Ê¤¤¤¿¤á 0x7fff 147 * ¤òÀßÄꤹ¤ë¡£ 148 */ 149 for ( i = 0, bit = 1; i <= 65535; bit <<= 1, i ++ ) { 150 if ( FRAGSIZE < bit ) { 151 i = ( i == 0 ) ? 0 : i - 1; 152 break; 153 } 154 } 155 156 fragarg = ( 0x7fff << 16 ) | i; 157 158 if ( ioctl( *dsp, SNDCTL_DSP_SETFRAGMENT, &fragarg )== -1 ) { 159 perror( "ioctl( SNDCTL_DSP_SETFRAGMENT )" ); 160 close( *dsp ); 161 return -1; 162 } 163 164 if ( ioctl( *dsp, SOUND_PCM_SETFMT, &fmt ) == -1 ) { 165 perror( "ioctl( SOUND_PCM_SETFMT )" ); 166 close( *dsp ); 167 return -1; 168 } 169 170 if ( ioctl( *dsp, SOUND_PCM_WRITE_CHANNELS, &channel ) == -1 ) { 171 perror( "ioctl( SOUND_PCM_WRITE_CHANNELS )" ); 172 close( *dsp ); 173 return -1; 174 } 175 176 if ( ioctl( *dsp, SOUND_PCM_WRITE_RATE, &freq ) == -1 ) { 177 perror( "ioctl( SOUND_PCM_WRITE_RATE )" ); 178 close( *dsp ); 179 return -1; 180 } 181 182 return 0; 183 } 184 185 186 /* 187 * ¥µ¡¼¥Ð¤È¤ÎÄÌ¿®ÍÑ¤Ë UDP ¥½¥±¥Ã¥È¤òÀßÄꤹ¤ë 188 */ 189 static int 190 setup_udp( 191 int* sock, struct sockaddr_in* server_addr, 192 char* host, unsigned short port ) 193 { 194 struct in_addr inaddr; 195 struct hostent* ent; 196 197 if ( ( *sock = socket( AF_INET, SOCK_DGRAM, 0 ) ) == -1 ) { 198 perror( "socket()" ); 199 return -1; 200 } 201 202 /* 203 * host ¤¬ IP ¥¢¥É¥ì¥¹¤Ç¤¢¤ë¤È²¾Äꤷ¤Æ inet_aton() ¤Ë¤è¤ê 204 * ʸ»úÎóɽµ¤Î IP ¥¢¥É¥ì¥¹¤ò¥Ð¥¤¥Ê¥êÃͤËÊÑ´¹¤¹¤ë¡£ 205 * 206 * ¼ºÇÔ¤·¤¿¾ì¹ç¤Ï¥Û¥¹¥È̾»ØÄê¤Ç¤¢¤ë¤È²¾Äꤷ¡¢ 207 * gethostbyname() ¤Ë¤è¤Ã¤ÆÂбþ¤¹¤ë¥Û¥¹¥È¾ðÊó¤ò¼èÆÀ¤¹¤ë 208 * ( ¤³¤Î¾ì¹ç¡¢/etc/hosts ¥Õ¥¡¥¤¥ë¤« DNS ¤Ë¤è¤Ã¤Æ̾Á°²ò·è 209 * ¤Ç¤¤ëɬÍפ¬¤¢¤ë )¡£ 210 */ 211 if ( ! inet_aton( host, &inaddr ) ) { 212 ent = gethostbyname( host ); 213 214 if ( ent == NULL ) { 215 fprintf( stderr, 216 "Couldn't get host information specified by %s", 217 host ); 218 219 close( *sock ); 220 return -1; 221 } 222 223 inaddr = *( struct in_addr* )( ent->h_addr_list[0] ); 224 } 225 226 memset( server_addr, 0, sizeof( struct sockaddr_in ) ); 227 228 server_addr->sin_family = AF_INET; 229 server_addr->sin_port = htons( port ); 230 231 memcpy( &server_addr->sin_addr, &inaddr.s_addr, 232 sizeof( server_addr->sin_addr ) ); 233 234 return 0; 235 } 236 237 238 /* 239 * ÆþÎϥХåե¡¤òÄ´¤Ù¤Æ¡¢Ìµ²»¾õÂ֤˶ᤤ²»À¼¤«¤É¤¦¤«¤òȽÄꤹ¤ë¡£ 240 */ 241 static int 242 need_to_skip( unsigned char* buf, int len ) 243 { 244 /* 245 * ¤³¤ÎÃͰʾå¤Î¥Ç¡¼¥¿¤¬Â¸ºß¤·¤Ê¤¤¾ì¹ç¤Ï¥µ¡¼¥Ð¤ËÁ÷¿®¤·¤Ê¤¤¡£ 246 * ¼«Ê¬¤ÎÂδ¶¾å¡¢ÌäÂê̵¤¤ÄøÅÙ¤ËŬÅö¤ËÁª¤ó¤ÀÃͤǤ¹¡£¡£¡£ 247 */ 248 int threshold = 0x0040; 249 250 int i; 251 unsigned short value; 252 253 for ( i = 0; i < len; i += 2 ) { 254 memcpy( &value, &buf[i], sizeof( value ) ); 255 if ( value >= threshold ) 256 return 0; 257 } 258 259 return 1; 260 } 261 262 263 /* End of client.c */
¡û ¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ÎÀâÌÀ
¤Ç¤Ï¡¢¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Ë¤Ä¤¤¤ÆÀâÌÀ¤·¤Æ¤¤¤¤Þ¤¹¡£
- 14 ¡Á 20 ¹ÔÌÜ
¼«ºîÆâÉô´Ø¿ô¤Î¥×¥í¥È¥¿¥¤¥×Àë¸À¤Ç¤¹¡£setup_dsp() ¤Ï /dev/dsp ¤ÎÀßÄê¤ò¹Ô¤¤¤Þ¤¹¡£setup_udp() ¤Ï¡¢ ¥µ¡¼¥Ð¤È¤ÎÄÌ¿®¤ò¹Ô¤¦¤¿¤á¤Î UDP ¥½¥±¥Ã¥È¤ÎÀßÄê¤ò¹Ô¤¤¤Þ¤¹¡£ need_to_skip() ¤ÏÆþÎϤµ¤ì¤¿²»À¼¤ò¥µ¡¼¥Ð¤ËÁ÷¤ë¤Ù¤¤«¤É¤¦¤«¤ÎȽÄê¤ò¹Ô¤¤¤Þ¤¹¡£
- 23 ¡Á 26 ¹ÔÌÜ
¥µ¡¼¥Ð¤¬¥¯¥é¥¤¥¢¥ó¥È¤«¤é¤Î²»À¼Á÷¿®¤ò¼õ¤±ÉÕ¤±¤ë UDP ¥Ý¡¼¥ÈÈÖ¹æ¤òÄêµÁ¤·¤Æ¤¤¤Þ¤¹¡£
- 29 ¡Á 41 ¹ÔÌÜ
LECTURE 5 ¥ê¥¢¥ë¥¿¥¤¥àÀ¤ò¹Íθ¤¹¤ë ¤ÇÀâÌÀ¤·¤¿ fragment ¤ÎÂ礤µ¤òÄêµÁ¤·¤Æ¤¤¤Þ¤¹¡£¤³¤³¤Ç¤Ï 16bits¡¢44.1 KHz¡¢100ms ʬ¤Î¥Ð¥Ã¥Õ¥¡¥µ¥¤¥º¤È¤·¤Þ¤·¤¿¡£ ¤³¤ÎÂ礤µ¤Îñ°Ì¤Ç¡¢¥µ¡¼¥Ð¤Ø¤Î²»À¼Á÷¿®¤ò¹Ô¤¤¤Þ¤¹¡£
- 44 ¡Á 111 ¹ÔÌÜ ( main() )
main() ´Ø¿ô¤Ç¤Ï¡¢¤Þ¤º /dev/dsp ¤ÎÀßÄê ( setup_dsp() ) ¤ª¤è¤Ó UDP ¥½¥±¥Ã¥È¤ÎÀßÄê ( setup_udp() ) ¤ò¹Ô¤Ã¤¿¸å¤Ë¥Þ¥¤¥¯ÆþÎϤò¥µ¡¼¥Ð¤ËÁ÷¿®¤¹¤ë while() ¥ë¡¼¥×¤ò¼Â¹Ô¤·¤Þ¤¹¡£
while() ¥ë¡¼¥×¤Ç¤Ï¡¢¤Þ¤º¥Þ¥¤¥¯¤«¤é¤ÎÆþÎϤò FRAGSIZE ʬ read() ¤ÇÆɤ߹þ¤ß¡¢Æɤ߹þ¤ó¤À²»À¼¤ò need_to_skip() ¤Ë¤è¤Ã¤Æ¥µ¡¼¥Ð¤ËÁ÷¿®¤¹¤ë¤Ù¤¤«¤É¤¦¤«È½Äꤷ¤Æ¤¤¤Þ¤¹¡£ Á÷¿®¤¹¤Ù¤¤ÈȽÄꤷ¤¿¾ì¹ç¤Ï¡¢sendto() ( ¥½¥±¥Ã¥È´ØÏ¢¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë ) ¤Ç¥µ¡¼¥Ð¤ËÁ÷¿®¤·¤Æ¤¤¤Þ¤¹¡£
#include <sys/types.h> #include <sys/socket.h> int sendto( int s, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen ); s : socket() ¥·¥¹¥Æ¥à¥³¡¼¥ë¤ÇºîÀ®¤·¤¿¥½¥±¥Ã¥È msg : Á÷¿®¥Ç¡¼¥¿¥Ð¥Ã¥Õ¥¡¤Ø¤Î¥Ý¥¤¥ó¥¿ len : Á÷¿®¥Ç¡¼¥¿Ä¹ flags : ¥½¥±¥Ã¥ÈÄÌ¿®Íѥե饰¡£¾ÜºÙ¤Ï¥Þ¥Ë¥å¥¢¥ë»²¾È¡£¤³¤³¤Ç¤Ï»ÈÍѤ·¤Ê¤¤¤¿¤á 0 ¤òÀßÄê to : Á÷¿®Àè¾ðÊó¤Ø¤Î¥Ý¥¤¥ó¥¿ tolen : to ¤Î¼Â¥Ç¡¼¥¿¤ÎÂ礤µ
¥¨¥é¡¼¤¬È¯À¸¤·¤¿¾ì¹ç¤Ï -1 ¤¬ÊÖ¤µ¤ì¡¢¤½¤ì°Ê³°¤ÏÁ÷¿®¤µ¤ì¤¿¥Ç¡¼¥¿Ä¹¤¬ÊÖ¤µ¤ì¤Þ¤¹¡£
- 114 ¡Á 183 ¹ÔÌÜ ( setup_dsp() )
/dev/dsp ¤ÎÀßÄê¤ò¹Ô¤Ã¤Æ¤¤¤Þ¤¹¡£134 ¡Á 162 ¹ÔÌÜ¤Ç fragment ¤ÎÂ礤µ¤òÀßÄꤷ¤Æ¤¤¤Þ¤¹¡£
fragment ¤ÎÂ礤µ¤Ï 0xMMMMSSSS ( MMMM ¤Ï fragment ¿ô¡¢SSSS ¤Ï fragment ¥µ¥¤¥º = 2^{SSSS} ¤È¤Ê¤ë¿ô ) ¤Î·Á¼°¤ÇÀßÄꤹ¤ë¤¿¤á¡¢SSSS ¤ò 149 ¡Á 154 ¹ÔÌܤǵá¤á¤Æ¤¤¤Þ¤¹¡£MMMM ¤Ë¤ÏÆäËÀ©¸Â¤òÀߤ±¤Ê¤¤¤¿¤á 0x7fff ¤È¤·¤Þ¤¹¡£
156 ¹ÔÌÜ¤Ç SSSS ¤ª¤è¤Ó MMMM ¤ò fragarg ¤ËÀßÄꤷ¡¢158 ¡Á 162 ¹ÔÌܤΠioctl() ¤Ç¼ÂºÝ¤Ë fragment ¤ÎÂ礤µ¤òÀßÄꤷ¤Æ¤¤¤Þ¤¹¡£¤³¤³¤Ç¤Ï fragment ¤¬¤Á¤ã¤ó¤ÈÀßÄꤵ¤ì¤¿¤«¤É¤¦¤«¤ò³Îǧ¤·¤Æ¤¤¤Þ¤»¤ó¤¬¡¢¼ÂºÝ¤Ë¤Ï ioctl SNDCTL_DSP_GETBLKSIZE ¥³¥Þ¥ó¥É¤Ç³Îǧ¤·¤¿Êý¤¬Îɤ¤¤«¤â¤·¤ì¤Þ¤»¤ó (LECTURE 5 ¥ê¥¢¥ë¥¿¥¤¥àÀ¤ò¹Íθ¤¹¤ë »²¾È)¡£
¤½¤ì°Ê¹ß¤Î ioctl() ¤Ï¤³¤ì¤Þ¤Ç¤ËÀâÌÀ¤·¤¿Ä̤ê¤Ç¤¹¡£
- 186 ¡Á 235 ¹ÔÌÜ ( setup_udp() )
¥µ¡¼¥Ð¤È¤ÎÄÌ¿®¤Ë»ÈÍѤ¹¤ë¡¢UDP ¥½¥±¥Ã¥È¤ÎÀßÄê¤ò¹Ô¤Ã¤Æ¤¤¤Þ¤¹¡£
197 ¡Á 200 ¹ÔÌܤǤϡ¢socket() ¥·¥¹¥Æ¥à¥³¡¼¥ë¤Ë¤è¤Ã¤ÆÄÌ¿®¤Ë»ÈÍѤ¹¤ë¥½¥±¥Ã¥È¤Î½àÈ÷¤ò¹Ô¤Ã¤Æ¤¤¤Þ¤¹¡£
#include <sys/types.h> #include <sys/socket.h> int socket( int domain, int type, int protocol ); domain : IP ÄÌ¿®¤Î¾ì¹ç¤Ï PF_INET ¤â¤·¤¯¤Ï AF_INET ¤ò»ØÄꤹ¤ë type : UDP ¤Î¾ì¹ç¤Ï SOCK_DGRAM ¤ò»ØÄꤹ¤ë protocol : ¤³¤³¤Ç¤Ï 0 ¤ò»ØÄꤷ¤Æ²¼¤µ¤¤
À®¸ù¤¹¤ë¤È¡¢½àÈ÷¤µ¤ì¤¿¥½¥±¥Ã¥È¤ò»²¾È¤¹¤ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬ÊÖ¤µ¤ì¤Þ¤¹¡£ °Ê¸å¤Ï¤³¤ÎÃͤò»ÈÍѤ·¤Æ¥½¥±¥Ã¥È¤ò»ØÄꤷ¤Þ¤¹¡£¥¨¥é¡¼¤¬È¯À¸¤·¤¿¾ì¹ç¤Ï -1 ¤¬ÊÖ¤µ¤ì¤Þ¤¹¡£
202 ¡Á 232 ¹ÔÌܤǤϡ¢Á÷¿®Àè¤Î¾ðÊó¤ò struct sockaddr_in ·¿¹½Â¤ÂΤ˳ÊǼ¤·¤Æ¤¤¤Þ¤¹¡£ ´û¤Ë½Ð¤ÆÍ褿 sendto() ¥·¥¹¥Æ¥à¥³¡¼¥ë¤Ç¤Ï¡¢¤³¤Î¹½Â¤ÂΤòÍøÍѤ·¤ÆÁ÷¿®Àè¤ò·èÄꤷ¤Þ¤¹¡£
¤Þ¤º°ú¿ô¤ËÍ¿¤¨¤é¤ì¤¿Á÷¿®Àè̾¾Î¤¬ IP ¥¢¥É¥ì¥¹¤Ç¤¢¤ë¤È²¾Äꤷ¤Æ inet_aton() ¤ò¼Â¹Ô¤·¤Þ¤¹ ( 211 ¹ÔÌÜ )¡£
#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> int inet_aton( const char *cp, struct in_addr *inp ); cp : ¥É¥Ã¥ÈÉÕ¤ 10 ¿Êɽ¼¨¤Î IP ¥¢¥É¥ì¥¹ ( 192.168.0.1 ¤Ê¤É ) inp : 32¥Ó¥Ã¥È¥Ð¥¤¥Ê¥êÃͤËÊÑ´¹¤µ¤ì¤¿ IP ¥¢¥É¥ì¥¹¤ò³ÊǼ¤¹¤ë¹½Â¤ÂÎ
ÊÖ¤êÃͤ¬ 0 °Ê³°¤Ç¤¢¤ì¤ÐÊÑ´¹¤¬Àµ¾ï¤Ë½ªÎ»¤·¤Æ¤¤¤Þ¤¹¡£0 ¤¬Ê֤äÆÍ褿¾ì¹ç¤Ï¡¢ Á÷¿®Àè̾¾Î¤¬¥Û¥¹¥È̾¤Ç¤¢¤Ã¤¿¤ÈȽÃǤ·¤Æ¡¢gethostbyname() ¤ò»È¤Ã¤Æ̾Á°²ò·è¤ò¹Ô¤¤ IP ¥¢¥É¥ì¥¹¤ò¼èÆÀ¤·¤Þ¤¹ ( 212 ¹ÔÌÜ )¡£
#include <netdb.h> struct hostent *gethostbyname( const char *name ); name : ¥Û¥¹¥È̾
NULL ¤¬ÊÖ¤µ¤ì¤¿¾ì¹ç¤Ï̾Á°²ò·è¤¬½ÐÍè¤Ê¤«¤Ã¤¿¤³¤È¤ò°ÕÌ£¤·¤Þ¤¹ ( 214 ¡Á 221 ¹ÔÌÜ )¡£ NULL °Ê³°¤¬ÊÖ¤µ¤ì¤¿¾ì¹ç¤Ï̾Á°²ò·è¤Î·ë²Ì¤¬ÊÖ¤µ¤ì¤Æ¤¤¤Þ¤¹¤Î¤Ç¡¢Êݸ¤·¤Þ¤¹ ( 223 ¹ÔÌÜ )¡£
228 ¹ÔÌܤǥµ¡¼¥Ð¤È¤ÎÄÌ¿®¤ËÍѤ¤¤ë¥×¥í¥È¥³¥ë¤ò ( IP ¤Î¾ì¹ç¤Ï PF_INET ¤â¤·¤¯¤Ï AF_INET )¡¢ 229 ¹ÔÌܤǥݡ¼¥ÈÈÖ¹æ¤òÁ÷¿®Àè¾ðÊó¤È¤·¤Æ struct sockaddr_in ·¿¤Î¹½Â¤ÂΤ˳ÊǼ¤·¤Æ¤¤¤Þ¤¹¡£ ¥Ý¡¼¥ÈÈÖ¹æ¤Ë¤Ä¤¤¤Æ¤Ï¡¢¥Í¥Ã¥È¥ï¡¼¥¯¥Ð¥¤¥È¥ª¡¼¥À¤È¤¤¤¦·Á¼°¤Ç¿ôÃͤò³ÊǼ¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¤Î¤Ç¡¢ unsigned short ·¿¤Î¿ôÃͤò¥Í¥Ã¥È¥ï¡¼¥¯¥Ð¥¤¥È¥ª¡¼¥À¤ËÊÑ´¹¤¹¤ë htons() ´Ø¿ô¤ò»È¤Ã¤Æ¤¤¤Þ¤¹¡£
ºÇ¸å¤Ë 231 ¡Á 232 ¹ÔÌܤÇÁ÷¿®Àè IP ¥¢¥É¥ì¥¹¤Î 32 ¥Ó¥Ã¥È¥Ð¥¤¥Ê¥êÃͤò³ÊǼ¤·¤Æ¤¤¤Þ¤¹¡£
- 238 ¡Á 260 ¹ÔÌÜ ( need_to_skip() )
ÆþÎϤµ¤ì¤¿²»À¼¤ò¥µ¡¼¥Ð¤ËÁ÷¿®¤¹¤Ù¤¤«¤É¤¦¤«¤ÎȽÄê½èÍý¤ò¹Ô¤Ã¤Æ¤¤¤Þ¤¹¡£¤È¤¤¤Ã¤Æ¤â¡¢ ÆþÎϤµ¤ì¤¿¥Ç¡¼¥¿¤¬¤¢¤ë°ìÄê¤ÎÂ礤µÌ¤Ëþ¤«¤É¤¦¤«¤òȽÄꤷ¤Æ¤¤¤ë¤À¤±¤Ç¤¹¡£
¤³¤Î½èÍý¤ò¹Ô¤ï¤Ê¤¤¤È¡¢²¿¤âÆþÎϤ·¤Æ¤¤¤Ê¤¤Ìµ²»¾õÂÖ ( ¼ÂºÝ¤Ë¤Ï¥Î¥¤¥º¤Ê¤É¤Î²»¤¬Æþ¤ê¹þ¤ó¤Ç¤¤¤ë ) ¤Ç¤â¥µ¥¦¥ó¥É¥«¡¼¥É¤«¤é¥Ç¡¼¥¿¤ò¼è¤ê¹þ¤ß¥µ¡¼¥Ð¤ËÁ÷¿®¤·¤Æ¤·¤Þ¤¦¤¿¤á¡¢ ¥Í¥Ã¥È¥ï¡¼¥¯¤ÎÉé²Ù¤¬Èó¾ï¤Ë¹â¤¯¤Ê¤Ã¤Æ¤·¤Þ¤¤¤Þ¤¹¡£
¤Þ¤¢¡¢Á÷¿®¤¹¤ë²»À¼¤Î°µ½Ì½èÍý¤Ê¤É¤ò¹Ô¤Ã¤Æ¤¤¤Ê¤¤¤Î¤Ç¡¢ Ï䷤äѤʤ·¤Î¾ì¹ç¤Ï¤¢¤Þ¤ê°ÕÌ£¤¬Ìµ¤¤¤Î¤Ç¤¹¤¬µ¤µÙ¤áÄøÅ٤ˡ£¡£¡£
¤Á¤Ê¤ß¤Ë 0x0040 ¤È¤¤¤¦Ãͤϼ¸³¤·¤ÆŬÅö¤ËÁª¤ó¤ÀÃͤǤ¹¡£¤Á¤ã¤ó¤È¤ä¤ë¤Ê¤é¤Ð¡¢Ìµ²»¾õÂ֤ΠÆþÎÏÃͤòËè²óÀßÄê¤Ç¤¤ëÍͤˤ¹¤ëɬÍפ¬¤¢¤ë¤Ç¤·¤ç¤¦¡£
client.c ¤ÎÀâÌÀ¤Ï°Ê¾å¤Ç¤¹¡£IP ÅÅÏäμ¹Ԥϼ¡²ó¤Î¥µ¡¼¥Ð¤ÎÀâÌÀ¤Þ¤Ç¤ªÂÔ¤Á²¼¤µ¤¤ m(__)m