|
£MyPLinuxÁªÃË
|
| ¡¡ |
MyPLinuxÁªÃË
| MyPLinuxÁªÃË inet_aton£¨½«ÍøÂçµØÖ·×ª³ÉÍøÂç¶þ½øÖƵÄÊý×Ö£© |
Ïà¹Øº¯Êý | inet_addr£¬inet_ntoaMyPLinuxÁªÃË
|
±íÍ·Îļþ | #include<sys/scoket.h>MyPLinuxÁªÃË #include<netinet/in.h>MyPLinuxÁªÃË #include<arpa/inet.h>MyPLinuxÁªÃË
|
¶¨Ò庯Êý | int inet_aton(const char * cp,struct in_addr *inp);MyPLinuxÁªÃË
|
º¯Êý˵Ã÷ | inet_aton()ÓÃÀ´½«²ÎÊýcpËùÖ¸µÄÍøÂçµØÖ·×Ö·û´®×ª»»³ÉÍøÂçʹÓõĶþ½øÖƵÄÊý×Ö£¬È»ºó´æÓÚ²ÎÊýinpËùÖ¸µÄin_addr½á¹¹ÖС£MyPLinuxÁªÃË ½á¹¹in_addr¶¨ÒåÈçÏÂMyPLinuxÁªÃË struct in_addrMyPLinuxÁªÃË {MyPLinuxÁªÃË unsigned long int s_addr;MyPLinuxÁªÃË };MyPLinuxÁªÃË
|
·µ»ØÖµ | ³É¹¦Ôò·µ»Ø·Ç0Öµ£¬Ê§°ÜÔò·µ»Ø0¡£MyPLinuxÁªÃË
|
| ¡¡ |
MyPLinuxÁªÃË
| MyPLinuxÁªÃË inet_ntoa£¨½«ÍøÂç¶þ½øÖƵÄÊý×Öת»»³ÉÍøÂçµØÖ·£© |
Ïà¹Øº¯Êý | inet_addr£¬inet_atonMyPLinuxÁªÃË
|
±íÍ·Îļþ | #include<sys/socket.h>MyPLinuxÁªÃË #include<netinet/in.h>MyPLinuxÁªÃË #include<arpa/inet.h>MyPLinuxÁªÃË
|
¶¨Ò庯Êý | char * inet_ntoa(struct in_addr in);MyPLinuxÁªÃË
|
º¯Êý˵Ã÷ | inet_ntoa()ÓÃÀ´½«²ÎÊýinËùÖ¸µÄÍøÂç¶þ½øÖƵÄÊý×Öת»»³ÉÍøÂçµØÖ·£¬È»ºó½«Ö¸Ïò´ËÍøÂçµØÖ·×Ö·û´®µÄÖ¸Õë·µ»Ø¡£MyPLinuxÁªÃË
|
·µ»ØÖµ | ³É¹¦Ôò·µ»Ø×Ö·û´®Ö¸Õ룬ʧ°ÜÔò·µ»ØNULL¡£MyPLinuxÁªÃË
|
| ¡¡ |
MyPLinuxÁªÃË
| MyPLinuxÁªÃË listen£¨µÈ´ýÁ¬½Ó£© |
Ïà¹Øº¯Êý | socket£¬bind£¬accept£¬connectMyPLinuxÁªÃË
|
±íÍ·Îļþ | #include<sys/socket.h>MyPLinuxÁªÃË
|
¶¨Ò庯Êý | int listen(int s,int backlog);MyPLinuxÁªÃË
|
º¯Êý˵Ã÷ | listen()ÓÃÀ´µÈ´ý²ÎÊýs µÄsocketÁ¬Ïß¡£²ÎÊýbacklogÖ¸¶¨Í¬Ê±ÄÜ´¦ÀíµÄ×î´óÁ¬½ÓÒªÇó£¬Èç¹ûÁ¬½ÓÊýÄ¿´ï´ËÉÏÏÞÔòclient¶Ë½«ÊÕµ½ECONNREFUSEDµÄ´íÎó¡£Listen()²¢Î´¿ªÊ¼½ÓÊÕÁ¬Ïߣ¬Ö»ÊÇÉèÖÃsocketΪlistenģʽ£¬ÕæÕý½ÓÊÕclient¶ËÁ¬ÏßµÄÊÇaccept()¡£Í¨³£listen()»áÔÚsocket()£¬bind()Ö®ºóµ÷Ó㬽Ó×Ųŵ÷ÓÃaccept()¡£MyPLinuxÁªÃË
|
·µ»ØÖµ | ³É¹¦Ôò·µ»Ø0£¬Ê§°Ü·µ»Ø-1£¬´íÎóÔÒò´æÓÚerrnoMyPLinuxÁªÃË
|
¸½¼Ó˵Ã÷ | listen()Ö»ÊÊÓÃSOCK_STREAM»òSOCK_SEQPACKETµÄsocketÀàÐÍ¡£Èç¹ûsocketΪAF_INETÔò²ÎÊýbacklog ×î´óÖµ¿ÉÉèÖÁ128¡£MyPLinuxÁªÃË
|
´íÎó´úÂë | EBADF ²ÎÊýsockfd·ÇºÏ·¨socket´¦Àí´úÂëMyPLinuxÁªÃË EACCESS ȨÏÞ²»×ãMyPLinuxÁªÃË EOPNOTSUPP Ö¸¶¨µÄsocket²¢Î´Ö§Ô®listenģʽ¡£MyPLinuxÁªÃË
|
·¶Àý | #include<sys/types.h>MyPLinuxÁªÃË #include<sys/socket.h>MyPLinuxÁªÃË #include<netinet/in.h>MyPLinuxÁªÃË #include<arpa/inet.h>MyPLinuxÁªÃË #include<unistd.h>MyPLinuxÁªÃË #define PORT 1234MyPLinuxÁªÃË #define MAXSOCKFD 10MyPLinuxÁªÃË main()MyPLinuxÁªÃË {MyPLinuxÁªÃË int sockfd,newsockfd,is_connected[MAXSOCKFD],fd;MyPLinuxÁªÃË struct sockaddr_in addr;MyPLinuxÁªÃË int addr_len = sizeof(struct sockaddr_in);MyPLinuxÁªÃË fd_set readfds;MyPLinuxÁªÃË char buffer[256];MyPLinuxÁªÃË char msg[ ] =¡±Welcome to server!¡±;MyPLinuxÁªÃË if ((sockfd = socket(AF_INET,SOCK_STREAM,0))<0){MyPLinuxÁªÃË perror(¡°socket¡±);MyPLinuxÁªÃË exit(1);MyPLinuxÁªÃË }MyPLinuxÁªÃË bzero(&addr,sizeof(addr));MyPLinuxÁªÃË addr.sin_family =AF_INET;MyPLinuxÁªÃË addr.sin_port = htons(PORT);MyPLinuxÁªÃË addr.sin_addr.s_addr = htonl(INADDR_ANY);MyPLinuxÁªÃË if(bind(sockfd,&addr,sizeof(addr))<0){MyPLinuxÁªÃË perror(¡°connect¡±);MyPLinuxÁªÃË exit(1);MyPLinuxÁªÃË }MyPLinuxÁªÃË if(listen(sockfd,3)<0){MyPLinuxÁªÃË perror(¡°listen¡±);MyPLinuxÁªÃË exit(1);MyPLinuxÁªÃË }MyPLinuxÁªÃË for(fd=0;fd<MAXSOCKFD;fd++)MyPLinuxÁªÃË is_connected[fd]=0;MyPLinuxÁªÃË while(1){MyPLinuxÁªÃË FD_ZERO(&readfds);MyPLinuxÁªÃË FD_SET(sockfd,&readfds);MyPLinuxÁªÃË for(fd=0;fd<MAXSOCKFD;fd++)MyPLinuxÁªÃË if(is_connected[fd]) FD_SET(fd,&readfds);MyPLinuxÁªÃË if(!select(MAXSOCKFD,&readfds,NULL,NULL,NULL))continue;MyPLinuxÁªÃË for(fd=0;fd<MAXSOCKFD;fd++)MyPLinuxÁªÃË if(FD_ISSET(fd,&readfds)){MyPLinuxÁªÃË if(sockfd = =fd){MyPLinuxÁªÃË if((newsockfd = accept (sockfd,&addr,&addr_len))<0)MyPLinuxÁªÃË perror(¡°accept¡±);MyPLinuxÁªÃË write(newsockfd,msg,sizeof(msg));MyPLinuxÁªÃË is_connected[newsockfd] =1;MyPLinuxÁªÃË printf(¡°cnnect from %sn¡±,inet_ntoa(addr.sin_addr));MyPLinuxÁªÃË }else{MyPLinuxÁªÃË bzero(buffer,sizeof(buffer));MyPLinuxÁªÃË if(read(fd,buffer,sizeof(buffer))<=0){MyPLinuxÁªÃË printf(¡°connect closed.n¡±);MyPLinuxÁªÃË is_connected[fd]=0;MyPLinuxÁªÃË close(fd);MyPLinuxÁªÃË }elseMyPLinuxÁªÃË printf(¡°%s¡±,buffer);MyPLinuxÁªÃË }MyPLinuxÁªÃË }MyPLinuxÁªÃË }MyPLinuxÁªÃË }MyPLinuxÁªÃË
|
Ö´ÐÐ | $ ./listenMyPLinuxÁªÃË connect from 127.0.0.1MyPLinuxÁªÃË hi I am clientMyPLinuxÁªÃË connected closed.MyPLinuxÁªÃË
|
| ¡¡ |
MyPLinuxÁªÃË
| MyPLinuxÁªÃË ntohl£¨½«32Î»ÍøÂç×Ö·û˳Ðòת»»³ÉÖ÷»ú×Ö·û˳Ðò£© |
Ïà¹Øº¯Êý | htonl£¬htons£¬ntohsMyPLinuxÁªÃË
|
±íÍ·Îļþ | #include<netinet/in.h>MyPLinuxÁªÃË
|
¶¨Ò庯Êý | unsigned long int ntohl(unsigned long int netlong);MyPLinuxÁªÃË
|
º¯Êý˵Ã÷ | ntohl()ÓÃÀ´½«²ÎÊýÖ¸¶¨µÄ32λnetlongת»»³ÉÖ÷»ú×Ö·û˳Ðò¡£MyPLinuxÁªÃË
|
·µ»ØÖµ | ·µ»Ø¶ÔÓ¦µÄÖ÷»ú×Ö·û˳Ðò¡£MyPLinuxÁªÃË
|
·¶Àý | ²Î¿¼getservent()¡£MyPLinuxÁªÃË
|
| ¡¡ |
MyPLinuxÁªÃË
| MyPLinuxÁªÃË ntohs£¨½«16Î»ÍøÂç×Ö·û˳Ðòת»»³ÉÖ÷»ú×Ö·û˳Ðò£© |
Ïà¹Øº¯Êý | htonl£¬htons£¬ntohlMyPLinuxÁªÃË
|
±íÍ·Îļþ | #include<netinet/in.
LinuxÁªÃËÊÕ¼¯ÕûÀí ,תÌùÇë±êÃ÷ÔʼÁ´½Ó,ÈçÓÐÈκÎÒÉÎÊ»¶ÓÀ´±¾Õ¾LinuxÂÛ̳ÌÖÂÛ |
|
|
|
|