unix domain socket dgram

大体上参考这里,http://philherlihy.com/unix-domain-sockets-datagram/

改动之后如下

服务端

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>

char * server_filename = "/tmp/socket-server";

int main(void)
{
    int s = socket(AF_UNIX, SOCK_DGRAM, 0);
    if (s == -1) {
        perror("socket server");
        exit(1);
    }
    
    struct sockaddr_un srv_un = {0};
    srv_un.sun_family = AF_UNIX;
    strncpy(srv_un.sun_path, server_filename, sizeof(srv_un.sun_path));

    unlink(server_filename);
    if (bind(s, (struct sockaddr *)&srv_un, sizeof(srv_un)) == -1) {
        perror("bind server");
        exit(1);
    }

    for(;;) {
        struct sockaddr_un cli_addr = {0};
        socklen_t cli_addr_len = sizeof(cli_addr);
        
        int msg;

        int recv_size = recvfrom(s, &msg, sizeof(msg), 0, (struct sockaddr*)&cli_addr, &cli_addr_len);
        if (recv_size < 0) {
            perror("recvfrom");
            break;
        }       
        printf("recv %d\n", msg);
        
        msg *= 2;
        int sendto_size = sendto(s, &msg, sizeof(msg), 0, (struct sockaddr*)&cli_addr, sizeof(cli_addr));
        if (sendto_size < 0) {
            perror("sendto");
            return -1;
        }       
        printf("send %d\n", msg); 
    }

    close(s);
    unlink(server_filename);

    return 0;
}

客户端

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>

char * server_filename = "/tmp/socket-server";
char * client_filename = "/tmp/socket-client";

int main(void)
{
    int s = socket(AF_UNIX, SOCK_DGRAM, 0);
    if (s == -1) {
        perror("socket server");
        exit(1);
    }
    
    {
        struct sockaddr_un cli_un = { 0 };
        cli_un.sun_family = AF_UNIX;
        strncpy(cli_un.sun_path, client_filename, sizeof(cli_un.sun_path));
        
        unlink(client_filename);
        if (bind(s, (struct sockaddr *)&cli_un, sizeof(cli_un)) == -1) {
            perror("bind client");
            exit(1);
        }
    }
    
    {
        struct sockaddr_un srv_un = {0};
        srv_un.sun_family = AF_UNIX;
        strncpy(srv_un.sun_path, server_filename, sizeof(srv_un.sun_path));

        int msg = 1006;
        int sendto_size = sendto(s, &msg, sizeof(msg), 0, (struct sockaddr*)&srv_un, sizeof(srv_un));
        if (sendto_size < 0) {
            perror("sendto");
            return -1;
        }   
        printf("send %d\n", msg);
    }
    
    {
        struct sockaddr_un cli_un = { 0 };
        socklen_t cli_len = sizeof(cli_un);
        
        int msg;
        int recv_size = recvfrom(s, &msg, sizeof(msg), 0, (struct sockaddr*)&cli_un, &cli_len);
        if (recv_size < 0) {
            perror("recvfrom");
            return -1;
        }   
        printf("recv %d\n", msg); 
    }
    
    close(s);
    unlink(client_filename);
    
    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *