0
点赞
收藏
分享

微信扫一扫

栈和队列的经典例题,LeetCode 括号匹配问题;栈实现队列;队列实现栈;队列带环问题

TiaNa_na 2024-05-26 阅读 24

1、域套接字UNIX

2、流式域套接字

        2.1 服务器端实现

int main()
{
    //1、为通信创建一个端点
    int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
   
    if(sfd == -1)
    {
        perror("socket error");
        return -1;
    }


    //判断要绑定的套接字文件是否存在?
    if(access("./unix", F_OK) == 0)
    {
        //说明文件存在,要将其删除
        if(unlink("./unix") ==-1)
        {
            perror("unlink error");
            return -1;
        }
    }


    //2、绑定ip和端口号
    //2.1 准备地址信息结构体
    struct sockaddr_un sun;
    sun.sun_family = AF_UNIX;    //通信域
    strcpy(sun.sun_path , "./unix");         //套接字文件路径


    //2.2 绑定工作:必须绑定一个不存在的套接字文件
    if(bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) ==-1)
    {
        perror("bind error");
        return -1;
    }
    printf("bind success\n");

  
    //3、将套接字设置成被动监听状态
    if(listen(sfd, 128)==-1)
    {
        perror("listen error");
        return -1;
    }
    printf("listen success\n");

    //4、阻塞等待客户端的连接
    //4.1 定义用于接受客户端信息的容器
    struct sockaddr_un cun;
    socklen_t addrlen = sizeof(cun);

    int newfd = accept(sfd, (struct sockaddr*)&cun, &addrlen);
    if(newfd == -1)
    {
        perror("accept error");
        return -1;
    }

    printf("[%s]:发来连接请求\n", cun.sun_path);

    //5、与客户端进行相互通信
    char rbuf[128] = "";            //读取消息内容的容器

    while(1)
    {
        //清空容器
        bzero(rbuf, sizeof(rbuf));

        //从套接字中读取数据
        //int res = read(newfd, rbuf, sizeof(rbuf));
        int res = recv(newfd, rbuf, sizeof(rbuf), 0);
        if(res == 0)
        {
            printf("客户端已经下线\n");
            break;
        }

        //将读取的消息展示出来
        printf("[%s]:%s\n", cun.sun_path, rbuf);

        //将收到的消息处理一下,回复给客户端
        strcat(rbuf, "*_*");

        //讲消息发送给客户端
        send(newfd, rbuf, strlen(rbuf), 0);
        printf("发送成功\n");
    }

    //6、关闭套接字
    close(newfd);
    close(sfd);
    return 0;
}

        2.2 客户端实现

int main()
{
    //1、创建用于通信的套接字文件描述符
    int cfd = socket(AF_UNIX, SOCK_STREAM, 0);
    if(cfd == -1)
    {
        perror("socket error");
        return -1;
    }

    //判断要绑定的套接字文件是否存在?
    if(access("./linux", F_OK) == 0)
    {
        //说明文件存在,要将其删除
        if(unlink("./linux") ==-1)
        {
            perror("unlink error");
            return -1;
        }
    }


    //2、绑定IP地址和端口号 
    //2.1 填充客户端地址信息结构体
    struct sockaddr_un cun;
    cun.sun_family = AF_UNIX;
    strcpy(cun.sun_path, "./linux");

    //2.2 绑定
    if( bind(cfd, (struct sockaddr*)&cun, sizeof(cun)) ==-1)
    {
        perror("bind error");
        return -1;
    }
    printf("bind success\n");
    
    
    
    //3、连接服务器
    //3.1 准备对端地址信息结构体
    struct sockaddr_un sun;
    sun.sun_family = AF_UNIX;
    strcpy(sun.sun_path, "./unix");

    //3.2 连接服务器
    if(connect(cfd, (struct sockaddr*)&sun, sizeof(sun))==-1)
    {
        perror("connect error");
        return -1;
    }
    printf("connect success\n");
    
    //4、数据收发
    char wbuf[128] = "";
    char rbuf[128] = "";
    while(1)
    {
        //从终端上获取要发送的数据
        fgets(wbuf, sizeof(wbuf), stdin);
        wbuf[strlen(wbuf)-1] = '\0';           //将读取的换行改成'\0'

        //将数据发送给服务器
        send(cfd, wbuf, strlen(wbuf), 0);
        printf("发送成功\n");

        //接收服务器发来的消息
        bzero(rbuf, sizeof(rbuf));

        recv(cfd, rbuf, sizeof(rbuf), 0);
        printf("服务器发来的消息为:%s\n", rbuf);
    }
    
    //5、关闭套接字
    close(cfd);

    return 0;
}

