一个Linux AF_UNIX socket通信的简单例子

服务端(接受内容):

#include "stdio.h"
#include "stdlib.h"
#include "drawFbStruct.h"
#include 
#include 
#include 

#define new(Class) (Class*)malloc(sizeof(Class))


#define UNIX_DOMAIN "/tmp/UNIX.domain"
#define READ_FROM_CLIENT 0X01
#define WRITE_TO_CLIENT 0x02
#define SCR_WIDTH 3840
#define SCR_HEIGHT 2160


int initSocketServer() {
    /*int serverFd = -1;
    struct sockaddr_un serv_addr;
    struct sockaddr_un client_addr;
    int client_addr_len = sizeof(struct sockaddr_un);
    memset(&client_addr, 0x00, sizeof(client_addr));
    serverFd = socket(AF_UNIX,SOCK_SEQPACKET,0);
    if(serverFd < 0) {
        return -1;
    }
    memset(&serv_addr,0x00,sizeof(serv_addr));*/

    socklen_t clt_addr_len;
    int listen_fd;
    int com_fd;
    int ret;
    static char data_buf[1 + 4 + SCR_WIDTH * SCR_HEIGHT * sizeof(int)];
    int len;
    struct sockaddr_un clt_addr;
    struct sockaddr_un srv_addr;


    listen_fd = socket(PF_UNIX, SOCK_STREAM, 0);
    if(listen_fd < 0) {
        perror("cannot create communication socket");
        return 1;
    }
    //set server addr_param
    srv_addr.sun_family = AF_UNIX;
    strcpy(srv_addr.sun_path,UNIX_DOMAIN);
    unlink(UNIX_DOMAIN);
    //bind sockfd & addr
    ret = bind(listen_fd, (struct sockaddr*)&srv_addr, sizeof(srv_addr));
    if(ret == -1) {
        perror("cannot bind server socket");
        close(listen_fd);
        unlink(UNIX_DOMAIN);
        return 1;
    }
    //listen sockfd
    ret = listen(listen_fd,1);
    if(ret == -1) {
        perror("cannot listen the client connect request");
        close(listen_fd);
        unlink(UNIX_DOMAIN);
        return 1;
    }
    //have connect request use accept
    len = sizeof(clt_addr);
    //accept the client
    com_fd = accept(listen_fd,(struct sockaddr*)&clt_addr,&len);
    if(com_fd < 0) {
        perror("cannot accept client connect request");
        close(listen_fd);
        unlink(UNIX_DOMAIN);
        return 1;
    }
    while(1) {

        //read and printf sent client info
        printf("\nReceive from client:\n");
        //clear the buffer

        //read data from socket to the buffer
        int recvBytes = read(com_fd, data_buf, sizeof(data_buf)); //read data to buffer
        if(recvBytes == 0){
            break;
        }

        //printf("%d Request is %X\n", data_buf[0]);
        printf("the msg is : %s\nrecvBytes:%d, com_fd:%d\n", data_buf, recvBytes, com_fd);

        //clear the buffer
        //memset(data_buf, 0, SCR_WIDTH * SCR_HEIGHT * sizeof(int));

    }
    //close the socket connection
    close(com_fd);
}





int main(){
    //open the socket listener
    initSocketServer();
    return 0;
}

客户端(发送内容):

#include 
#include 
#include 
#include 
 
#define UNIX_DOMAIN "/tmp/UNIX.domain"
 
int main(void)
{
    int connect_fd;
    int ret;
    char snd_buf[1024];
    char fuckStr[] = "shit every day";
    static struct sockaddr_un srv_addr;

    //create unix socket
    connect_fd = socket(PF_UNIX, SOCK_STREAM, 0);
    if(connect_fd < 0) {
        perror("cannot create communication socket");
        return 1;
    }
    srv_addr.sun_family = AF_UNIX;
    strcpy(srv_addr.sun_path,UNIX_DOMAIN);
    //connect server
    ret = connect(connect_fd, (struct sockaddr*)&srv_addr, sizeof(srv_addr));
    if(ret == -1) {
        perror("cannot connect to the server");    
        close(connect_fd);
        return 1;
    }
    while(1){
        memset(snd_buf, 0, 1024);


        {
            struct timeval stamp;
            gettimeofday(&stamp, NULL);
            sprintf(snd_buf, "%d", stamp.tv_sec * 1000 + stamp.tv_usec);          
        }

    
        //send buffer
        write(connect_fd, snd_buf, sizeof(snd_buf));
    }
    
    close(connect_fd);
    return 0;
}

效果(显示时间戳):

一个Linux AF_UNIX socket通信的简单例子_第1张图片

你可能感兴趣的:(C语言)