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

Linux ÍøÂçµÄ¶à²¥ IP ¼¼Êõ

·¢²¼Ê±¼ä:2005-06-10 00:10:53À´Ô´:ºìÁª×÷Õß:linux_
×÷Õߣº²Ü½­»ª

¶à²¥¹ã²¥ÊÇÓÃÓÚ½¨Á¢·Ö²½Ê½ÏµÍ³£ºÀýÈçÍøÂçÓÎÏ·¡¢ICQ ÁÄÌì¹¹½¨¡¢Ô¶³ÌÊÓƵ»áÒéϵͳµÄÖØÒª¹¤¾ß¡£Ê¹Óöಥ¹ã²¥µÄ³ÌÐòºÍ UDP Ïòµ¥¸ö½éÉÜ·½·¢ËÍÐÅÏ¢µÄ³ÌÐòÏàËÆ¡£Çø±ðÔÚÓڶಥ¹ã²¥³ÌÐòʹÓÃÌØÊâµÄ¶à²¥ IP µØÖ·¡£

Ŀ¼

Ò»¡¢ÅäÖà Linux Ö§³Ö¶à²¥ IP
¶þ¡¢Ê¹Óà Linux ¶à²¥ IP ¹ã²¥Êý¾Ý

ÀýÈç±¾µØ¼ÆËã»úµÄµÄIPµØÖ·ÊÇ£º127.0.0.1 ¶þËüµÄ¶à²¥µØÖ·ÊÇ£º 224.0.0.1¡£ÕâÊÇÓÉ RCF 1390 ¶¨ÒåµÄ¡£Îª·¢ËÍ IP ¶à²¥Êý¾Ý£¬·¢ËÍÕßÐèҪȷ¶¨Ò»¸öºÏÊʵĶಥµØÖ·£¬Õâ¸öµØÖ·´ú±íÒ»¸ö×é¡£IPv4 ¶à²¥µØÖ·²ÉÓà D Àà IP µØÖ·È·¶¨¶à²¥µÄ×é¡£ÔÚ Internet ÖУ¬¶à²¥µØÖ··¶Î§ÊÇ´Ó 224.0.0.0 µ½ 234.255.255.255¡£ÆäÖбȽÏÖØÒªµÄµØÖ·ÓУº

¡¡¡¡224.0.0.1 £­ Íø¶ÎÖÐËùÓÐÖ§³Ö¶à²¥µÄÖ÷»ú

¡¡¡¡224.0.0.2 £­ Íø¶ÎÖÐËùÓÐÖ§³Ö¶à²¥µÄ·ÓÉÆ÷

¡¡¡¡224.0.0.4 £­ Íø¶ÎÖÐËùÓÐµÄ DVMRP ·ÓÉÆ÷

¡¡¡¡224.0.0.5 £­ ËùÓÐµÄ OSPF ·ÓÉÆ÷

¡¡¡¡224.0.0.6 £­ ËùÓÐµÄ OSPF Ö¸ÅÉ·ÓÉÆ÷

¡¡¡¡224.0.0.9 £­ ËùÓÐ RIPv2 ·ÓÉÆ÷

IPv6 µØÖ·¿Õ¼äÖÐÓÐ 1/256 µÄµØÖ·¿Õ¼ä·ÖÅä¸ø¶à²¥µØÖ·¡£Ò»¸öFF£¨11111111£©Öµ±êʶ¸ÃµØÖ·ÊǶಥµØÖ·¡£±êʶ¶Î¸ßÈýλʼÖÕÉèÖÃΪ 0 ²¢±£Áô¡£µÚËÄλT±êʶÉèÖÃΪ 0 ʱ±íʾһ¸öÓÀ¾Ã·ÖÅäµÄ¶à²¥µØÖ·¡£T ±êʶÉèÖÃΪ 1 ʱ£¬±íʾ·ÇÓÀ¾Ã·ÖÅäµÄ¶à²¥µØÖ·£¬ÕâÖÖµØÖ·×÷Ϊһ¸öÁÙʱµÄ¶à²¥µØÖ·¡£

