udp与tcp数据模式区别-用代码解释

所有接触过网络编程的,都会被告之:udp传输的是数据报,tcp传输的是流。但是数据报和流到底有什么不同,怎么测试他们的不同呢?今天的这个小程序能很好的解释这个问题。

udp server

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdlib.h>
#include <arpa/inet.h>



int main(int argc, char **argv)
{
    struct sockaddr_in s_addr;
    struct sockaddr_in c_addr;
    int sock;
    socklen_t addr_len;
    int len;
    char buff[128];

    if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        perror("socket");
        exit(errno);
    }

    memset(&s_addr, 0, sizeof(struct sockaddr_in));
    s_addr.sin_family = AF_INET;

    if (argv[2])
        s_addr.sin_port = htons(atoi(argv[2]));
    else
        s_addr.sin_port = htons(7838);
    if (argv[1])
        s_addr.sin_addr.s_addr = inet_addr(argv[1]);
    else
        s_addr.sin_addr.s_addr = INADDR_ANY;

    if ((bind(sock, (struct sockaddr *) &s_addr, sizeof(s_addr))) == -1) {
        perror("bind");
        exit(errno);
    }

    addr_len = sizeof(c_addr);
    while (1) {//接收client发送的数据并打印到屏幕
        len = recvfrom(sock, buff, sizeof(buff) - 1, 0,
                (struct sockaddr *) &c_addr, &addr_len);
        if (len < 0) {
            perror("recvfrom");
            exit(errno);
        }

        buff[len] = '\0';

        printf("recv from: %s:%d message: %s\n", inet_ntoa(c_addr.sin_addr), ntohs(c_addr.sin_port), buff);
    }
    return 0;
}

udp client

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdlib.h>
#include <arpa/inet.h>

int main(int argc, char **argv)
{
    struct sockaddr_in d_addr;
    int sock;
    int addr_len;
    int len, i;
    char buff[3] = {'a', 'b', 'c'};//要发送的字符

    if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        perror("socket");
        exit(errno);
    }

    d_addr.sin_family = AF_INET;

    if (argv[2])
        d_addr.sin_port = htons(atoi(argv[2]));
    else
        d_addr.sin_port = htons(7838);

    if (argv[1])
        d_addr.sin_addr.s_addr = inet_addr(argv[1]);
    else {
        printf("reciver's ip must be set\n");
        exit(0);
    }

    addr_len = sizeof(d_addr);

    for (i = 0; i < sizeof(buff); i++)//分3次,每次发送一个字符
    {
        len = sendto(sock, &buff[i], 1, 0,
                (struct sockaddr *) &d_addr, addr_len);
        if (len < 0) {
            printf("send error.\n");
            return -1;
        }
    }

    printf("complete.\n");
    return 0;
}



tcp server

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdlib.h>
#include <arpa/inet.h>



int main(int argc, char **argv)
{
    struct sockaddr_in s_addr;
    struct sockaddr_in c_addr;
    int sock, new_sock;
    socklen_t addr_len;
    int len;
    char buff[128];

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(errno);
    }

    memset(&s_addr, 0, sizeof(struct sockaddr_in));
    s_addr.sin_family = AF_INET;

    if (argv[2])
        s_addr.sin_port = htons(atoi(argv[2]));
    else
        s_addr.sin_port = htons(7838);
    if (argv[1])
        s_addr.sin_addr.s_addr = inet_addr(argv[1]);
    else
        s_addr.sin_addr.s_addr = INADDR_ANY;

    if ((bind(sock, (struct sockaddr *) &s_addr, sizeof(s_addr))) == -1) {
        perror("bind");
        exit(errno);
    }

    if (listen(sock, 3) < 0)
    {
        perror("listen");
        exit(errno);
    }


    addr_len = sizeof(c_addr);
    while (1) {//接收client端发送过来的数据,并打印到终端
        new_sock = accept(sock, (struct sockaddr *)&c_addr, &addr_len);
        if (new_sock == -1)
        {
            perror("accept");
            exit(errno);
        }

        memset(buff, 0, sizeof(buff));

        len = recv(new_sock, buff, sizeof(buff) - 1, 0);
        if (len < 0) {
            perror("recv");
            exit(errno);
        }

        buff[len] = '\0';

        printf("recv from: %s:%d message: %s\n", inet_ntoa(c_addr.sin_addr), ntohs(c_addr.sin_port), buff);
    }
    return 0;
}


tcp client

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdlib.h>
#include <arpa/inet.h>

int main(int argc, char **argv)
{
    struct sockaddr_in d_addr;
    int sock;
    int addr_len;
    int len, i;
    char buff[3] = {'a', 'b', 'c'};

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(errno);
    }

    d_addr.sin_family = AF_INET;
    if (argv[2])
        d_addr.sin_port = htons(atoi(argv[2]));
    else
        d_addr.sin_port = htons(7838);
    if (argv[1])
        d_addr.sin_addr.s_addr = inet_addr(argv[1]);
    else {
        printf("reciver's ip must be set\n");
        exit(0);
    }

    addr_len = sizeof(d_addr);

    if (connect(sock, (struct sockaddr *) &d_addr, addr_len) != 0) {
        perror("Connect ");
        exit(errno);
    }

    for (i = 0; i < sizeof(buff); i++)//分3次发送,每次发送一个字符
    {
        len = send(sock, &buff[i], 1, 0);
        if (len < 0) {
            printf("send error.\n");
            return -1;
        }
    }

    printf("complete.\n");
    return 0;
}



执行结果:

udp:

$ ./udp_server 192.168.7.130 5678
recv from: 192.168.7.145:37102 message: a
recv from: 192.168.7.145:37102 message: b
recv from: 192.168.7.145:37102 message: c


tcp:

$ ./tcp_server 192.168.7.130 5678
recv from: 192.168.7.145:43973 message: abc


从上面的执行结果可以看到,udp每次接收到的是一个字符,而tcp则是组成了一个字符串,提供出来。这就所谓的udp数据报,和tcp流的区别。形成这样的原因是,在kernel中,udp数据有结束标志,可以知道这个数据包有多少数据,但是tcp则没有,所以调用recv获取的时候,就会将kernel接收到的数据全部copy过来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值