linux c例子 - 网络编程(3) - unix domain - 流

服务端

#include <stdio.h>          // printf perror
#include <sys/socket.h>     // socket 

#include <unistd.h>

#include <sys/un.h>
#include <arpa/inet.h>

#define UD_PATH_SERVER "/tmp/unix_domain_ser"

static void help()
{
    printf("./a.out port\n");
}

static void print_sockaddr(struct sockaddr_un *addr)
{
    
    printf("from %s - ", addr->sun_path);
    
}

int main(int argc, char **argv)
{
    int                 n, rc, fd, afd, flag;
    unsigned short      port;
    char                buf[16];
    socklen_t           slen;
    
    struct sockaddr_un  ser_addr, cli_addr;
    
    fd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (fd == -1) {
        perror("socket");
        return -1;
    }
    
    memset(&ser_addr, 0, sizeof(struct sockaddr_un));
    ser_addr.sun_family = AF_UNIX;
    memcpy(ser_addr.sun_path, UD_PATH_SERVER, sizeof(UD_PATH_SERVER) - 1);
    
    unlink(UD_PATH_SERVER);
    
    rc = bind(fd, (struct sockaddr *)&ser_addr, sizeof(ser_addr));
    if (rc == -1) {
        perror("bind");
        return -1;
    }
    
    rc = listen(fd, 1024);
    if (rc == -1) {
        perror("listen");
        return -1;
    }
    
    slen = sizeof(cli_addr);
    
    while (1) {
    
        afd = accept(fd, (struct sockaddr *)&cli_addr, &slen);
        if (afd == -1) {
            perror("socket");
            return -1;
        }
        
        print_sockaddr(&cli_addr);
        
        n = recv(afd, buf, 15, 0);
        if (n <= 0) {
            perror("recv");
            close(afd);
            continue;
        }
        
        buf[n] = 0;
        
        printf("recv: %s\n", buf);
        
        n = send(afd, "got", 3, 0);
        if (n <= 0) {
            perror("send");
            close(afd);
            continue;
        }
        
        close(afd);
        
    
    }
    
}


客户端

#include <stdio.h>          // printf perror
#include <sys/socket.h>     // socket 

#include <sys/un.h>
#include <arpa/inet.h>

#define UD_PATH_SERVER "/tmp/unix_domain_ser"

#define UD_PATH_SERVER_CLIENT "/tmp/unix_domain_cli"

static void help()
{
    printf("./a.out ip port\n");
}

int main(int argc, char **argv)
{
    int             fd, n;
    in_port_t       port;
    char            *ip;
    char            buf[16];
    
    struct sockaddr_un      ser_addr, cli_addr;
    
    
    fd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (fd == -1) {
        perror("socket");
        return -1;
    }
    
    unlink(UD_PATH_SERVER_CLIENT);
    
    memset(&cli_addr, 0, sizeof(struct sockaddr_un));
    cli_addr.sun_family = AF_UNIX;
    memcpy(cli_addr.sun_path, UD_PATH_SERVER_CLIENT, sizeof(UD_PATH_SERVER) - 1);
    
    if (bind(fd, (struct sockaddr *)&cli_addr, sizeof(cli_addr)) == -1) {
        perror("bind");
        return -1;
    }
    
    memset(&ser_addr, 0, sizeof(struct sockaddr_un));
    ser_addr.sun_family = AF_UNIX;
    memcpy(ser_addr.sun_path, UD_PATH_SERVER, sizeof(UD_PATH_SERVER) - 1);
    
    if (connect(fd, (struct sockaddr *)&ser_addr, sizeof(ser_addr)) == -1) {
        perror("connect");
        return -1;
    }
    
    n = send(fd, "hello", 5, 0);
    if (n <= 0) {
        perror("send");
        return -1;
    }
    
    n = recv(fd, buf, 15, 0);
    if (n <= 0) {
        perror("recv");
        return -1;
    }
    
    buf[n] = 0;
    
    printf("recv: %s\n", buf);
    
    close(fd);
    
    return 0;
}



上一篇: linux c例子 - 网络编程(2) - udp
下一篇: linux c例子 - 网络编程(3) - unix domain - 数据报
作者邮箱: 203328517@qq.com