Ò»¡¢ÅäÖà Linux Ö§³Ö¶à²¥ IP

ÔÚĬÈÏ״̬Ï£¬´ó¶à Linux ·¢Ðа汾¹Ø±ÕµÄ¶Ô¶à²¥ IP µÄÖ§³Ö¡£ÎªÁËÔÚ Linux ϵͳʹÓöಥÌ×½Ó¿Ú£¬ÐèÒª´ÓÐÂÅäÖúͱàÒë Linux Äںˡ£ÏÂÃæ¿´Ò»ÏÂÅäÖò½Ö裺

1.cd /usr/src/linux

2.make menuconfig

3.Ñ¡ÔñÍøÂçÑ¡Ïî

4.Ñ¡ÖÐ IP£ºEnable Multicasting IP Ò»Ïî

5.±£´æ²¢´Ó menuconfig Í˳ö

6.ÔËÐУºmake dep£»make clean£»make bzlmage

7.cp/vmlinuz/vdimLz_good

8.cparch/i386/boot/zImage/vmlinzz

9.cd/etc

10.±à¼­ lilo£®conf£¬¼ÓÈëÕë¶Ô /vmlinuz_good µÄÄÚºËÐÂÑ¡Ïî

11.ÔËÐÐ li1o

Linux Äں˱àÒëºó£¬ÒÔ³¬¼¶Óû§Éí·ÝÔËÐÐÃüÁ

££router add -net 224.0.0.0 netmask 224.0.0.0 dev lo

ºËʵÃüÁîÊÇ·ñ¼ÓÈëϵͳ£¬ÔËÐÐÃüÁ

££route -eKernel IP routing table
Destination gatewary Genmask Flags MSS Window irtt Iface
10.0.0.0 * 255.255.255.0 U 0 0 0 eth0
127.0.0.0 * 255.0.0.0 U 0 0 0 lo
BASE_ADDRESS>MC * 240.0.0.0 U 0 0 0 lo
Default 10.0.0.1 0.0.0.0 UG 0 0 0 eth0

ÆäÖгöÏֶಥµØÖ·£º 224.0.0.1¡£¾Í±íʾÅäÖóɹ¦ÁË¡£

¶þ¡¢Ê¹Óà Linux ¶à²¥ IP¹ã²¥Êý¾Ý

1¡¢Ê×ÏÈÔÚ·þÎñÆ÷¶Ë½¨Á¢¶à²¥³ÌÐò£º

·þÎñÆ÷¶Ë³ÌÐò´úÂëºÍ½âÊÍ£º

/* * broadcast.c - An IP multicast server */ #include #include #include #include #include #include #include int port = 6789; int main(void) { int socket_descriptor; struct sockaddr_in address; /* Ê×ÏȽ¨Á¢Ì×½Ó¿Ú */ socket_descriptor = socket(AF_INET, SOCK_DGRAM, 0); if (socket_descriptor == -1) { perror("Opening socket"); exit(EXIT_FAILURE); } /* ³õʼ»¯IP¶à²¥µØÖ· */ memset(&address, 0, sizeof(address)); address.sin_family = AF_INET; address.sin_addr.s_addr = inet_addr("224.0.0.1"); address.sin_port = htons(port); /* ¿ªÊ¼½øÐÐIP¶à²¥ */ while(1) { if(sendto(socket_descriptor, "test from broadcast", sizeof("test from broadcast"), 0, (struct sockaddr *)&address, sizeof(address)) < 0) { perror("sendto"); exit(EXIT_FAILURE); } sleep(2); } exit(EXIT_SUCCESS);}

2¡¢´´½¨ Linux ¿Í»§¶ËµÄ³ÌÐò¶à²¥ IP ¹ã²¥

