Linux网络编程-UDP

UDP通信

服务端

  • udp_server.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    /*
    服务端的开发流程:
    1.创建socket
    udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
    2.绑定--bind()
    3.传输数据
    while(1)
    {
    //读取数据
    n = recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr *)&client, &len);

    //发送数据
    sendto(cfd, buf, n, 0, (struct sockaddr *)&client, len);
    }
    4.关闭socket套接字
    close(cfd);
    */
    //udp服务端
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <ctype.h>

    int main()
    {
    //创建socket
    int cfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (cfd < 0)
    {
    perror("socket error");
    return -1;
    }

    //绑定
    struct sockaddr_in client;
    struct sockaddr_in serv;
    bzero(&serv, sizeof(serv));
    serv.sin_family = AF_INET;
    serv.sin_port = htons(8888);
    serv.sin_addr.s_addr = htonl(INADDR_ANY);
    bind(cfd, (struct sockaddr *)&serv, sizeof(serv));

    int i;
    int n;
    char buf[1024];
    socklen_t len;
    while (1)
    {
    //读取数据
    memset(buf, 0x00, sizeof(buf));
    len = sizeof(client);
    n = recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr *)&client, &len);

    //将大写转换为小写
    for (i = 0; i < n; i++)
    {
    buf[i] = toupper(buf[i]);
    }
    printf("port=[%d], n=[%d], buf=[%s]\n", ntohs(client.sin_port), n, buf);
    //发送数据
    sendto(cfd, buf, n, 0, (struct sockaddr *)&client, len);
    }
    //关闭文件描述符
    close(cfd);

    return 0;
    }

客户端

  • udp_client.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    /*
    客户端的开发流程:
    1.创建socket
    udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
    2.传输数据
    while(1)
    {
    //发送数据
    sendto(cfd, buf, n, 0, (struct sockaddr *)&client, len);

    //读取数据
    n = recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr *)&client, &len);
    }
    3.关闭socket套接字
    close(cfd);
    */
    //udp客户端
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <ctype.h>

    int main()
    {
    //创建socket
    int cfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (cfd < 0)
    {
    perror("socket error");
    return -1;
    }

    struct sockaddr_in serv;
    serv.sin_family = AF_INET;
    serv.sin_port = htons(8888);
    inet_pton(AF_INET, "127.0.0.1", &serv.sin_addr.s_addr);

    int n;
    char buf[1024];

    while (1)
    {
    //读标准输入数据
    memset(buf, 0x00, sizeof(buf));
    n = read(STDIN_FILENO, buf, sizeof(buf));

    //发送数据
    sendto(cfd, buf, n, 0, (struct sockaddr *)&serv, sizeof(serv));

    //读取数据
    memset(buf, 0x00, sizeof(buf));
    n = recvfrom(cfd, buf, sizeof(buf), 0, NULL, NULL);
    printf("n=[%d], buf=[%s]\n", n, buf);
    }
    //关闭文件描述符
    close(cfd);

    return 0;
    }

Linux网络编程-UDP
https://lcf163.github.io/2021/05/01/Linux网络编程-UDP/
作者
乘风的小站
发布于
2021年5月1日
许可协议