zoukankan      html  css  js  c++  java
  • unix domain socket的用法

    服务器程序

    #include <stdio.h>
    #include <sys/un.h>
    #include <sys/socket.h>
    #include <errno.h>
    #include <unistd.h>
    
    #define SV_SOCK_PATH "/tmp/us_xfr"
    #define BUF_SIZE 100
    #define BACKLOG 5
    
    int main() {
        struct sockaddr_un addr;
        int sfd, cfd;
        ssize_t numRead;
        char buf[BUF_SIZE];
    
        sfd = socket(AF_UNIX, SOCK_STREAM, 0);
        if(sfd == -1) {
            fprintf(stderr, "socket error");
            return -1;
        }
        if(remove(SV_SOCK_PATH) == -1 && errno != ENOENT) {
            fprintf(stderr, "remove /tmp/us_xfr");
            return -1;
        }
        memset(&addr, 0, sizeof(struct sockaddr_un));
        addr.sun_family = AF_UNIX;
        strncpy(addr.sun_path, SV_SOCK_PATH, sizeof(addr.sun_path)-1);
        if(bind(sfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)) == -1) {
            fprintf(stderr, "bind error.");
            return -1;
        }
    
        if(listen(sfd, BACKLOG) == -1) {
            fprintf(stderr, "listen error");
            return -1;
        }
    
        for(;;) {
            cfd = accept(sfd, NULL, NULL);
            if(cfd == -1) {
                fprintf(stderr, "accept error.");
                return -1;
            }
    
            while((numRead = read(cfd, buf, BUF_SIZE)) > 0) {
                if(write(STDOUT_FILENO, buf, numRead) != numRead) {
                    fprintf(stderr, "write error.");
                    return -1;
                }
            }
            if(numRead == -1) {
                fprintf(stderr, "read error.");
                return -1;
            }
            if(close(cfd) == -1) {
                fprintf(stderr, "close error.");
                return -1;
            }
        }
    
        return 0;
    }

    客户端代码

    #include <stdio.h>
    #include <unistd.h>
    #include <sys/un.h>
    #include <sys/socket.h>
    
    #define SV_SOCK_PATH "/tmp/us_xfr"
    #define BUF_SIZE 100
    
    int main() {
        struct sockaddr_un addr;
        int sfd;
        ssize_t numRead;
        char buf[BUF_SIZE];
    
        sfd = socket(AF_UNIX, SOCK_STREAM, 0);
        if(sfd == -1) {
            fprintf(stderr, "socket error");
            return -1;
        }
    
        memset(&addr, 0, sizeof(struct sockaddr_un));
        addr.sun_family = AF_UNIX;
        strncpy(addr.sun_path, SV_SOCK_PATH, sizeof(addr.sun_path)-1);
    
        if(connect(sfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) {
            fprintf(stderr, "connect error.");
            return -1;
        }
    
        while((numRead=read(STDIN_FILENO, buf, BUF_SIZE)) > 0) {
            if(write(sfd, buf, numRead) != numRead) {
                fprintf(stderr, "partial/failed write");
                return -1;
            }
        }
    
        if(numRead == -1) {
            fprintf(stderr, "read error");
            return -1;
        }
        return 0;
    }
  • 相关阅读:
    获取本地计算机的主机信息
    用函数SendARP()获取局域网计算机的MAC地址
    获取本地计算机的操作系统
    获取本地计算机名和IP地址
    基于TCP(面向连接)的Socket编程
    SMTP协议
    使用截获消息响应截断某些消息
    《Windows核心编程》读书笔记 上
    vc++高级班之多线程篇[7]---线程间的同步机制②
    vc++高级班之多线程篇[6]---线程间的同步机制①
  • 原文地址:https://www.cnblogs.com/donggongdechen/p/15035032.html
Copyright © 2011-2022 走看看