½¨Á¢ºÅ¹ã²¥·þÎñÆ÷ºó£¬¾ÍÐèÒªÒ»¸ö¿Í»§¶ËµÄÊÕÌý³ÌÐò£¬ÊÕÌý¶à²¥ IP ¹ã²¥ÒªÇóÔÚ³ÌÐòÖÐ×÷ÒÔϼ¸Ï×÷£º

£¨1£©¡¢¶à²¥·½ÊÕÌý£¬¿Í»§¶Ë±àд֪ͨ Linux ÄÚºËÿ¸öÖ¸¶¨µÄÌ×½Ó¿Ú¼ÓÈë¶à²¥ IP ¹ã²¥×é¡£

£¨2£©¡¢ÊÕÌý·½±ØÐëÔËÐÐÔÚͬһ¸ö Linux ¼ÆËã»úµÄ²»Í¬½ø³ÌµÄͬһ¸öÌ×½Ó¿Ú¡£

£¨3£©¡¢±àдÅäÖõ¤½­¿ÚÉ趨¹ã²¥ÐÅÏ¢¿ÉÒÔ·¢Ë͸øͬһ¸ö Linux Ö÷»ú£¬ÕâÑù×÷µÄÓô¦ÊÇÔÚͬһ¸ö Linux Ö÷»úÉϲâÊԹ㲥³ÌÐòºÍÊÕÌý³ÌÐò£¬Ò×ÓÚµ÷ÊÔ¡£

¿Í»§¶Ë³ÌÐò´úÂëºÍ½âÊÍ

/* * listen.c - An IP multicast client */ #include #include #include #include #include #include #include char * host_name = "224.0.0.1"; /* ¶à²¥IPµØÖ· */ int port = 6789; int main(void) { struct ip_mreq command; int loop = 1; /* ¶à²¥Ñ­»· */ int iter = 0; int sin_len; char message[256]; int socket_descriptor; struct sockaddr_in sin; struct hostent *server_host_name; if((server_host_name = gethostbyname(host_name)) == 0) { perror("gethostbyname"); exit(EXIT_FAILURE); } /*bzero(&sin, sizeof(sin));*/ memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_port = htons(port); if((socket_descriptor = socket(PF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(EXIT_FAILURE); } /* µ÷ÓÃbind֮ǰ£¬ÉèÖÃÌ×½Ó¿ÚÑ¡ÏîÆôÓöಥIPÖ§³Ö*/ loop = 1; if(setsockopt(socket_descriptor, SOL_SOCKET, SO_REUSEADDR, &loop, sizeof(loop)) < 0) { perror("setsockopt:SO_REUSEADDR"); exit(EXIT_FAILURE); } if(bind(socket_descriptor, (struct sockaddr *)&sin, sizeof(sin)) < 0) { perror("bind"); exit(EXIT_FAILURE); } /* ÔÚͬһ¸öÖ÷»úÉϽøÐй㲥ÉèÖÃÌ×½Ó¿Ú£¬ ×÷ÓÃÊÇ·½±ãµ¥¸ö¿ª·¢ÏµÍ³ÉϲâÊԶಥIP¹ã²¥ */ loop = 1; if(setsockopt(socket_descriptor, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0) { perror("setsockopt:IP_MULTICAST_LOOP"); exit(EXIT_FAILURE); } /* ¼ÓÈëÒ»¸ö¹ã²¥×é¡£½øÒ»²½¸æËßLinuxÄںˣ¬ Ìض¨µÄÌ×½Ó¿Ú¼´½«½ÓÊܹ㲥Êý¾Ý*/ command.imr_multiaddr.s_addr = inet_addr("224.0.0.1"); command.imr_interface.s_addr = htonl(INADDR_ANY); if(command.imr_multiaddr.s_addr == -1) { perror("224.0.0.1 not a legal multicast address"); exit(EXIT_FAILURE); } if (setsockopt(socket_descriptor, IPPROTO_IP, IP_ADD_MEMBERSHIP, &command, sizeof(command)) < 0) { perror("setsockopt:IP_ADD_MEMBERSHIP"); } while(iter++ < 8) { sin_len = sizeof(sin); if(recvfrom(socket_descriptor, message, 256, 0, (struct sockaddr *)&sin, &sin_len) == -1) { perror("recvfrom"); } printf("Response #%-2d from server: %s\n", iter, message); sleep(2); } /* ½ÓÊÜ8¸ö¹ã²¥ºóÍ˳ö */ if(setsockopt(socket_descriptor, IPPROTO_IP, IP_DROP_MEMBERSHIP, &command, sizeof(command)) < 0) { perror("setsockopt:IP_DROP_MEMBERSHIP"); } close(socket_descriptor); exit(EXIT_SUCCESS);}

