加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Unix域套接字的初级应用

发布时间:2020-12-15 19:52:17 所属栏目:安全 来源:网络整理
导读:Unix字节流套接字的回显服务: 服务端代码: #include stdio.h #include stdlib.h #include sys/socket.h #include sys/un.h #include unistd.h #include string.h #include errno.h #include sys/wait.h #define UNIX_PATH "/home/marco/sock.str" #define BU

Unix字节流套接字的回显服务:

服务端代码:

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

#define UNIX_PATH "/home/marco/sock.str"
#define BUFSIZE 4096
#define LISTENQ 100
void sig_child(int signo) {
    int ppid;
    while ((ppid = waitpid(-1,NULL,WNOHANG)) > 0) {
        printf("child process: %d terminatedn",ppid);
    }
}
void server_echo(int fd) {
    char send[BUFSIZE];
    int n;
    while ((n = read(fd,send,BUFSIZE)) > 0) {
        if (write(fd,n) != n) {
            printf("write error: %sn",strerror(errno));
            exit(1);
        }
    }
}
int main(int argc,char** argv) {
    int sockfd;
    if ((sockfd = socket(AF_LOCAL,SOCK_STREAM,0)) < 0) {
        printf("socket error: %sn",strerror(errno));
        exit(1);
    }
    unlink(UNIX_PATH);
    struct sockaddr_un serveraddr;
    bzero(&serveraddr,sizeof(struct sockaddr_un));
    serveraddr.sun_family = AF_LOCAL;
    strcpy(serveraddr.sun_path,UNIX_PATH);
    if (bind(sockfd,(struct sockaddr*)&serveraddr,sizeof(struct sockaddr_un)) < 0) {
        printf("bind error: %sn",strerror(errno));
        exit(1);
    }

    if (listen(sockfd,LISTENQ) < 0) {
        printf("lsiten error: %sn",strerror(errno));
        exit(1);
    }

    int connfd;
    int pid;
    if (signal(SIGCHLD,sig_child) == SIG_ERR) {
        printf("signal error: %sn",strerror(errno));
        exit(1);
    }
    for (; ;) {
        if ((connfd = accept(sockfd,NULL)) < 0) {
            if (errno == EINTR) {
                continue;
            }else {
                printf("accept error: %sn",strerror(errno));
                exit(1);
            }
        }
        if ((pid = fork()) < 0) {
            printf("fork error: %sn",strerror(errno));
            exit(1);
        }else if (pid == 0) {
            close(sockfd);
            server_echo(connfd);
            close(connfd);
            exit(0);
        }
        close(connfd);  
    }   
}

客户端代码:

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

#define UNIX_PATH "/home/marco/sock.str"
#define BUFSIZE 4096

void client_echo(int fd) {
    char recv[BUFSIZE];
    int n;
    while ((n = read(STDIN_FILENO,recv,BUFSIZE)) > 0) {
        if (write(fd,n) != n) {
            printf("write error: %sn",strerror(errno));
            exit(1);
        }
        if ((n = read(fd,BUFSIZE)) < 0) {
            printf("read error: %sn",strerror(errno));
            exit(1);
        }

        if (write(STDOUT_FILENO,strerror(errno));
            exit(1);
        }
    }
}
int main(int argc,char** argv) {
    int sockfd;
    if ((sockfd = socket(AF_LOCAL,0)) < 0) {
        printf("socket error: %sn",strerror(errno));
        exit(1);
    }

    struct sockaddr_un serveraddr;
    bzero(&serveraddr,sizeof(struct sockaddr_un));
    serveraddr.sun_family = AF_LOCAL;
    strcpy(serveraddr.sun_path,UNIX_PATH);
    if (connect(sockfd,(struct sockaddr*)&serveraddr,sizeof(struct sockaddr_un)) < 0) {
        printf("connect error: %sn",strerror(errno));
        exit(1);
    }
    client_echo(sockfd);
    return 0;
}

Unix域数据报套接字回显服务:

服务器端代码:

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

#define BUFSIZE 4096
#define UNIX_PATH "/home/marco/tmp.sock"

int main(int argc,SOCK_DGRAM,strerror(errno));
        exit(1);
    }

    unlink(UNIX_PATH);
    struct sockaddr_un serveraddr;
    bzero(&serveraddr,UNIX_PATH);
    if (bind(sockfd,sizeof(struct sockaddr_un)) < 0) {
        printf("bind error: %sn",strerror(errno));
        exit(1);
    }
    struct sockaddr_un clientaddr;
    socklen_t len = sizeof(struct sockaddr_un);
    char recv[BUFSIZE];
    int n;
    for (; ;) {
        if ((n = recvfrom(sockfd,BUFSIZE,0,(struct sockaddr*)&clientaddr,&len)) < 0) {
            printf("receive from client fail: %sn",strerror(errno));
            exit(1);
        }
        if (sendto(sockfd,n,len) != n) {
            printf("send message to client error: %sn",strerror(errno));
            exit(1);
        }
    }
}

客户端代码:

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

#define UNIX_PATH "/home/marco/tmp.sock"
#define BUFSIZE 4096
#define TMP_CLIENT "/home/marco/client.sock"

void client_echo(int fd,struct sockaddr* to,socklen_t tolen) {
    char send[BUFSIZE];
    int n;
    struct sockaddr_un from;
    socklen_t len = sizeof(struct sockaddr_un);
    while ((n = read(STDIN_FILENO,send,BUFSIZE)) > 0) {
        if (sendto(fd,to,tolen) != n) {
            printf("send message to server error: %sn",strerror(errno));
            exit(1);
        }

        if ((n = recvfrom(fd,(struct sockaddr*)&from,&len)) < 0) {
            printf("receive from server fail: %sn",strerror(errno));
            exit(1);
        }
        if (write(STDOUT_FILENO,strerror(errno));
            exit(1);
        }
    }
}

int main() {
    int sockfd;
    if ((sockfd = socket(AF_LOCAL,UNIX_PATH);

    struct sockaddr_un clientaddr;
    bzero(&clientaddr,sizeof(struct sockaddr_un));
    clientaddr.sun_family = AF_LOCAL;
    unlink(TMP_CLIENT);
    strcpy(clientaddr.sun_path,TMP_CLIENT);
    if (bind(sockfd,strerror(errno));
        exit(1);
    }
    client_echo(sockfd,sizeof(serveraddr));
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读