ºìÁªLinuxÃÅ»§
Linux°ïÖú

±àдLinuxϵÄUDP Client/Server³ÌÐò

·¢²¼Ê±¼ä:2006-09-18 20:55:37À´Ô´:ºìÁª×÷Õß:Service
¡¡¡¡Ò»¡¢ÒýÑÔ

¡¡¡¡UDPÊÇTCP/IPЭÒéÖеĴ«Êä²ãЭÒéµÄÒ»ÖÖ£¬±¾ÎĽéÉÜÁËÔÚLinuxϱàд»ùÓÚUDPЭÒéµÄClient/ServerÄ£Ð͵ijÌÐòµÄ·½·¨£¬²¢¸ø³öÁËÒ»¸öecho Client/ServerÀý×Ó³ÌÐò¡£

¡¡¡¡¶þ¡¢UDPЭÒé¼ò½é

¡¡¡¡UDPÊÇÒ»ÖÖ¼òµ¥µÄ´«Êä²ãЭÒ飬ÔÚRFC768ÖÐÓÐÏêϸÃèÊö¡£UDPЭÒéÊÇÒ»ÖÖ·ÇÁ¬½ÓµÄ¡¢²»¿É¿¿µÄÊý¾Ý±¨ÎÄЭÒ飬ÍêÈ«²»Í¬ÓÚÌṩÃæÏòÁ¬½ÓµÄ¡¢¿É¿¿µÄ×Ö½ÚÁ÷µÄTCPЭÒé¡£ËäÈ»UDPÓкܶ಻×㣬µ«ÊÇ»¹ÊÇÓкܶàÍøÂç³ÌÐòʹÓÃËü£¬ÀýÈçDNS£¨ÓòÃû½âÎö·þÎñ£©¡¢NFS£¨ÍøÂçÎļþϵͳ£©¡¢SNMP£¨¼òµ¥ÍøÂç¹ÜÀíЭÒ飩µÈ¡£

¡¡¡¡Í¨³££¬UDP Client³ÌÐò²»ºÍServer³ÌÐò½¨Á¢Á¬½Ó£¬¶øÊÇÖ±½ÓʹÓÃsendto()À´·¢ËÍÊý¾Ý¡£Í¬Ñù£¬UDP Server³ÌÐò²»ÐèÒªÔÊÐíClient³ÌÐòµÄÁ¬½Ó£¬¶øÊÇÖ±½ÓʹÓÃrecvfrom()À´µÈ´ýÖ±µ½½ÓÊÕµ½Client³ÌÐò·¢ËÍÀ´µÄÊý¾Ý¡£

¡¡¡¡ÕâÀÎÒÃÇʹÓÃÒ»¸ö¼òµ¥µÄecho Client/Server³ÌÐòÀ´½éÉÜÔÚLinuxϱàдUDP³ÌÐòµÄ·½·¨¡£Client³ÌÐò´Óstdin¶ÁÈ¡Êý¾Ý²¢Í¨¹ýÍøÂç·¢Ë͵½Server³ÌÐò£¬Server³ÌÐòÔÚÊÕµ½Êý¾ÝºóÖ±½ÓÔÙ·¢ËÍ»ØClient³ÌÐò£¬Client³ÌÐòÊÕµ½Server·¢»ØµÄÊý¾ÝºóÔÙ´ÓstdoutÊä³ö¡£

¡¡¡¡Èý¡¢UDP Server³ÌÐò

