zoukankan      html  css  js  c++  java
  • unix域套接字

    与tcp套接字的主要不同

    1.tcp为AF_INET; unix域为AF_LOCAL
    2.tcp需要指定IP和port,连接两端无限制; unix域需要连接两端在同一台主机上,连接两端通过一个socket文件互相通信
    3.tcp结构名称sockaddr_in,unix域结构名称sockaddr_un

    server

    #include <unistd.h>
    #include <string.h>
    #include <errno.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <signal.h>
    #include <sys/wait.h>
    #include <sys/un.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
     
    #define MAXLINE 1024
    #define UN_PATH "/tmp/un_path"
     
    void err_quit(const char *s){
        perror(s);
        exit(1);
    }
     
    void serv_echo(int sockfd){
        int n;
        char mesg[MAXLINE];
     
        for(;;){
            bzero(mesg,sizeof(mesg));
            n=read(sockfd,mesg,MAXLINE);
            if(n == -1){
                if(errno == EINTR)
                    continue;
                else
                    err_quit("recvfrom");
            }
            if(n == 0){
                puts("peer closed");
                return ;
            }
            fputs(mesg,stdout);
            write(sockfd,mesg,n);
        }
    }
     
     
    void sig_chld(int signo){
        while(waitpid(-1,NULL,WNOHANG) > 0)
            ;
    }
     
    int main(int argc,char *argv[]){
        int sockfd,connfd;
        pid_t pid;
        struct sockaddr_un servaddr;
     
        sockfd=socket(AF_LOCAL,SOCK_STREAM,0);
     
        bzero(&servaddr,sizeof(servaddr));
        servaddr.sun_family=AF_LOCAL;
        unlink(UN_PATH);
        strcpy(servaddr.sun_path,UN_PATH);
     
        if(bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)) < 0)
            err_quit("bind");
     
        if(listen(sockfd,SOMAXCONN) < 0)
            err_quit("listen");
     
        signal(SIGCHLD,sig_chld);
     
        while(1){
            connfd=accept(sockfd,NULL,NULL);
            if(connfd == -1){
                if(errno == EINTR)
                    continue;
                else
                    err_quit("accept");
            }
     
            pid=fork();
            if(pid == -1)
                err_quit("fork");
            else if(pid == 0){
                close(sockfd);  
                serv_echo(connfd);
                exit(0);
            }
     
            close(connfd);
        }
        return 0;
    }
    

    client

    #include <unistd.h>
    #include <errno.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <netinet/in.h>
     
    #define MAXLINE 1024
    #define UN_PATH "/tmp/un_path" 
     
    void err_quit(const char *s){
        perror(s);
        exit(1);
    }
     
    void cli_echo(int sockfd){
        int n;
        char sendline[MAXLINE],recvline[MAXLINE];
     
        while(fgets(sendline,MAXLINE,stdin) != NULL){
            bzero(recvline,sizeof(recvline));
            write(sockfd,sendline,strlen(sendline));
            n=read(sockfd,recvline,MAXLINE);
            if(n == -1){
                if(errno == EINTR)
                    continue;
                else
                    err_quit("write");
            }
            if(n == 0){
                puts("peer closed");
                return ;
            }
            recvline[n]=0;
            fputs(recvline,stdout);
        }
    }
     
    int main(int argc,char *argv[]){
        int sockfd;
        struct sockaddr_un servaddr;
     
        bzero(&servaddr,sizeof(servaddr));
        servaddr.sun_family=AF_LOCAL;
        strcpy(servaddr.sun_path,UN_PATH);
     
        sockfd=socket(AF_LOCAL,SOCK_STREAM,0);
        if(sockfd == -1)
            err_quit("socket");
     
        if(connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)) < 0)
                err_quit("connect");
     
        cli_echo(sockfd);
     
        return 0;
    }
    
  • 相关阅读:
    中国区 Azure 服务和定价模式概述
    云计算的那些「What」
    【虚拟机-可用性集】ARM 中可用性集使用的注意事项
    【虚拟机-可用性集】将虚拟机添加到可用性集中
    【虚拟机-远程连接】Azure Linux 虚拟机常见导致无法远程的操作
    【虚拟机-远程链接】Azure Windows 虚拟机常见导致无法远程的操作
    【虚拟机-网络IP】使用 Powershell 设置 VNET 中的静态 IP
    matlab的diff()函数
    matlab的拟合函数polyfit()函数
    解决Python各种no module named "XX"的问题
  • 原文地址:https://www.cnblogs.com/cfans1993/p/6144810.html
Copyright © 2011-2022 走看看