|
h>MyPLinuxÁªÃË
|
¶¨Ò庯Êý | unsigned short int ntohs(unsigned short int netshort);MyPLinuxÁªÃË
|
º¯Êý˵Ã÷ | ntohs()ÓÃÀ´½«²ÎÊýÖ¸¶¨µÄ16λnetshortת»»³ÉÖ÷»ú×Ö·û˳Ðò¡£MyPLinuxÁªÃË
|
·µ»ØÖµ | ·µ»Ø¶ÔÓ¦µÄÖ÷»ú˳Ðò¡£MyPLinuxÁªÃË
|
·¶Àý | ²Î¿¼getservent()¡£MyPLinuxÁªÃË
|
| ¡¡ |
MyPLinuxÁªÃË
| MyPLinuxÁªÃË recv£¨¾socket½ÓÊÕÊý¾Ý£© |
Ïà¹Øº¯Êý | recvfrom£¬recvmsg£¬send£¬sendto£¬socketMyPLinuxÁªÃË
|
±íÍ·Îļþ | #include<sys/types.h>MyPLinuxÁªÃË #include<sys/socket.h>MyPLinuxÁªÃË
|
¶¨Ò庯Êý | int recv(int s,void *buf,int len,unsigned int flags);MyPLinuxÁªÃË
|
º¯Êý˵Ã÷ | recv()ÓÃÀ´½ÓÊÕÔ¶¶ËÖ÷»ú¾Ö¸¶¨µÄsocket´«À´µÄÊý¾Ý£¬²¢°ÑÊý¾Ý´æµ½ÓɲÎÊýbuf Ö¸ÏòµÄÄÚ´æ¿Õ¼ä£¬²ÎÊýlenΪ¿É½ÓÊÕÊý¾ÝµÄ×î´ó³¤¶È¡£MyPLinuxÁªÃË
|
²ÎÊý | flagsÒ»°ãÉè0¡£ÆäËûÊýÖµ¶¨ÒåÈçÏÂ:MyPLinuxÁªÃË MSG_OOB ½ÓÊÕÒÔout-of-band ËͳöµÄÊý¾Ý¡£MyPLinuxÁªÃË MSG_PEEK ·µ»ØÀ´µÄÊý¾Ý²¢²»»áÔÚϵͳÄÚɾ³ý£¬Èç¹ûÔÙµ÷ÓÃrecv()»á·µ»ØÏàͬµÄÊý¾ÝÄÚÈÝ¡£MyPLinuxÁªÃË MSG_WAITALLÇ¿ÆÈ½ÓÊÕµ½len´óСµÄÊý¾Ýºó²ÅÄÜ·µ»Ø£¬³ý·ÇÓдíÎó»òÐźŲúÉú¡£MyPLinuxÁªÃË MSG_NOSIGNAL´Ë²Ù×÷²»Ô¸±»SIGPIPEÐźÅÖжϷµ»ØÖµ³É¹¦Ôò·µ»Ø½ÓÊÕµ½µÄ×Ö·ûÊý£¬Ê§°Ü·µ»Ø-1£¬´íÎóÔÒò´æÓÚerrnoÖС£MyPLinuxÁªÃË
|
´íÎó´úÂë | EBADF ²ÎÊýs·ÇºÏ·¨µÄsocket´¦Àí´úÂëMyPLinuxÁªÃË EFAULT ²ÎÊýÖÐÓÐÒ»Ö¸ÕëÖ¸ÏòÎÞ·¨´æÈ¡µÄÄÚ´æ¿Õ¼äMyPLinuxÁªÃË ENOTSOCK ²ÎÊýsΪһÎļþÃèÊö´Ê£¬·Çsocket¡£MyPLinuxÁªÃË EINTR ±»ÐźÅËùÖжÏMyPLinuxÁªÃË EAGAIN ´Ë¶¯×÷»áÁî½ø³Ì×è¶Ï£¬µ«²ÎÊýsµÄsocketΪ²»¿É×è¶ÏMyPLinuxÁªÃË ENOBUFS ϵͳµÄ»º³åÄÚ´æ²»×ã¡£MyPLinuxÁªÃË ENOMEM ºËÐÄÄÚ´æ²»×ãMyPLinuxÁªÃË EINVAL ´«¸øÏµÍ³µ÷ÓõIJÎÊý²»ÕýÈ·¡£MyPLinuxÁªÃË
|
·¶Àý | ²Î¿¼listen()¡£MyPLinuxÁªÃË
|
| ¡¡ |
MyPLinuxÁªÃË
| MyPLinuxÁªÃË recvfrom£¨¾socket½ÓÊÕÊý¾Ý£© |
Ïà¹Øº¯Êý | recv£¬recvmsg£¬send£¬sendto£¬socketMyPLinuxÁªÃË
|
±íÍ·Îļþ | #include<sys/types.h>MyPLinuxÁªÃË #include<sys/socket.h>MyPLinuxÁªÃË
|
¶¨Ò庯Êý | int recvfrom(int s,void *buf,int len,unsigned int flags ,struct sockaddr *from ,int *fromlen);MyPLinuxÁªÃË
|
º¯Êý˵Ã÷ | recv()ÓÃÀ´½ÓÊÕÔ¶³ÌÖ÷»ú¾Ö¸¶¨µÄsocket ´«À´µÄÊý¾Ý£¬²¢°ÑÊý¾Ý´æµ½ÓɲÎÊýbuf Ö¸ÏòµÄÄÚ´æ¿Õ¼ä£¬²ÎÊýlen Ϊ¿É½ÓÊÕÊý¾ÝµÄ×î´ó³¤¶È¡£²ÎÊýflags Ò»°ãÉè0£¬ÆäËûÊýÖµ¶¨ÒåÇë²Î¿¼recv()¡£²ÎÊýfromÓÃÀ´Ö¸¶¨Óû´«Ë͵ÄÍøÂçµØÖ·£¬½á¹¹sockaddr Çë²Î¿¼bind()¡£²ÎÊýfromlenΪsockaddrµÄ½á¹¹³¤¶È¡£MyPLinuxÁªÃË
|
·µ»ØÖµ | ³É¹¦Ôò·µ»Ø½ÓÊÕµ½µÄ×Ö·ûÊý£¬Ê§°ÜÔò·µ»Ø-1£¬´íÎóÔÒò´æÓÚerrnoÖС£MyPLinuxÁªÃË
|
´íÎó´úÂë | EBADF ²ÎÊýs·ÇºÏ·¨µÄsocket´¦Àí´úÂëMyPLinuxÁªÃË EFAULT ²ÎÊýÖÐÓÐÒ»Ö¸ÕëÖ¸ÏòÎÞ·¨´æÈ¡µÄÄÚ´æ¿Õ¼ä¡£MyPLinuxÁªÃË ENOTSOCK ²ÎÊýsΪһÎļþÃèÊö´Ê£¬·Çsocket¡£MyPLinuxÁªÃË EINTR ±»ÐźÅËùÖжϡ£MyPLinuxÁªÃË EAGAIN ´Ë¶¯×÷»áÁî½ø³Ì×è¶Ï£¬µ«²ÎÊýsµÄsocketΪ²»¿É×è¶Ï¡£MyPLinuxÁªÃË ENOBUFS ϵͳµÄ»º³åÄÚ´æ²»×ãMyPLinuxÁªÃË ENOMEM ºËÐÄÄÚ´æ²»×ãMyPLinuxÁªÃË EINVAL ´«¸øÏµÍ³µ÷ÓõIJÎÊý²»ÕýÈ·¡£MyPLinuxÁªÃË
|
·¶Àý | /*ÀûÓÃsocketµÄUDP clientMyPLinuxÁªÃË ´Ë³ÌÐò»áÁ¬ÏßUDP server£¬²¢½«¼üÅÌÊäÈëµÄ×Ö·û´®´«¸øserver¡£MyPLinuxÁªÃË UDP server ·¶ÀýÇë²Î¿¼sendto£¨£©¡£MyPLinuxÁªÃË */MyPLinuxÁªÃË #include<sys/stat.h>MyPLinuxÁªÃË #include<fcntl.h>MyPLinuxÁªÃË #include<unistd.h>MyPLinuxÁªÃË #include<sys/typs.h>MyPLinuxÁªÃË #include<sys/socket.h>MyPLinuxÁªÃË #include<netinet/in.h>MyPLinuxÁªÃË #include<arpa/inet.h>MyPLinuxÁªÃË #define PORT 2345MyPLinuxÁªÃË #define SERVER_IP ¡°127.0.0.1¡±MyPLinuxÁªÃË main()MyPLinuxÁªÃË {MyPLinuxÁªÃË int s,len;MyPLinuxÁªÃË struct sockaddr_in addr;MyPLinuxÁªÃË int addr_len =sizeof(struct sockaddr_in);MyPLinuxÁªÃË char buffer[256];MyPLinuxÁªÃË /* ½¨Á¢socket*/MyPLinuxÁªÃË if((s = socket(AF_INET,SOCK_DGRAM,0))<0){MyPLinuxÁªÃË perror(¡°socket¡±);MyPLinuxÁªÃË exit(1);MyPLinuxÁªÃË }MyPLinuxÁªÃË /* Ìîдsockaddr_in*/MyPLinuxÁªÃË bzero(&addr,sizeof(addr));MyPLinuxÁªÃË addr.sin_family = AF_INET;MyPLinuxÁªÃË addr.sin_port = htons(PORT);MyPLinuxÁªÃË addr.sin_addr.s_addr = inet_addr(SERVER_IP);MyPLinuxÁªÃË while(1){MyPLinuxÁªÃË bzero(buffer,sizeof(buffer));MyPLinuxÁªÃË /* ´Ó±ê×¼ÊäÈëÉ豸ȡµÃ×Ö·û´®*/MyPLinuxÁªÃË len =read(STDIN_FILENO,buffer,sizeof(buffer));MyPLinuxÁªÃË /* ½«×Ö·û´®´«Ë͸øserver¶Ë*/MyPLinuxÁªÃË sendto(s,buffer,len,0,&addr,addr_len);MyPLinuxÁªÃË /* ½ÓÊÕserver¶Ë·µ»ØµÄ×Ö·û´®*/MyPLinuxÁªÃË len = recvfrom(s,buffer,sizeof(buffer),0,&addr,&addr_len);MyPLinuxÁªÃË printf(¡°receive: %s¡±,buffer);MyPLinuxÁªÃË }MyPLinuxÁªÃË }MyPLinuxÁªÃË
|
Ö´ÐÐ | (ÏÈÖ´ÐÐudp server ÔÙÖ´ÐÐudp client)MyPLinuxÁªÃË hello /*´Ó¼üÅÌÊäÈë×Ö·û´®*/MyPLinuxÁªÃË receive: hello /*server¶Ë·µ»ØÀ´µÄ×Ö·û´®*/MyPLinuxÁªÃË
|
| ¡¡ |
MyPLinuxÁªÃË
| MyPLinuxÁªÃË recvmsg£¨¾socket½ÓÊÕÊý¾Ý£© |
Ïà¹Øº¯Êý | recv£¬recvfrom£¬send£¬sendto£¬sendmsg£¬socketMyPLinuxÁªÃË
|
±íÍ·Îļþ | #include<sys/types.h>MyPLinuxÁªÃË #include<sys/socktet.h>MyPLinuxÁªÃË
|
¶¨Ò庯Êý | int recvmsg(int s,struct msghdr *msg,unsigned int flags);MyPLinuxÁªÃË
|
º¯Êý˵Ã÷ | recvmsg()ÓÃÀ´½ÓÊÕÔ¶³ÌÖ÷»ú¾Ö¸¶¨µÄsocket´«À´µÄÊý¾Ý¡£²ÎÊýsΪÒѽ¨Á¢ºÃÁ¬ÏßµÄsocket£¬Èç¹ûÀûÓÃUDPÐÒéÔò²»Ðè¾¹ýÁ¬Ïß²Ù×÷¡£²ÎÊýmsgÖ¸ÏòÓûÁ¬ÏßµÄÊý¾Ý½á¹¹ÄÚÈÝ£¬²ÎÊýflagsÒ»°ãÉè0£¬ÏêϸÃèÊöÇë²Î¿¼send()¡£¹ØÓڽṹmsghdrµÄ¶¨ÒåÇë²Î¿¼sendmsg()¡£MyPLinuxÁªÃË
|
·µ»ØÖµ | ³É¹¦Ôò·µ»Ø½ÓÊÕµ½µÄ×Ö·ûÊý£¬Ê§°ÜÔò·µ»Ø-1£¬´íÎóÔÒò´æÓÚerrnoÖС£MyPLinuxÁªÃË
|
´íÎó´úÂë | EBADF ²ÎÊýs·ÇºÏ·¨µÄsocket´¦Àí´úÂë¡£MyPLinuxÁªÃË EFAULT ²ÎÊýÖÐÓÐÒ»Ö¸ÕëÖ¸ÏòÎÞ·¨´æÈ¡µÄÄÚ´æ¿Õ¼äMyPLinuxÁªÃË ENOTSOCK ²ÎÊýsΪһÎļþÃèÊö´Ê£¬·Çsocket¡£MyPLinuxÁªÃË EINTR ±»ÐźÅËùÖжϡ£MyPLinuxÁªÃË EAGAIN ´Ë²Ù×÷»áÁî½ø³Ì×è¶Ï£¬µ«²ÎÊýsµÄsocketΪ²»¿É×è¶Ï¡£MyPLinuxÁªÃË ENOBUFS ϵͳµÄ»º³åÄÚ´æ²»×ãMyPLinuxÁªÃË ENOMEM ºËÐÄÄÚ´æ²»×ãMyPLinuxÁªÃË EINVAL ´«¸øÏµÍ³µ÷ÓõIJÎÊý²»ÕýÈ·¡£MyPLinuxÁªÃË
|
·¶Àý | ²Î¿¼recvfrom()¡£MyPLinuxÁªÃË
|
| ¡¡ |
MyPLinuxÁªÃË
| MyPLinuxÁªÃË send£¨¾socket´«ËÍÊý¾Ý£© |
Ïà¹Øº¯Êý | sendto£¬sendmsg£¬recv£¬recvfrom£
LinuxÁªÃËÊÕ¼¯ÕûÀí ,תÌùÇë±êÃ÷ÔʼÁ´½Ó,ÈçÓÐÈκÎÒÉÎÊ»¶ÓÀ´±¾Õ¾LinuxÂÛ̳ÌÖÂÛ |
|
|
|
|