¡¡¡¡1¡¢±àдUDP Server³ÌÐòµÄ²½Öè
¡¡¡¡(1)ʹÓÃsocket()À´½¨Á¢Ò»¸öUDP socket£¬µÚ¶þ¸ö²ÎÊýΪSOCK_DGRAM¡£
¡¡¡¡(2)³õʼ»¯sockaddr_in½á¹¹µÄ±äÁ¿£¬²¢¸³Öµ¡£sockaddr_in½á¹¹¶¨Ò壺
[code]struct sockaddr_in {
uint8_t sin_len;
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};[/code]
¡¡¡¡ÕâÀïʹÓá°08¡±×÷Ϊ·þÎñ³ÌÐòµÄ¶Ë¿Ú£¬Ê¹Óá°INADDR_ANY¡±×÷Ϊ°ó¶¨µÄIPµØÖ·¼´ÈκÎÖ÷»úÉϵĵØÖ·¡£
¡¡¡¡(3)ʹÓÃbind()°ÑÉÏÃæµÄsocketºÍ¶¨ÒåµÄIPµØÖ·ºÍ¶Ë¿Ú°ó¶¨¡£ÕâÀï¼ì²ébind()ÊÇ·ñÖ´Ðгɹ¦£¬Èç¹ûÓдíÎó¾ÍÍ˳ö¡£ÕâÑù¿ÉÒÔ·ÀÖ¹·þÎñ³ÌÐòÖظ´ÔËÐеÄÎÊÌâ¡£
¡¡¡¡(4)½øÈëÎÞÏÞÑ­»·³ÌÐò£¬Ê¹ÓÃrecvfrom()½øÈëµÈ´ý״̬£¬Ö±µ½½ÓÊÕµ½¿Í»§³ÌÐò·¢Ë͵ÄÊý¾Ý£¬¾Í´¦ÀíÊÕµ½µÄÊý¾Ý£¬²¢Ïò¿Í»§³ÌÐò·¢ËÍ·´À¡¡£ÕâÀïÊÇÖ±½Ó°ÑÊÕµ½µÄÊý¾Ý·¢»Ø¸ø¿Í»§³ÌÐò¡£

