Linux高并发服务器开发(十一)UDP通信和本地socket通信

文章目录


1 TCP和UDP的区别

2 UDP

API

流程

服务端流程


客户端流程

代码

服务端

cpp 复制代码
#include<sys/socket.h>
#include<stdio.h>
#include<arpa/inet.h>
#include <unistd.h>
#include<sys/time.h>
#include<sys/types.h>
#include<sys/select.h>
#include<sys/epoll.h>
#include <fcntl.h>
#include <strings.h>
#include <string.h>
#include <ctype.h>

int main()
{
    // 创建套接字
    int cfd = socket(AF_INET,SOCK_DGRAM,0);
    if(cfd<0)
    {
        perror("socket error");
        return -1;
    }
    // 绑定
    struct sockaddr_in serv;
    struct sockaddr_in client;

    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));
    
    // while中收发数据
    int n;
    socklen_t len;
    char buf[1024] = "";
    while(1)
    {
        memset(buf,0x00,sizeof(buf));
        len = sizeof(client);
        n = recvfrom(cfd, buf, sizeof(buf),0,(struct sockaddr*)&client, &len);

        // 将大写转换为小写
        for(int i=0;i<n;i++)
        {
            buf[i] = toupper(buf[i]);
        }

        sendto(cfd,buf,n,0, (struct sockaddr*) &client, len);
    }

    close(cfd);
    return 0;
}

客户端

cpp 复制代码
#include<sys/socket.h>
#include<stdio.h>
#include<arpa/inet.h>
#include <unistd.h>
#include<sys/time.h>
#include<sys/types.h>
#include<sys/select.h>
#include<sys/epoll.h>
#include <fcntl.h>
#include <strings.h>
#include <string.h>
#include <ctype.h>

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


  
    
    // while中收发数据
    int n;
    socklen_t len;
    char buf[1024] = "";
    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);

    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;
}

3 本地socket通信

创建成功后,会在内核中创建缓冲区



流程

服务端

cpp 复制代码
#include<sys/socket.h>
#include<stdio.h>
#include<arpa/inet.h>
#include <unistd.h>
#include<sys/types.h>
#include <fcntl.h>
#include <strings.h>
#include <string.h>
#include <ctype.h>
#include <sys/un.h>

int main()
{
    // 创建socket
    int lfd = socket(AF_UNIX, SOCK_STREAM, 0);
    if(lfd<0)
    {
        perror("socket error");
        return 1;
    }

    // 删除 serv.sock文件
    unlink("serv.sock");

    // 绑定
    struct sockaddr_un serv;
    serv.sun_family = AF_UNIX;
    strcpy(serv.sun_path, "./serv.sock");
    int ret = bind(lfd, (struct sockaddr*)&serv,sizeof(serv));
    if(ret < 0)
    {
        perror("bind");
        return 1;
    }
    // 监听
    listen(lfd,128);
    // 接受新的客户端连接
    struct sockaddr_un client;
    socklen_t len = sizeof(client);
    int cfd = accept(lfd, (struct sockaddr*)&client, &len);
    if(cfd < 0)
    {
        perror("accpet error");
        return -1;
    }

    int n;
    int i;
    char buf[1024];
    while(1)
    {
        memset(buf, 0x00, sizeof(buf));
        // 读数据
        n = read(cfd, buf, sizeof(buf));
        if(n<=0)
        {
            printf("read error or  client clsoe");
            break;
        }
        // 发送数据
        for(i = 0;i<n;i++)
        {
            buf[i] = toupper(buf[i]);
        }

        write(cfd,buf,n);
    }

    close(cfd);
    close(lfd);

    return 0;

}

客户端

客户端代码

cpp 复制代码
#include<sys/socket.h>
#include<stdio.h>
#include<arpa/inet.h>
#include <unistd.h>
#include<sys/types.h>
#include <fcntl.h>
#include <strings.h>
#include <string.h>
#include <ctype.h>
#include <sys/un.h>

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

    // 连接服务端
    struct sockaddr_un serv;
    serv.sun_family = AF_UNIX;
    strcpy(serv.sun_path, "./serv.sock");

    connect(cfd, (struct sockaddr*)&serv, sizeof(serv));

    int n;
    int i;
    char buf[1024];
    while(1)
    {
        memset(buf, 0x00, sizeof(buf));
        n = read(STDIN_FILENO, buf, sizeof(buf));
        // 发送数据
        write(cfd,buf,n);

        // 读数据
        memset(buf, 0x00, sizeof(buf));
        n = read(cfd, buf, sizeof(buf));
        if(n<=0)
        {
            printf("read error or  client clsoe");
            break;
        }
        
    }

    close(cfd);

    return 0;

}
相关推荐
Johny_Zhao4 小时前
OpenClaw安装部署教程
linux·人工智能·ai·云计算·系统运维·openclaw
tingshuo29174 小时前
S001 【模板】从前缀函数到KMP应用 字符串匹配 字符串周期
笔记
YuMiao18 小时前
gstatic连接问题导致Google Gemini / Studio页面乱码或图标缺失问题
服务器·网络协议
chlk1232 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑2 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件2 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
深紫色的三北六号2 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash2 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI3 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行3 天前
Linux和window共享文件夹
linux