©®linuxÉçÇø©® °®ÐÄÔ®Öú ©® LinuxÈÏÖ¤©®ÏµÁÐ½Ì³Ì ©® Òµ½ç¶¯Ì¬©® Õ¾ÎñÐÂÎÅ ©® ¹«Ë¾ÕÐÆ¸©® ½¨ÒéÁôÑÔ ©® ÍøÖ·´óÈ« ©®LPIרÌâ©® CISCOרÌâ
ÉèΪÊ×Ò³
¼ÓÈëÊÕ²Ø
¹ÜÀíÍŶÓ
JSP  
JAVA  
PERL  
 ÄúµÄλÖãºÊ×Ò³ > article > Linux¿ª·¢Çø > Èí¼þ¿ª·¢ >
À¸Ä¿µ¼À¸
×ÊÁÏËÑË÷
ÈÈÃÅÎÄÕÂ
¡¤Linux Ï C ÓïÑÔ±à³Ì
¡¤LinuxϵÄͨÓÃÏ̳߳ش´½¨
¡¤C++×Ö·û´®×ª»»Æª
¡¤linux C ½ø³Ì²Ù×÷ƪ
¡¤linuxÉϵÄC/C++±àÒëÆ÷gcc/egcs
¡¤linux C ÎļþȨÏÞ¿ØÖÆÆª
¡¤GCCʹÓÃÊÖ²á
¡¤linux C ½Ó¿Ú´¦ÀíÆª
¡¤ÔÚRedhat LinuxÉϰ²×° GCC ±àÒë
¡¤GCCʹÓÃÖ¸ÄÏ
¡¤CÓïÑÔÔËËã·û
¡¤LinuxÏÂC¿ª·¢»·¾³µÄ¹¹³ÉºÍ°²×°
¡¤fopen()º¯ÊýµÄ²ÎÊý˵Ã÷
¡¤GCCʹÓÃÊÖ²áÓë³£ÓÃÃüÁî
¡¤Scheme ÓïÑÔ½éÉÜ
×îÐÂÎÄÕÂ
¡¤ÔÚUbuntu Linux 8.04ÉϹ¹½¨GCC
¡¤Linux²Ù×÷ϵͳÏÂSocket±à³ÌµØÖ·
¡¤½«VC³ÌÐòÒÆÖ²µ½LinuxϵͳµÄ¼¸µã
¡¤LinuxÏÂmalloc/freeÓënew/dele
¡¤LinuxÏÂÓÃGTKºÍsocketʵÏÖ¼òµ¥
¡¤Linux²Ù×÷ϵͳÏÂÈÃTomcatÆô¶¯ÔÚ
¡¤Linux²Ù×÷ϵͳÖÐÈçºÎ±àÒëC³ÌÐò
¡¤¼¸ÖÖ³£±»ÈËÃǺöÂÔµÄLinuxϵͳÏÂ
¡¤Eclipse±à³Ì¹¤¾ß ÔÚUbuntuϵÄ
¡¤Linux²Ù×÷ϵͳϵÄÍøÂçµØÖ·×ª»»
¡¤ÀÏÊÖ¾­Ñé̸£ºLinuxÇý¶¯³ÌÐò¿ª·¢
¡¤Linux²Ù×÷ϵͳ¶àÏß³Ìͬ²½Mutex
¡¤Linux²Ù×÷ϵͳÏÂC³ÌÐòÓïÑÔ¼òÒ×
¡¤Linuxϵͳƽ̨ϹØÓÚGCC±àÒë¼°
¡¤½â¾öLinuxϵͳϹܵÀ±»½ÓÊÜ·½¹Ø
Google
 
linux C ½Ó¿Ú´¦ÀíÆª
[ ×÷Õß:LinuxÁªÃËÊÕ¼¯  ¼ÓÈëʱ¼ä:2006-06-24 14:13:30  À´×Ô:LinuxÁªÃËÊÕ¼¯ ]
h>MyPLinuxÁªÃË
¶¨Ò庯Êý
unsigned short int ntohs(unsigned short int netshort);MyPLinuxÁªÃË
º¯Êý˵Ã÷
ntohs()ÓÃÀ´½«²ÎÊýÖ¸¶¨µÄ16λnetshortת»»³ÉÖ÷»ú×Ö·û˳Ðò¡£MyPLinuxÁªÃË
·µ»ØÖµ
·µ»Ø¶ÔÓ¦µÄÖ÷»ú˳Ðò¡£MyPLinuxÁªÃË
·¶Àý
²Î¿¼getservent()¡£MyPLinuxÁªÃË
¡¡
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ÁªÃË
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ÁªÃË
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ÁªÃË
MyPLinuxÁªÃË
send£¨¾­socket´«ËÍÊý¾Ý£©
Ïà¹Øº¯Êý
sendto£¬sendmsg£¬recv£¬recvfrom£ LinuxÁªÃËÊÕ¼¯ÕûÀí ,תÌùÇë±êÃ÷ԭʼÁ´½Ó,ÈçÓÐÈκÎÒÉÎÊ»¶Ó­À´±¾Õ¾LinuxÂÛ̳ÌÖÂÛ
·ÖÒ³£º1 2 3 [4] 5 6
¡¾ÆÀÂÛ¡¿¡¾¼ÓÈëÊղؼС¿¡¾´ó ÖРС¡¿¡¾´òÓ¡¡¿¡¾¹Ø±Õ¡¿
¡ù Ïà¹ØÁ´½Ó
ÎÞÏà¹ØÐÅÏ¢