¡¡¡¡2¡¢udpserv.c³ÌÐòÄÚÈÝ£º
[code]#include
#include
#include
#include
#include
#include
#define MAXLINE 80
#define SERV_PORT 8888
void do_echo(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen)
{
int n;
socklen_t len;
char mesg[MAXLINE];
for(;;)
{
len = clilen;
/* waiting for receive data */
n = recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);
/* sent data back to client */
sendto(sockfd, mesg, n, 0, pcliaddr, len);
}
}
int main(void)
{
int sockfd;
struct sockaddr_in servaddr, cliaddr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0); /* create a socket */
/* init servaddr */
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
/* bind address and port to socket */
if(bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
{
perror("bind error");
exit(1);
}
do_echo(sockfd, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
return 0;
}[/code]

¡¡¡¡ËÄ¡¢UDP Client³ÌÐò

¡¡¡¡1¡¢±àдUDP Client³ÌÐòµÄ²½Öè
¡¡¡¡(1)³õʼ»¯sockaddr_in½á¹¹µÄ±äÁ¿£¬²¢¸³Öµ¡£ÕâÀïʹÓá°8888¡±×÷ΪÁ¬½ÓµÄ·þÎñ³ÌÐòµÄ¶Ë¿Ú£¬´ÓÃüÁîÐвÎÊý¶ÁÈ¡IPµØÖ·£¬²¢ÇÒÅжÏIPµØÖ·ÊÇ·ñ·ûºÏÒªÇó¡£
¡¡¡¡(2)ʹÓÃsocket()À´½¨Á¢Ò»¸öUDP socket£¬µÚ¶þ¸ö²ÎÊýΪSOCK_DGRAM¡£
¡¡¡¡(3)ʹÓÃconnect()À´½¨Á¢Óë·þÎñ³ÌÐòµÄÁ¬½Ó¡£ÓëTCPЭÒ鲻ͬ£¬UDPµÄconnect()²¢Ã»ÓÐÓë·þÎñ³ÌÐòÈý´ÎÎÕÊÖ¡£ÉÏÃæÎÒÃÇ˵ÁËUDPÊÇ·ÇÁ¬½ÓµÄ£¬Êµ¼ÊÉÏÒ²¿ÉÒÔÊÇÁ¬½ÓµÄ¡£Ê¹ÓÃÁ¬½ÓµÄUDP£¬kernel¿ÉÒÔÖ±½Ó·µ»Ø´íÎóÐÅÏ¢¸øÓû§³ÌÐò£¬´Ó¶ø±ÜÃâÓÉÓÚûÓнÓÊÕµ½Êý¾Ý¶øµ¼Öµ÷ÓÃrecvfrom()Ò»Ö±µÈ´ýÏÂÈ¥£¬¿´ÉÏÈ¥ºÃÏñ¿Í»§³ÌÐòûÓз´Ó¦Ò»Ñù¡£
¡¡¡¡(4)Ïò·þÎñ³ÌÐò·¢ËÍÊý¾Ý£¬ÒòΪʹÓÃÁ¬½ÓµÄUDP£¬ËùÒÔʹÓÃwrite()À´Ìæ´úsendto()¡£ÕâÀïµÄÊý¾ÝÖ±½Ó´Ó±ê×¼ÊäÈë¶ÁÈ¡Óû§ÊäÈë¡£
¡¡¡¡(5)½ÓÊÕ·þÎñ³ÌÐò·¢»ØµÄÊý¾Ý£¬Í¬ÑùʹÓÃread()À´Ìæ´úrecvfrom()¡£
¡¡¡¡(6)´¦Àí½ÓÊÕµ½µÄÊý¾Ý£¬ÕâÀïÊÇÖ±½ÓÊä³öµ½±ê×¼Êä³öÉÏ¡£

¡¡¡¡2¡¢udpclient.c³ÌÐòÄÚÈÝ£º
[code]#include
#include
#include
#include
#include
#include
#include
#include
#define MAXLINE 80
#define SERV_PORT 8888
void do_cli(FILE *fp, int sockfd, struct sockaddr *pservaddr, socklen_t servlen)
{
int n;
char sendline[MAXLINE], recvline[MAXLINE + 1];
/* connect to server */
if(connect(sockfd, (struct sockaddr *)pservaddr, servlen) == -1)
{
perror("connect error");
exit(1);
}
while(fgets(sendline, MAXLINE, fp) != NULL)
{
/* read a line and send to server */
write(sockfd, sendline, strlen(sendline));
/* receive data from server */
n = read(sockfd, recvline, MAXLINE);
if(n == -1)
{
perror("read error");
exit(1);
}
recvline[n] = 0; /* terminate string */
fputs(recvline, stdout);
}
}
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in srvaddr;
/* check args */
if(argc != 2)
{
printf("usage: udpclient \n");
exit(1);
}
/* init servaddr */
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
{
printf("[%s] is not a valid IPaddress\n", argv[1]);
exit(1);
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
do_cli(stdin, sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
return 0;
}[/code]

¡¡¡¡Îå¡¢ÔËÐÐÀý×Ó³ÌÐò

¡¡¡¡1¡¢±àÒëÀý×Ó³ÌÐò
¡¡¡¡Ê¹ÓÃÈçÏÂÃüÁîÀ´±àÒëÀý×Ó³ÌÐò£º
[code]gcc -Wall -o udpserv udpserv.c
gcc -Wall -o udpclient udpclient.c[/code]
¡¡¡¡±àÒëÍê³ÉÉú³ÉÁËudpservºÍudpclientÁ½¸ö¿ÉÖ´ÐгÌÐò¡£

¡¡¡¡2¡¢ÔËÐÐUDP Server³ÌÐò
¡¡¡¡Ö´ÐÐ./udpserv &ÃüÁîÀ´Æô¶¯·þÎñ³ÌÐò¡£ÎÒÃÇ¿ÉÒÔʹÓÃnetstat -lnÃüÁîÀ´¹Û²ì·þÎñ³ÌÐò°ó¶¨µÄIPµØÖ·ºÍ¶Ë¿Ú£¬²¿·ÖÊä³öÐÅÏ¢ÈçÏ£º
[code]Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:32768 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:6000 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
udp 0 0 0.0.0.0:32768 0.0.0.0:*
udp 0 0 0.0.0.0:8888 0.0.0.0:*
udp 0 0 0.0.0.0:111 0.0.0.0:*
udp 0 0 0.0.0.0:882 0.0.0.0:*[/code]
¡¡¡¡¿ÉÒÔ¿´µ½udp´¦ÓС°0.0.0.0:8888¡±µÄÄÚÈÝ£¬ËµÃ÷·þÎñ³ÌÐòÒѾ­Õý³£ÔËÐУ¬¿ÉÒÔ½ÓÊÕÖ÷»úÉÏÈκÎIPµØÖ·ÇҶ˿ÚΪ8888µÄÊý¾Ý¡£
¡¡¡¡Èç¹ûÕâʱÔÙÖ´ÐÐ./udpserv &ÃüÁ¾Í»á¿´µ½ÈçÏÂÐÅÏ¢£º
[code]bind error: Address already in use[/code]
¡¡¡¡ËµÃ÷ÒѾ­ÓÐÒ»¸ö·þÎñ³ÌÐòÔÚÔËÐÐÁË¡£

¡¡¡¡3¡¢ÔËÐÐUDP Client³ÌÐò
¡¡¡¡Ö´ÐÐ./udpclient 127.0.0.1ÃüÁîÀ´Æô¶¯¿Í»§³ÌÐò£¬Ê¹ÓÃ127.0.0.1À´Á¬½Ó·þÎñ³ÌÐò£¬Ö´ÐÐЧ¹ûÈçÏ£º
[code]Hello, World!
Hello, World!
this is a test
this is a test
^d[/code]
¡¡¡¡ÊäÈëµÄÊý¾Ý¶¼ÕýÈ·´Ó·þÎñ³ÌÐò·µ»ØÁË£¬°´ctrl+d¿ÉÒÔ½áÊøÊäÈ룬Í˳ö³ÌÐò¡£
¡¡¡¡Èç¹û·þÎñ³ÌÐòûÓÐÆô¶¯£¬¶øÖ´Ðпͻ§³ÌÐò£¬¾Í»á¿´µ½ÈçÏÂÐÅÏ¢£º
[code]$ ./udpclient 127.0.0.1
test
read error: Connection refused[/code]
¡¡¡¡ËµÃ÷Ö¸¶¨µÄIPµØÖ·ºÍ¶Ë¿ÚûÓзþÎñ³ÌÐò°ó¶¨£¬¿Í»§³ÌÐò¾ÍÍ˳öÁË¡£Õâ¾ÍÊÇʹÓÃconnect()µÄºÃ´¦£¬×¢Ò⣬ÕâÀï´íÎóÐÅÏ¢ÊÇÔÚÏò·þÎñ³ÌÐò·¢ËÍÊý¾ÝºóÊÕµ½µÄ£¬¶ø²»ÊÇÔÚµ÷ÓÃconnect()ʱ¡£Èç¹ûÄãʹÓÃtcpdump³ÌÐòÀ´×¥°ü£¬»á·¢ÏÖÊÕµ½µÄÊÇICMPµÄ´íÎóÐÅÏ¢¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 4 ÌõÆÀÂÛ

  1. 59.25.9.* ÓÚ 2007-07-19 06:02:23·¢±í:

    http://www.ringtones-dir.net/get/ ringtones site free. Download ringtones FREE, Best free samsung ringtones, Cingular ringtones and more. From website .

  2. 59.25.9.* ÓÚ 2007-07-19 06:02:20·¢±í:

    http://www.ringtones-dir.net/get/ ringtones site free. ringtones download: Download ringtones FREE, Best free samsung ringtones, Cingular ringtones and more. samsung ringtones From website .

  3. 59.25.9.* ÓÚ 2007-07-19 06:02:19·¢±í:

    http://www.ringtones-dir.net/download/ download ringtones. nokia ringtones: Download ringtones FREE, Best free samsung ringtones, Cingular ringtones and more. http://www.ringtones-dir.net/free/ [link=http://www.ringtones-dir.net]ring tones[/link] From site .

  4. ÓÚ 2006-11-22 11:24:02·¢±í:

    :0)1 :handshake :ha3nd :0Q :time:1 :49ab:1 :77ab:1 :0L :0(1 :0D1 :0)1 :mad:1 :ha3nd :victory: :0Q