3¡¢ÔËÐÐ Linux ¶à²¥ IP ³ÌÐò

ÔËÐгÌÐò£¬´ò¿ªÁ½¸öÖն˴°¿Ú²¢ÔÚÿ¸ö´°¿ÚÖÐÊäÈëÒÔÉϵÄÔ´´úÂë¡£ÔÚÒ»¸ö´°¿ÚÖмüÈë make£¬±àÒë broadcast ºÍ listen ÕâÁ½¸öµÄ¿ÉÖ´ÐÐÎļþ,ÓÉ GUN make ½¨Á¢Ò»¸ö Makfiles Îļþ¡£GUN make ÊÇ Linux ϵͳÖÐÒ»¸ö×Ô¶¯Éú³ÉºÍά»¤Ä¿±ê³ÌÐòµÄ¹¤¾ß¡£

ÔÚÒ»¸ö´°¿ÚÖÐͨ¹ýÖ´ÐÐ ./broadcast,ÔÚÁíÒ»¸öµÄ´°¿ÚÖ´ÐÐ ./listen£¬Æô¶¯ÊÕÌý³ÌÐò£¬ÄãÓ¦¸Ã¿´µ½ÈçÏÂÊä³ö£º


££./listenResponse #1 form sever :test from broadcast Response #2 form sever :test from broadcast Response #3 form sever :test from broadcast Response #4 form sever :test from broadcast Response #5 form sever :test from broadcast Response #6 form sever :test from broadcast Response #7 form sever :test from broadcast Response #8 form sever :test from broadcast #

4¡¢×ܽá

Linux ¶à²¥ IP ÊÇÒ»ÖÖͬʱÏò¼Û¸ñ½ø³Ì¸ßЧµÄ·¢ËÍÐÅÏ¢µÄ½éÉÜ¡£¶à²¥´«ÊäÖУ¬Êý¾Ý±»·¢Ë͵½½ÓÊÕÕߵĶಥµØÖ·£¬¶ø²»ÊÇÿ¸ö½ÓÊÕÕߵĵ¥²¥µØÖ·£¬·¢ËÍÕßÖ»·¢ËÍÒ»¸öÊý¾Ý¿½±´£¬Ô´¶Ëµ½Ä¿±ê¶Ë·¾¶ÉϵÄÖмä½Úµã¸´ÖƸÃÊý¾Ý¡£ÏÖÔڶಥ IP ÒѾ­¹ã·ºÓ¦ÓÃÓÚÍøÂçÓÎÏ·¡¢ÊÓƵ¹ã²¥µÄÁìÓò¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 6 ÌõÆÀÂÛ

  1. 657129880@qq.co ÓÚ 2013-04-21 18:12:34·¢±í:

    лл·ÖÏí£¡£¡£¡

  2. juyo ÓÚ 2005-12-22 09:02:35·¢±í:

    ÊܽÌÁË

  3. °®ÁãÕûÕû ÓÚ 2005-10-24 00:13:24·¢±í:

    ²»´í£¬Ö§³Ö

  4. bluesky ÓÚ 2005-09-26 00:01:06·¢±í:

    ÎÒ¶¥~~~~~~~~~~~~~~`

  5. °®µÄ±ßÔµ ÓÚ 2005-09-21 00:08:46·¢±í:

    ²»´í

  6. »ð ÓÚ 2005-07-09 00:09:28·¢±í:

    Ö§³Ö