domain socket

client

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <errno.h>
#include <stddef.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#define BUFFER_SIZE 1024

const char *filename = "uds-tmp";

void *thread_recv(void *ptr)
{
    int sock_fd = *(int *)ptr;
    char buffer[128] = {0};
    while (1)
    {
        int ret = recv(sock_fd, buffer, 128, 0);
        if (ret <= 0)
        {
            printf("recv error: %s\n", strerror(ret));
            close(sock_fd);
            //exit(EXIT_FAILURE);
        } else {
            printf("%s\n", buffer);
        }
    }
}

int main()
{
    while (1)
    {
        struct sockaddr_un un;
        int sock_fd;
        char buffer[BUFFER_SIZE] = "hello world";
        un.sun_family = AF_UNIX;
        strcpy(un.sun_path, filename);
        sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
        if (sock_fd < 0)
        {
            printf("Request socket failed\n");
            return -1;
        }
        if (connect(sock_fd, (struct sockaddr *)&un, sizeof(un)) < 0)
        {
            printf("connect socket failed\n");
            return -1;
        }

        pthread_t pth;

        pthread_create(&pth, NULL, thread_recv, (void *)(&sock_fd));

        while (1)
        {
            char log_content[100];
	    int log_typ, log_v, log_num;
	    memset(buffer, 0, BUFFER_SIZE);
           
	    printf("input parameters,the format is:p(priority)r(rate,unit:s)n(number of logs)\n");
            fgets(buffer, BUFFER_SIZE, stdin);
            int sscanf_re = sscanf(buffer,"p%dr%dn%d", &log_typ, &log_v, &log_num);
	    switch(log_typ)
	    {
                case 1:strcpy(log_content, "This is first");
		       break;
		case 2:strcpy(log_content, "This is second");
		       break;
		case 3:strcpy(log_content, "This is third");
		       break;
	    }

            printf("send data: %s\n",log_content);
            while(log_num)
            {
            int error = 0;
            socklen_t len = sizeof(error);
            int retval = getsockopt(sock_fd, SOL_SOCKET, SO_ERROR, &error, &len);

            if (retval != 0)
            {
                /* there was a problem getting the error code */
                fprintf(stderr, "error getting socket error code: %s\n", strerror(retval));
                return 0;
            }

            if (error != 0)
            {
                /* socket has a non zero error status */
                fprintf(stderr, "socket error: %s\n", strerror(error));
                return 0;
            }

            ssize_t size = send(sock_fd, log_content, BUFFER_SIZE, MSG_NOSIGNAL);

            if (size <= 0)
            {
                // close(sock_fd);
                // return 0;
                printf("send data failed.\n");
            }
	    log_num -= 1;
	    sleep(log_v);
	    }
        }
    }

    return 0;
}
server
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <errno.h>
#include <stddef.h>
#include <unistd.h>
#include <pthread.h>

#define MAX_CONNECT_NUM 2
#define BUFFER_SIZE 1024
const char *filename = "uds-tmp";
static pthread_t th;
static pthread_t th1;

void *thread_recv(void *ptr)
{
    int fd = *(int *)ptr;
    char buffer[BUFFER_SIZE];
    bzero(buffer, BUFFER_SIZE);

    while (1)
    {
        printf("waiting recv...\n");
        int ret = recv(fd, buffer, BUFFER_SIZE, 0);

        if (ret <= 0)
        {
            printf("recv failed\n");
            close(fd);
            pthread_cancel(th1);
            return NULL;
        }

        printf("%s\n", buffer);

        if (strncmp("end", buffer, 3) == 0)
        {
            close(fd);
            exit(0);
        }
    }
}

void *thread_send(void *ptr)
{
    int fd = *(int *)ptr;
    while(1)
    {
        char buff[128] = {0};
        fgets(buff, 128, stdin);

        int ret = send(fd, buff, 128, 0);
        if (ret <= 0)
        {
            close(fd);
            pthread_cancel(th);
            return NULL;
        }
    }
}

int main()
{
    int fd, new_fd, len, i;
    struct sockaddr_un un;
    fd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (fd < 0)
    {
        printf("Request socket failed!\n");
        return -1;
    }
    un.sun_family = AF_UNIX;
    unlink(filename);
    strcpy(un.sun_path, filename);
    if (bind(fd, (struct sockaddr *)&un, sizeof(un)) < 0)
    {
        printf("bind failed!\n");
        return -1;
    }
    if (listen(fd, MAX_CONNECT_NUM) < 0)
    {
        printf("listen failed!\n");
        return -1;
    }
    while (1)
    {
        printf("wait to accept...\n");

        new_fd = accept(fd, NULL, NULL);

        printf("new accept.\n");

        if (new_fd < 0)
        {
            printf("accept failed\n");
            return -1;
        }
        pthread_create(&th, NULL, thread_recv, (void *)(&new_fd));
        pthread_create(&th1, NULL, thread_send, (void *)(&new_fd));

    }
    close(fd);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值