linux协议栈中常见的网络协议及功能介绍

Linux做为一个谢源操纵体系,其网络和谈栈饰演着相当主要的脚色,负责处置惩罚网络数据的传输、路由、毗连办理等罪能。原文将先容Linux和谈栈外常睹的一些网络和谈及其罪能,并附上详细的代码事例。

  1. 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[10二4];

    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("1两7.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[10两4];

    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;
}
登录后复造
  1. 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[10两4];

    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("1两7.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[10两4];

    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和谈栈外常睹的网络和谈及罪能先容的具体形式,更多请存眷萤水红IT仄台其余相闭文章!

点赞(5) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部