3、报式域套接字

        3.1 服务器端实现

int main(int argc, const char *argv[])
{
    //1、创建用于通信的套接字文件描述符
    int sfd = socket(AF_UNIX, SOCK_DGRAM, 0);
    if(sfd == -1)
    {
        perror("socket error");
        return -1;
    }

    //判断要绑定的套接字文件是否存在?
    if(access("./unix", F_OK) == 0)
    {
        //说明文件存在,要将其删除
        if(unlink("./unix") ==-1)
        {
            perror("unlink error");
            return -1;
        }
    }

    //2、绑定IP地址和端口号
    //2.1 填充地址信息结构体
    struct sockaddr_un sun;
    sun.sun_family = AF_UNIX;
    strcpy(sun.sun_path, "./unix");


    //2.2 绑定
    if(bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) ==-1)
    {
        perror("bind error");
        return -1;
    }
    printf("bind success\n");
    
    //3、数据收发
    char rbuf[128] = "";
    //准备接受对端的地址信息
    struct sockaddr_un cun;
    socklen_t addrlen = sizeof(cun);

    while(1)
    {
        //清空容器
        bzero(rbuf,sizeof(rbuf));

        //读取数据
        recvfrom(sfd, rbuf, sizeof(rbuf), 0, (struct sockaddr*)&cun, &addrlen);
        printf("[%s]:%s\n", cun.sun_path, rbuf);

        //将收到的消息,加个笑脸回过去
        strcat(rbuf, "*_*");
        if(sendto(sfd, rbuf, strlen(rbuf), 0, (struct sockaddr*)&cun, sizeof(cun))==-1)
        {
            perror("write error");
            return -1;
        }
        printf("发送成功\n");

    }
    
    //4、关闭套接字
    close(sfd);

    return 0;
}

        3.2 客户端实现

int main(int argc, const char *argv[])
{
    //1、创建用于通信的套接字文件描述符
    int cfd = socket(AF_UNIX, SOCK_DGRAM, 0);
    if(cfd == -1)
    {
        perror("socket error");
        return -1;
    }

    //2、绑定套接字文件
    //2.1 填充地址信息结构体
    struct sockaddr_un cun;
    cun.sun_family = AF_UNIX;
    strcpy(cun.sun_path, "./linux");

    //判断要绑定的套接字文件是否存在?
    if(access("./linux", F_OK) == 0)
    {
        //说明文件存在,要将其删除
        if(unlink("./linux") ==-1)
        {
            perror("unlink error");
            return -1;
        }
    }


    //2.2 绑定
    if(bind(cfd, (struct sockaddr*)&cun, sizeof(cun)) ==-1)
    {
        perror("bind error");
        return -1;
    }
    printf("bind success\n");
    
    
    //3、数据收发
    char wbuf[128] = "";
    //填充服务器的地址信息结构体
    struct sockaddr_un sun;
    sun.sun_family = AF_UNIX;
    strcpy(sun.sun_path, "./unix");


    char rbuf[128] = "";

    while(1)
    {
        //清空容器
        bzero(wbuf,sizeof(wbuf));
        bzero(rbuf, sizeof(rbuf));
        
        //从终端上获取信息
        fgets(wbuf, sizeof(wbuf), stdin);
        wbuf[strlen(wbuf)-1] = 0;

        //讲消息发送给服务器
        sendto(cfd, wbuf, strlen(wbuf), 0, (struct sockaddr*)&sun, sizeof(sun));
        printf("发送成功\n");

        //接受服务器发来的消息
        recvfrom(cfd, rbuf, sizeof(rbuf), 0,NULL, NULL);
        printf("收到服务器消息为:%s\n", rbuf);
    }
    
    //4、关闭套接字
    close(cfd);

    return 0;
}

举报

相关推荐

算法-栈和队列:匹配括号

0 条评论