LinuxÐÒéÕ»Öг£¼ûµÄÍøÂçÐÒé¼°¹¦Ð§ÏÈÈÝ
Linux×÷Ϊһ¸ö¿ªÔ´²Ù×÷ϵͳ£¬ÆäÍøÂçÐÒéÕ»ÊÎÑÝ×ÅÖÁ¹ØÖ÷ÒªµÄ½ÇÉ«£¬ÈÏÕæ´¦ÀíÍøÂçÊý¾ÝµÄ´«Ê䡢·ÓÉ¡¢ÅþÁ¬ÖÎÀíµÈ¹¦Ð§¡£±¾ÎĽ«ÏÈÈÝLinuxÐÒéÕ»Öг£¼ûµÄһЩÍøÂçÐÒé¼°Æ书Ч£¬²¢¸½ÉÏÏêϸµÄ´úÂëʾÀý¡£
TCPÐÒé
TCP£¨Transmission Control Protocol£©ÊÇÒ»ÖÖÃæÏòÅþÁ¬µÄ¡¢¿É¿¿µÄ¡¢»ùÓÚ×Ö½ÚÔ¼µÄ´«ÊäÐÒé¡£ËüÌṩÁËÊý¾ÝÍêÕûÐÔУÑé¡¢Á÷Á¿¿ØÖÆ¡¢ÓµÈû¿ØÖƵȹ¦Ð§£¬°ü¹ÜÊý¾ÝµÄ¿É¿¿´«Êä¡£
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʹÓÃTCPÐÒé¾ÙÐпͻ§¶ËÓëЧÀÍÆ÷ͨѶµÄʾÀý£º
// TCP client #include <sys/socket.h> #include <netinet/in.h> #include <stdio.h> #include <string.h> int main() { int sockfd; struct sockaddr_in server_addr; char buffer[1024]; sockfd = socket(AF_INET, SOCK_STREAM, 0); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8080); server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)); send(sockfd, "Hello, server!", strlen("Hello, server!"), 0); recv(sockfd, buffer, sizeof(buffer), 0); printf("%s ", buffer); close(sockfd); return 0; }
µÇ¼ºó¸´ÖÆ
// TCP server #include <sys/socket.h> #include <netinet/in.h> #include <stdio.h> #include <string.h> int main() { int sockfd, client_sock; struct sockaddr_in server_addr, client_addr; char buffer[1024]; sockfd = socket(AF_INET, SOCK_STREAM, 0); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8080); server_addr.sin_addr.s_addr = htonl(INADDR_ANY); bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)); listen(sockfd, 5); int addr_len = sizeof(client_addr); client_sock = accept(sockfd, (struct sockaddr*)&client_addr, &addr_len); recv(client_sock, buffer, sizeof(buffer), 0); printf("%s ", buffer); send(client_sock, "Hello, client!", strlen("Hello, client!"), 0); close(client_sock); close(sockfd); return 0; }
µÇ¼ºó¸´ÖÆ
UDPÐÒé
UDP£¨User Datagram Protocol£©ÊÇÒ»ÖÖÎÞÅþÁ¬µÄ¡¢²»¿É¿¿µÄ´«ÊäÐÒ飬Ëü²»°ü¹ÜÊý¾ÝµÄ¿É¿¿ÐÔÎÂ˳ÐòÐÔ£¬ÊÊÓÃÓÚһЩʵʱÐÔÒªÇó½Ï¸ßµÄ³¡¾°¡£
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʹÓÃUDPÐÒé¾ÙÐпͻ§¶ËÓëЧÀÍÆ÷ͨѶµÄʾÀý£º
// UDP client #include <sys/socket.h> #include <netinet/in.h> #include <stdio.h> #include <string.h> int main() { int sockfd; struct sockaddr_in server_addr; char buffer[1024]; sockfd = socket(AF_INET, SOCK_DGRAM, 0); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8080); server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); sendto(sockfd, "Hello, server!", strlen("Hello, server!"), 0, (struct sockaddr*)&server_addr, sizeof(server_addr)); recv(sockfd, buffer, sizeof(buffer), 0); printf("%s ", buffer); close(sockfd); return 0; }
µÇ¼ºó¸´ÖÆ
// UDP server #include <sys/socket.h> #include <netinet/in.h> #include <stdio.h> #include <string.h> int main() { int sockfd; struct sockaddr_in server_addr, client_addr; char buffer[1024]; sockfd = socket(AF_INET, SOCK_DGRAM, 0); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8080); server_addr.sin_addr.s_addr = htonl(INADDR_ANY); bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)); int addr_len = sizeof(client_addr); recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&client_addr, &addr_len); printf("%s ", buffer); sendto(sockfd, "Hello, client!", strlen("Hello, client!"), 0, (struct sockaddr*)&client_addr, addr_len); close(sockfd); return 0; }
µÇ¼ºó¸´ÖÆ
³ýÁËTCPºÍUDPÐÒéÍ⣬LinuxÐÒéÕ»»¹Ö§³ÖÖîÈçIPÐÒé¡¢ICMPÐÒé¡¢ARPÐÒéµÈÍøÂçÐÒ飬ËüÃÇÅäºÏ×é³ÉÁËÒ»¸öÍêÕûµÄÍøÂçͨѶϵͳ£¬ÎªÓ¦ÓóÌÐòÌṩÁ˸»ºñµÄÍøÂçͨѶ¹¦Ð§¡£Í¨¹ýѧϰºÍÃ÷È·ÕâЩÍøÂçÐÒ飬ÎÒÃÇ¿ÉÒÔ¸üºÃµØʹÓÃLinux²Ù×÷ϵͳ¾ÙÐÐÍøÂç±à³Ì£¬¹¹½¨¸ßЧ¿É¿¿µÄÍøÂçÓ¦Óá£
ÒÔÉϾÍÊÇLinuxÐÒéÕ»Öг£¼ûµÄÍøÂçÐÒé¼°¹¦Ð§ÏÈÈݵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