LinuxÏÂselectµÄʹÓÃС¼Ç
½ñÌìʹÓÃselect±à³Ì£¬ÎÊÌâºÜ¶à£¬ÒÔÇ°ÕæµÄûÓÐ×ÐϸÑо¿¹ý¡£
³öÏÖÁËÒÔϼ¸¸öÎÊÌâ
1 recvfromÔÚ½ÓÊÕʱ£¬Ìṩ¸øϵͳµ÷ÓõÄ×îºóÒ»¸ö²ÎÊý£¬¿Í»§¶ËµÄµØÖ·½á¹¹£¬Æ䳤¶È±ØÐë½øÐгõʼ»¯£¬Èç¹ûΪ0£¬Ôò²»ÄܽÓÊÕÊý¾Ý¡£
2 selectµÄµÚÒ»¸ö²ÎÊýΪËùÓмàÊÓµÄÎļþÃèÊö·ûµÄ×î´óÖµ+1£¬¶ø²»ÊǼàÊÓµÄÎļþÃèÊö·û¸öÊý+1¡£
3 ²»Òª¼àÊÓÄÇЩ¾³£¿ÉÓõÄÎļþÃèÊö·û£¬Èç¹ûÕâÑù×ö£¬select»áÒ»Ö±·µ»Ø¡£
4 ÔÚselect·µ»Øºó£¬Èç¹û¼àÊÓµÄÎļþÃèÊö·ûûÓÐ״̬¸Ä±ä£¬ÔòÔÚfd_setÖдËÎļþÃèÊö·û»á±»Çå0£¬Òò´ËÔÚselectµÄÑ»·ÖУ¬Ã¿´Î½øÈ붼ҪÖØÐÂÉèÖÃÎÒÃÇËù¹Ø×¢µÄÎļþÃèÊö·û¡£
5 Èç¹ûselectʹÓÃÁ˳¬Ê±²Ù×÷£¬Ã¿´Î·µ»Øselect¶¼»áÐ޸ļÆʱÆ÷£¬½«¼ÆʱÆ÷ÉèΪÓàϵÄʱ¼ä£¬Òò´ËÈç¹ûʹÓÃÁ˼ÆʱÆ÷£¬Ã¿´Î½øÈëÑ»·¶¼ÒªÖØÖüÆʱÆ÷¡£pselectûÓÐÕâ¸ö벡¡£
ÏÂÃæÊÇlinuxϵĻùÓÚselectµÄÒ»¸öUDP·´Éä·þÎñÆ÷µÄʾÀý´úÂ룺
#include <sys/select.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <error.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <assert.h>
int
main()
{
int sockfd = socket(AF_INET,SOCK_DGRAM,0);
assert(sockfd>0);
struct sockaddr_in local;
local.sin_family = AF_INET;
local.sin_port = htons(8000);
local.sin_addr.s_addr = INADDR_ANY;
int err = bind(sockfd,(struct sockaddr*)&local,sizeof(local));
assert(err==0);
fd_set sets[3];
for(int i=0;i<3;i++)
{
FD_ZERO(&sets[i]);
}
timeval tv;
char buf[1024];
while(1)
{
FD_SET(sockfd,&sets[0]);
//register
tv.tv_sec = 1;
//monitor and handle
int nfds = select(sockfd+1,&sets[0],&sets[1],&sets[2],&tv);
if(nfds<0)
{
perror("select");
exit(-1);
}
printf("evnet num = %d\n",nfds);
//handle
for(int i=0;i<nfds;i++)
{
struct sockaddr_in remo
Ïà¹ØÎĵµ£º
ÓÉÓÚ Linux Á¼ºÃµÄÓû§È¨ÏÞ¹ÜÀíÌåϵ£¬²¡¶¾ÍùÍùÊÇ Linux ϵͳ¹ÜÀíÔ±×îºó²ÅÐèÒª¿¼ÂǵÄÎÊÌâ¡£ÒÔÍù£¬Linux ÉϵÄɱ¶¾Èí¼þÖ÷ÒªÊÇΪÆóÒµµÄÓʼþºÍÎļþ·þÎñÆ÷ËùÉè¼ÆµÄ¡£Èç½ñ£¬Ëæ×Å Linux ×ÀÃæÓû§ÊýÁ¿µÄÔö³¤£¬×ÀÃæÓû§ÔÚÊÜÒæÓÚ Linux ϵͳ¶Ô²¡¶¾½ÏÇ¿µÄÌìÈ»ÃâÒßÁ¦µÄͬʱ£¬Ò²ÐèҪɱ¶¾Èí¼þÇåÀí´ÓÍøÂç»òUÅÌ´øÀ´µÄWIndows²¡¶¾¡£¾¡¹ÜÄÇЩ ......
linuxµÄÍø¿¨IPµØÖ·ÊÇ´æ·ÅÔÚÎĵµÖеģ¬Õâ¸öÅäÖÃÎĵµÔÚ/etc/sysconfig/network-scriptsÏ£¬Ãû³Æ·Ö±ðΪifcfg-eth0,ifcfg-eth1....¼ÙÈçÄúÓÐÒ»¿éÍø¿¨£¬¾ÍÖ»ÓÐifcfg-eth0Ò»¸öÎĵµ£¬¼ÙÈçÄúÓÐÁ½¿é»òÁ½¿éÒÔÉϵÄÍø¿¨£¬¾Í»áÓÐifcfg-eth1¡¢ifcfg-eth2µÈÎĵµµÄ³öÏÖ¡£
Îĵµ½á¹¹£º
DEVICE=eth0 &n ......
ÕªÒª:ÊÓƵͼÏñ²É¼¯¼°´¦Àí¼¼ÊõÔÚÔ¶³ÌÊÓƵ¼à¿ØºÍ¿ÉÊӵ绰ÖÐÓÐ׏㷺µÄÓ¦ÓÃÇ°¾°£¬Çý¶¯ÊÓƵ²É¼¯É豸ºÍ»ñÈ¡ÊÓƵÊý¾Ý²¢½øÐÐÏàÓ¦µÄ´¦Àí£¬ÊÇʵÏÖÕâЩӦÓõĻù´¡¡£Õë¶ÔÕâЩӦÓ㬹¹½¨ÁËÒ»¸ö»ùÓÚǶÈëʽ LinuxºÍ PXA270΢´¦ÀíÆ÷µÄÊÓƵ²É¼¯Óë´«Êäϵͳ£¬ÀûÓà Video4LinuxʵÏÖ USBÉãÏñÍ·ÊÓƵÊý¾Ý²É¼¯£¬²É¼¯µÄÊÓƵÊý¾Ý¾ JPEGѹËõº ......
Linux MakefileÒѱ»¹ã·ºÓ¦Óõ«ÊÇÒ²ÔÚ²»¶ÏµÄ¸üУ¬ÕâÀï½éÉÜLinux Makefile°²×°ÉèÖÃʹÓ㬰ïÖú´ó¼Ò°²×°¸üÐÂLinux Makefileϵͳ¡£×÷ΪLinuxϵijÌÐò¿ª·¢ÈËÔ±£¬´ó¼ÒÒ»¶¨¶¼Óöµ½¹ýLinux Makefile£¬ÓÃmakeÃüÁîÀ´±àÒë×Ô¼ºÐ´µÄ³ÌÐòȷʵÊǺܷ½±ã¡£Ò»°ãÇé¿öÏ£¬´ó¼Ò¶¼ÊÇÊÖ¹¤Ð´Ò»¸ö¼òµ¥Linux Makefile£¬Èç¹ûÒªÏëд³öÒ»¸ö·ûºÏ×ÔÓÉÈí¼þ¹ßÀ ......