|
¡û ¤Ï¤¸¤á¤Ë
Á°²ó ¤Ë°ú³¤¡¢¥µ¡¼¥Ð¦¤Î¥×¥í¥°¥é¥à¤òºîÀ®¤·¤Þ¤¹¡£º£²ó¤â UDP ¥½¥±¥Ã¥ÈÄÌ¿®ÍÑ¥·¥¹¥Æ¥à¥³¡¼¥ë¤ò»È¤Ã¤Æ¤¤¤Þ¤¹¡£´Êñ¤Ë¤Ï²òÀ⤷¤Æ¤¤¤Þ¤¹¤¬¡¢¾ÜºÙ¤Ï¥½¥±¥Ã¥È¥×¥í¥°¥é¥ß¥ó¥°´ØÏ¢¤Î½ñÀҤʤɤò»²¾È¤·¤Æ²¼¤µ¤¤¡£
¡û -¥µ¡¼¥Ð- ¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à
¥µ¡¼¥Ð¤Î¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à server.c ¤Ç¤¹¡£¤Ï¤¸¤á¤Ë ¤Î¥Ú¡¼¥¸¤«¤é¡¢Á´¤Æ¤Î¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ò¸Ç¤á¤¿ tar ¥Ü¡¼¥ë¤ò¥À¥¦¥ó¥í¡¼¥É¤¹¤ë¤³¤È¤â¤Ç¤¤Þ¤¹¡£
gcc -o server server.c
¤È¤·¤Æ¥³¥ó¥Ñ¥¤¥ë¤·¤Þ¤¹¡£¥³¥ó¥Ñ¥¤¥ë¥ª¥×¥·¥ç¥ó¤Ï³Æ¼«¤Î´Ä¶¤Ê¤É¤Ë±þ¤¸¤Æ½¤Àµ¤·¤Æ²¼¤µ¤¤¡£
¼Â¹Ô¤¹¤ë¤È¡¢UDP ¥Ý¡¼¥È¤Î 13000 ÈÖ¤ËÂФ·¤ÆÁ÷¿®¤µ¤ì¤¿²»À¼¥Ñ¥±¥Ã¥È¤ò¼õ¿®¤·¤Æ¥¹¥Ô¡¼¥«¤Ë½ÐÎϤ·¤Þ¤¹¡£¥µ¡¼¥Ð¤òµ¯Æ°¤·¤¿¾å¤ÇÁ°²óºîÀ®¤·¤¿¥¯¥é¥¤¥¢¥ó¥È¤ò¼Â¹Ô¤·¡¢¥¯¥é¥¤¥¢¥ó¥È PC ¤Ç¥Þ¥¤¥¯¤Ë¸þ¤«¤Ã¤Æ²¿¤«Ïä·¤¿¤ê²Î¤Ã¤¿¤ê¤·¤Æ¤ß¤Æ²¼¤µ¤¤¡£¥µ¡¼¥Ð PC ¤Î¥¹¥Ô¡¼¥«¤«¤éƱ¤¸²»¤¬½Ð¤ÆÍè¤Þ¤·¤¿¤«¡© Â礷¤¿¥×¥í¥°¥é¥à¤Ç¤Ï¤Ê¤¤¤Ç¤¹¤¬¡¢¼«Ê¬¤Çºî¤Ã¤¿¥×¥í¥°¥é¥à¤¬¤¦¤Þ¤¯Æ°¤¤¤¿»þ¤Î´ò¤·¤µ¤ò¼Â´¶¤Ç¤¤ë¤È»×¤¤¤Þ¤¹¡£
--- server.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 static int setup_udp( int* sock, unsigned short port ); 16 17 18 /* 19 * ¥µ¡¼¥Ð¤Î UDP ¥Ý¡¼¥È 20 */ 21 #define PORT 13000 22 23 24 /* 25 * Î̻Ҳ½¥Ó¥Ã¥È¿ô : 16 bits 26 * ¥µ¥ó¥×¥ê¥ó¥°¼þÇÈ¿ô : 44.1 KHz 27 * ¥Á¥ã¥ó¥Í¥ë¿ô : 1 28 * 29 * ¥Õ¥é¥°¥á¥ó¥È¥µ¥¤¥º¤ò 100 ms ʬ¤ÎÎΰè 30 * 31 * 16 (bits) * 44100 (Hz) * 0.1 (sec) = 8820 (bytes) 32 * 33 * ¤ò±Û¤¨¤Ê¤¤ºÇÂç¤Î 2 ¤Î¶Ò¾è¿ô¤È¤¹¤ë 34 * ( SNDCTL_DSP_SETFRAGMENT ¤Î»ÅÍͤˤè¤ë ) 35 */ 36 #define FRAGSIZE 8192 37 38 39 int 40 main( int argc, char** argv ) 41 { 42 struct sockaddr_in client_addr; 43 int sock; 44 int dsp; 45 int len; 46 int addr_len; 47 48 unsigned char buf[ FRAGSIZE ]; 49 50 51 /* /dev/dsp ¤ÎÀßÄê */ 52 if ( setup_dsp( &dsp ) != 0 ) { 53 fprintf( stderr, "Setup /dev/dsp failed.\n" ); 54 return 1; 55 } 56 57 /* UDP ¥½¥±¥Ã¥È¤ÎÀßÄê */ 58 if ( setup_udp( &sock, PORT ) != 0 ) { 59 fprintf( stderr, "Setup UDP socket failed.\n" ); 60 close( dsp ); 61 return 1; 62 } 63 64 addr_len = sizeof( struct sockaddr_in ); 65 66 /* 67 * ¥¨¥é¡¼¤¬µ¯¤³¤ë¤«¶¯À©½ªÎ»¤µ¤ì¤ë¤Þ¤Ç¥ë¡¼¥×¤·¤Þ¤¹¡£ 68 */ 69 while ( 1 ) { 70 /* 71 * UDP ¥½¥±¥Ã¥ÈÆþÎϤòÆɤ߹þ¤à¤Þ¤Ç¥Ö¥í¥Ã¥¯ 72 */ 73 if ( ( len = recvfrom( sock, buf, sizeof( buf ), 0, 74 ( struct sockaddr* )&client_addr, &addr_len ) ) == -1 ) { 75 76 perror( "recvfrom()" ); 77 close( dsp ); 78 close( sock ); 79 return 1; 80 } 81 82 /* 83 * /dev/dsp ¤Ë½ñ¤¹þ¤à 84 */ 85 if ( write( dsp, buf, len ) == -1 ) { 86 perror( "write()" ); 87 close( dsp ); 88 close( sock ); 89 return 1; 90 } 91 } 92 93 94 /* 95 * while() ¥ë¡¼¥×¤Ï½ª¤ï¤é¤Ê¤¤¤Î¤Ç¤³¤³¤Ë¤ÏÍè¤Þ¤»¤ó¡£¡£¡£ 96 */ 97 close( dsp ); 98 close( sock ); 99 100 return 0; 101 } 102 103 104 /* 105 * /dev/dsp ¤òÀßÄꤹ¤ë 106 */ 107 static int 108 setup_dsp( int* dsp ) 109 { 110 int fmt = AFMT_S16_LE; 111 int freq = 44100; 112 int channel = 1; 113 114 int bit; 115 int i; 116 int fragarg; 117 118 119 if ( ( *dsp = open( "/dev/dsp", O_WRONLY ) ) == -1 ) { 120 perror( "open()" ); 121 return -1; 122 } 123 124 /* 125 * ¥Õ¥é¥°¥á¥ó¥È¥µ¥¤¥º¤ÎÀßÄê 126 * 127 * 0xMMMMSSSS ( MMMM ¤Ï¥Õ¥é¥°¥á¥ó¥È¿ô¡¢SSSS ¤Ï 128 * ¥Õ¥é¥°¥á¥ó¥È¥µ¥¤¥º = 2^{SSSS} ¤È¤Ê¤ë¿ô ) 129 * 130 * ¤Î·Á¼°¤ÇÀßÄꤹ¤ë¤¿¤á¡¢¤Þ¤º SSSS ¤òµá¤á¤ë¡£ 131 * 132 * ( 0 <= SSSS <= 65535, ¤¿¤À¤·¥Õ¥é¥°¥á¥ó¥È¥µ¥¤¥º 133 * ¤ÏºÇÂç¤Ç¤âÁ´¥Ð¥Ã¥Õ¥¡¥µ¥¤¥º / 2 ¤Þ¤Ç¤·¤« 134 * ÀßÄê¤Ç¤¤Ê¤¤ ) 135 * 136 * ¤Þ¤¿ MMMM ¤ÏÆäËÀ©¸Â¤òÀߤ±¤Ê¤¤¤¿¤á 0x7fff 137 * ¤òÀßÄꤹ¤ë¡£ 138 */ 139 for ( i = 0, bit = 1; i <= 65535; bit <<= 1, i ++ ) { 140 if ( FRAGSIZE < bit ) { 141 i = ( i == 0 ) ? 0 : i - 1; 142 break; 143 } 144 } 145 146 fragarg = ( 0x7fff << 16 ) | i; 147 148 if ( ioctl( *dsp, SNDCTL_DSP_SETFRAGMENT, &fragarg )== -1 ) { 149 perror( "ioctl( SNDCTL_DSP_SETFRAGMENT )" ); 150 close( *dsp ); 151 return -1; 152 } 153 154 if ( ioctl( *dsp, SOUND_PCM_SETFMT, &fmt ) == -1 ) { 155 perror( "ioctl( SOUND_PCM_SETFMT )" ); 156 close( *dsp ); 157 return -1; 158 } 159 160 if ( ioctl( *dsp, SOUND_PCM_WRITE_CHANNELS, &channel ) == -1 ) { 161 perror( "ioctl( SOUND_PCM_WRITE_CHANNELS )" ); 162 close( *dsp ); 163 return -1; 164 } 165 166 if ( ioctl( *dsp, SOUND_PCM_WRITE_RATE, &freq ) == -1 ) { 167 perror( "ioctl( SOUND_PCM_WRITE_RATE )" ); 168 close( *dsp ); 169 return -1; 170 } 171 172 return 0; 173 } 174 175 176 /* 177 * ¥¯¥é¥¤¥¢¥ó¥È¤È¤ÎÄÌ¿®ÍÑ¤Ë UDP ¥½¥±¥Ã¥È¤òÀßÄꤹ¤ë 178 */ 179 static int 180 setup_udp( int* sock, unsigned short port ) 181 { 182 struct sockaddr_in server_addr; 183 184 if ( ( *sock = socket( AF_INET, SOCK_DGRAM, 0 ) ) == -1 ) { 185 perror( "socket()" ); 186 return -1; 187 } 188 189 memset( &server_addr, 0, sizeof( struct sockaddr_in ) ); 190 191 server_addr.sin_family = AF_INET; 192 server_addr.sin_addr.s_addr = INADDR_ANY; 193 server_addr.sin_port = htons( port ); 194 195 if ( bind( *sock, ( struct sockaddr* )&server_addr, 196 sizeof( struct sockaddr_in ) ) == -1 ) { 197 198 perror( "bind()" ); 199 close( *sock ); 200 return -1; 201 } 202 203 return 0; 204 } 205 206 207 /* End of server.c */
¡û ¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ÎÀâÌÀ
¤Ç¤Ï¡¢¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Ë¤Ä¤¤¤ÆÀâÌÀ¤·¤Æ¤¤¤¤Þ¤¹¡£
- 14 ¡Á 15 ¹ÔÌÜ
¼«ºîÆâÉô´Ø¿ô¤Î¥×¥í¥È¥¿¥¤¥×Àë¸À¤Ç¤¹¡£setup_dsp() ¤Ï /dev/dsp ¤ÎÀßÄê¤ò¹Ô¤¤¤Þ¤¹¡£setup_udp() ¤Ï¡¢ ¥¯¥é¥¤¥¢¥ó¥È¤È¤ÎÄÌ¿®¤ò¹Ô¤¦¤¿¤á¤Î UDP ¥½¥±¥Ã¥È¤ÎÀßÄê¤ò¹Ô¤¤¤Þ¤¹¡£
- 18 ¡Á 21 ¹ÔÌÜ
¥¯¥é¥¤¥¢¥ó¥È¤«¤é¤Î²»À¼¤ò¼õ¿®¤¹¤ë¤¿¤á¤Î UDP ¥Ý¡¼¥ÈÈÖ¹æ¤òÄêµÁ¤·¤Æ¤¤¤Þ¤¹¡£ ¤³¤ÎÃͤϥ¯¥é¥¤¥¢¥ó¥È¤ÎÃͤȰìÃפ·¤Æ¤¤¤Æ¤«¤Ä¾¤Î¥½¥Õ¥È¥¦¥§¥¢¤«¤é»ÈÍѤµ¤ì¤Æ¤¤¤Ê¤±¤ì¤Ð¡¢ ¤É¤ó¤ÊÃͤǤ⹽¤¤¤Þ¤»¤ó¡£
- 24 ¡Á 36 ¹ÔÌÜ
Á°²ó ¤Î client.c ¤Î FRAGSIZE ¤ÈƱ¤¸ÃͤΠfragment ¥µ¥¤¥º¤òÄêµÁ¤·¤Æ¤¤¤Þ¤¹¡£16bits¡¢44.1 KHz¡¢100ms ʬ¤Î¥Ð¥Ã¥Õ¥¡¥µ¥¤¥º¤Ç¤¹¡£
- 39 ¡Á 101 ¹ÔÌÜ ( main() )
main() ´Ø¿ô¤Ç¤Ï¡¢¤Þ¤º /dev/dsp ¤ÎÀßÄê ( setup_dsp() ) ¤ª¤è¤Ó UDP ¥½¥±¥Ã¥È¤ÎÀßÄê ( setup_udp() ) ¤ò¹Ô¤Ã¤¿¸å¤Ë¡¢¼õ¿®¤·¤¿²»À¼¥Ñ¥±¥Ã¥È¤ò /dev/dsp ¤Ë½ñ¤¹þ¤à while() ¥ë¡¼¥×¤ò¼Â¹Ô¤·¤Þ¤¹¡£
while() ¥ë¡¼¥×¤Ç¤Ï¡¢¤Þ¤º UDP ¥½¥±¥Ã¥È¤«¤é FRAGSIZE ʬ¤Î¥Ç¡¼¥¿¤ò recvfrom() ¤ÇÆɤ߹þ¤ß¡¢ Æɤ߹þ¤ó¤À¥Ñ¥±¥Ã¥È¤ò write() ¤Ç /dev/dsp ¤Ë½ñ¤¹þ¤ó¤Ç¤¤¤Þ¤¹¡£¤³¤Î½ñ¤¹þ¤ß¤Ë¤è¤ê¡¢ ²»À¼¤¬¥¹¥Ô¡¼¥«¤«¤é½ÐÎϤµ¤ì¤Þ¤¹¡£
#include <sys/types.h> #include <sys/socket.h> int recvfrom( int s, void* buf, size_t len, int flags, struct sockaddr* from, socklen_t* fromlen ); s : socket() ¥·¥¹¥Æ¥à¥³¡¼¥ë¤ÇºîÀ®¤·¤¿¥½¥±¥Ã¥È buf : ¼õ¿®¥Ç¡¼¥¿³ÊǼÍѥХåե¡¤Ø¤Î¥Ý¥¤¥ó¥¿ len : ¼õ¿®¤¹¤ë¥Ç¡¼¥¿Ä¹ flags : ¥½¥±¥Ã¥ÈÄÌ¿®Íѥե饰¡£¾ÜºÙ¤Ï¥Þ¥Ë¥å¥¢¥ë»²¾È¡£¤³¤³¤Ç¤Ï»ÈÍѤ·¤Ê¤¤¤¿¤á 0 ¤òÀßÄê from : Á÷¿®¸µ¾ðÊó¤Ø¤Î¥Ý¥¤¥ó¥¿(Á÷¿®¸µ¾ðÊ󤬳ÊǼ¤µ¤ì¤ë) fromlen : from ¤Î¼Â¥Ç¡¼¥¿¤ÎÂ礤µ
¥¨¥é¡¼¤¬È¯À¸¤·¤¿¾ì¹ç¤Ï -1 ¤¬ÊÖ¤µ¤ì¡¢¤½¤ì°Ê³°¤Ï¼õ¿®¤·¤¿¥Ç¡¼¥¿Ä¹¤¬ÊÖ¤µ¤ì¤Þ¤¹¡£
- 104 ¡Á 173 ¹ÔÌÜ ( setup_dsp() )
/dev/dsp ¤ÎÀßÄê¤ò¹Ô¤Ã¤Æ¤¤¤Þ¤¹¡£Á°²ó ¤Î client.c ¤Î setup_dsp() ¤È°ì¥ö½ê¤ò½ü¤¤¤ÆÁ´¤¯Æ±¤¸ÆâÍƤǤ¹¡£Í£°ì¡¢119 ¹ÔÌÜ¤Ç /dev/dsp ¤ò O_WRONLY ( ½ñ¤¹þ¤ß¤Î¤ß ) ¥â¡¼¥É¤Ç³«¤¤¤Æ¤¤¤ëÅÀ¤¬°Û¤Ê¤ê¤Þ¤¹¡£
- 176 ¡Á 204 ¹ÔÌÜ ( setup_udp() )
¥¯¥é¥¤¥¢¥ó¥È¤È¤ÎÄÌ¿®¤Ë»ÈÍѤ¹¤ë¡¢UDP ¥½¥±¥Ã¥È¤ÎÀßÄê¤ò¹Ô¤Ã¤Æ¤¤¤Þ¤¹¡£
184 ¡Á 187 ¹ÔÌܤǤϡ¢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 ¤¬ÊÖ¤µ¤ì¤Þ¤¹¡£
191 ¡Á 193 ¹ÔÌܤǤϡ¢¼«Ê¬¤Î¾ðÊó¤ò struct sockaddr_in ·¿¹½Â¤ÂΤ˳ÊǼ¤·¤Æ¤¤¤Þ¤¹¡£ ¸å½Ò¤Î bind() ¥·¥¹¥Æ¥à¥³¡¼¥ë¤Ç»ÈÍѤ·¤Þ¤¹¡£
191 ¹ÔÌܤǥ¯¥é¥¤¥¢¥ó¥È¤È¤ÎÄÌ¿®¤ËÍѤ¤¤ë¥×¥í¥È¥³¥ë¤ò ( IP ¤Î¾ì¹ç¤Ï PF_INET ¤â¤·¤¯¤Ï AF_INET )¡¢ 192 ¹ÔÌܤǥѥ±¥Ã¥È¤Î¼õ¿®ÂÔ¤Á¤ò¹Ô¤¦ IP ¥¢¥É¥ì¥¹¤òÀßÄê ( INADDR_ANY ¤ò»ØÄꤹ¤ë¤³¤È¤Ë¤è¤ê¡¢ ¼ÂºÝ¤Î¥¢¥É¥ì¥¹ÃͤÎÀßÄê¤ò OS ¤Ë°ÍÍꤷ¤Æ¤¤¤ë ) ¤·¡¢193 ¹ÔÌܤǥݡ¼¥ÈÈÖ¹æ¤òÀßÄꤷ¤Æ¤¤¤Þ¤¹¡£ ¥Ý¡¼¥ÈÈÖ¹æ¤Ë¤Ä¤¤¤Æ¤Ï¡¢¥Í¥Ã¥È¥ï¡¼¥¯¥Ð¥¤¥È¥ª¡¼¥À¤È¤¤¤¦·Á¼°¤Ç¿ôÃͤò³ÊǼ¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¤Î¤Ç¡¢ unsisgned short ·¿¤Î¿ôÃͤò¥Í¥Ã¥È¥ï¡¼¥¯¥Ð¥¤¥È¥ª¡¼¥À¤ËÊÑ´¹¤¹¤ë htons() ´Ø¿ô¤ò»È¤Ã¤Æ¤¤¤Þ¤¹¡£
195 ¡Á 201 ¹ÔÌÜ¤Ç¤Ï bind() ¥·¥¹¥Æ¥à¥³¡¼¥ë¤ò»È¤Ã¤Æ¡¢ºîÀ®¤·¤¿¥½¥±¥Ã¥È¤Ë¥¢¥É¥ì¥¹¤òÀßÄꤷ¤Æ¤¤¤Þ¤¹¡£ ¤³¤ì¤Ë¤è¤ê¡¢OS ¤¬³ºÅö¥¢¥É¥ì¥¹:¥Ý¡¼¥È¤ËÂФ·¤ÆÁ÷¿®¤µ¤ì¤¿¥Ñ¥±¥Ã¥È¤Î¼õ¿®¤ò³«»Ï¤·¤Æ¤¯¤ì¤Þ¤¹¡£
#include <sys/types.h> #include <sys/socket.h> int bind( int sockfd, struct sockaddr* my_addr, socklen_t addrlen ); sockfd : ºîÀ®¤·¤¿¥½¥±¥Ã¥È my_addr : ¥½¥±¥Ã¥È¤ËÀßÄꤹ¤ë¥¢¥É¥ì¥¹¾ðÊó addrlen : my_addr ¤Î¥Ç¡¼¥¿Ä¹
À®¸ù¤¹¤ë¤È 0 ¤òÊÖ¤·¡¢¥¨¥é¡¼¤¬È¯À¸¤·¤¿¾ì¹ç¤Ë¤Ï -1 ¤òÊÖ¤·¤Þ¤¹¡£
server.c ¤ÎÀâÌÀ¤Ï°Ê¾å¤Ç¤¹¡£¥µ¡¼¥Ð¼Â¹Ô¸å¤Ë¥¯¥é¥¤¥¢¥ó¥È¤ò¼Â¹Ô¤·¤Æ¡¢¥¯¥é¥¤¥¢¥ó¥È¤Î¥Þ¥¤¥¯¤ËÆþÎϤ·¤¿²»À¼¤¬¥µ¡¼¥Ð¤Î¥¹¥Ô¡¼¥«¤«¤éʹ¤³¤¨¤ÆÍè¤ë´¶Æ°¤ò¤¼¤ÒÌ£¤ï¤Ã¤Æ¤ß¤Æ²¼¤µ¤¤¡ª