linux UDP C++网络编程演示

1.引用头文件

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

2.创建数据报套接字

int socket_fd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
    if (socket_fd<0)
    {
        perror("创建socket失败");
        exit(-1);

    }

3.绑定套接字

struct sockaddr_in server_addr;
    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_port=htons(port);
    server_addr.sin_addr.s_addr = htons(INADDR_ANY);
    server_addr.sin_family = AF_INET;

    if(bind(socket_fd,(struct sockaddr*)&server_addr, sizeof(server_addr)))
    {
        perror("绑定UDP服务失败");
        close(socket_fd);
        exit(-1);
    }

4.接收与发送数据

sendto(socket_fd,send_buf, strlen(send_buf),0,(struct sockaddr*)&client_addr,client_addr_len);
        int recv_len = recvfrom(socket_fd,recv_buf, sizeof(recv_buf),0,(struct sockaddr*)&client_addr,&client_addr_len);
        if (recv_len>0)
        {
            printf("%s\n",recv_buf);
        }

服务器完整代码:

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

int main() {
    std::cout << "UDP网络编程!" << std::endl;

    unsigned short port=8000;
    int socket_fd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
    if (socket_fd<0)
    {
        perror("创建socket失败");
        exit(-1);

    }

    struct sockaddr_in server_addr;
    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_port=htons(port);
    server_addr.sin_addr.s_addr = htons(INADDR_ANY);
    server_addr.sin_family = AF_INET;

    if(bind(socket_fd,(struct sockaddr*)&server_addr, sizeof(server_addr)))
    {
        perror("绑定UDP服务失败");
        close(socket_fd);
        exit(-1);
    }

    char send_buf[512]="udp server message";
    char recv_buf[512]="";
    unsigned short client_port = 9000;
    struct sockaddr_in client_addr;
    socklen_t client_addr_len = sizeof(sockaddr_in);
    bzero(&client_addr, sizeof(client_addr));
    client_addr.sin_family = AF_INET;
    client_addr.sin_port =htons(client_port);
    client_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    while (true)
    {
        sendto(socket_fd,send_buf, strlen(send_buf),0,(struct sockaddr*)&client_addr,client_addr_len);
        int recv_len = recvfrom(socket_fd,recv_buf, sizeof(recv_buf),0,(struct sockaddr*)&client_addr,&client_addr_len);
        if (recv_len>0)
        {
            printf("%s\n",recv_buf);
        }

    }

    return 0;
}

客户端完整代码:

//
// Created by linux on 2021/2/9.
//

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

int main(int argc,char *argv[])
{
    unsigned short port = 9000;
    int socket_fd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
    if (socket_fd<0)
    {
        perror("创建socket失败");
        exit(-1);

    }

    struct sockaddr_in server_addr;
    socklen_t server_addr_len = sizeof(server_addr);
    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_port=htons(port);
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    server_addr.sin_family = AF_INET;

    bind(socket_fd,(struct sockaddr*)&server_addr, sizeof(server_addr));

    char recv_buf[512] = "";
    char send_buf[512] = "udp client data";
    unsigned short client_port = 8000;
    struct sockaddr_in client_addr;
    socklen_t client_addr_len = sizeof(sockaddr_in);
    bzero(&client_addr, sizeof(client_addr));
    client_addr.sin_family = AF_INET;
    client_addr.sin_port =htons(client_port);
    client_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    while (true)
    {
        sendto(socket_fd,send_buf, sizeof(send_buf),0,(struct sockaddr*)&client_addr,client_addr_len);
        int recv_len = recvfrom(socket_fd,recv_buf, sizeof(recv_buf),0,(struct sockaddr*)&server_addr, &server_addr_len);
        if (recv_len>0)
        {
            printf("接收的长度:%d,数据:%s\r\n",recv_len,recv_buf);
        }

    }
    close(socket_fd);
    return 0;
}

 

上一篇:从IO模型到协程(二) BIO模型和NIO模型


下一篇:recv&send函数