linux網絡編程--UNIX域套接字

UNIX域套接字服務器

  socket一樣能夠用於本地通訊socket

  建立套接字時使用本地協議PF_UNIX(或PF_LOCAL)PF_LOCALtcp

  分爲流式套接字和用戶數據報套接字ide

  和其餘進程間通訊方式相比使用方便。效率更高ui

  用於先後臺進程通訊spa

 

本地地址結構:3d

 struct sockaddr_uncode

{server

  sa_family_t sun_family;blog

  char sun_path[108];

};

填充地址結構

   struct sockaddr_un myaddr;

  bzero(&myaddr,sizeof(myaddr));

  myaddr.sun_family=PF_UNIX;

  strcpy(myaddr.sun_path,"mysocket");

UNIX域流式套接字

服務器端:

  socket(PF_UNIX,SOCK_STREAM,0);

  bind(本地地址);

  listen()

  aceept()

  recv/send

客戶端

  socket(PF_UNIX,SOCK_STREAM,0);

  bind(本地地址);

  connect();

  recv/send();

 

server.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/un.h>

#define  err_log(errlog)  do{perror(errlog); exit(1);}while(0)
#define  N  128

int main(int argc, const char *argv[])
{
    int sockfd;
    int confd;
    struct sockaddr_un  serveraddr, clientaddr;
    char buf[N] = {};


    if((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
    {
        err_log("fail to socket");
    }

    printf("sockfd = %d\n", sockfd);

    serveraddr.sun_family = AF_UNIX;
    strcpy(serveraddr.sun_path, "mysocket");

    if(bind(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0)
    {
        err_log("fail to bind");
    }

    if(listen(sockfd, 10) < 0)
    {
        err_log("fail to listen");
    }

    socklen_t addrlen = sizeof(struct sockaddr);
    if((confd = accept(sockfd, (struct sockaddr *)&clientaddr, &addrlen)) < 0)
    {
        err_log("fail to accept");
    }
    
    while(1)
    {

        if(recv(confd, buf, N, 0) < 0)
        {
            err_log("fail to recv");
        }
        printf("From client:%s\n", buf);

        strcpy(buf, " Message from server");

        if(send(confd, buf, N, 0) < 0)
        {
            err_log("fail to send");
        }
    }

    close(sockfd);

    return 0;
}

client.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/un.h>

#define  err_log(errlog)  do{perror(errlog); exit(1);}while(0)
#define  N  128

// ./client  192.168.0.196  10000

int main(int argc, const char *argv[])
{
    int sockfd;
    int confd;
    struct sockaddr_un  serveraddr, clientaddr;
    char buf[N] = {0};

    if((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
    {
        err_log("fail to socket");
    }

    printf("sockfd = %d\n", sockfd);

    serveraddr.sun_family = AF_UNIX;
    strcpy(serveraddr.sun_path, "mysocket");

    if(connect(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0)
    {
        err_log("fail to connect");
    }

    while(1)
    {
        printf("Input >");
        fgets(buf, N, stdin);
        buf[strlen(buf)-1] = '\0';

        send(sockfd, buf, N, 0);
        
    }


    close(sockfd);
    

    return 0;
}

其實這個與前面的tcp模型相似

重點關注UNIX域用戶數據報套接字

UNIX域用戶數據報套接字:

服務器端:

  socket(PF_UNIX,SOCK_DGRAM,0);

  bind(本地地址);

  recvfrom

  sendto

客戶端:

 socket(PF_UNIX,SOCK_DGRAM,0);

  bind(本地地址);

  須要設置兩個地址以下:

  serveraddr.sun_family = AF_UNIX;
  strcpy(serveraddr.sun_path, "mysocket");

  clientaddr.sun_family = AF_UNIX;
  strcpy(clientaddr.sun_path, "socket");

  if(bind(sockfd, (struct sockaddr*)&clientaddr, sizeof(clientaddr)) < 0)
  {
  err_log("fail to bind");
  }

  sendto()

  recvfrom();

實例以下:

server.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/un.h>

#define  err_log(errlog)  do{perror(errlog); exit(1);}while(0)
#define   N  128

int main(int argc, const char *argv[])
{
    int sockfd;
    struct sockaddr_un serveraddr;
    struct sockaddr_un clientaddr;
    socklen_t addrlen = sizeof(clientaddr);
    char buf[N] = {};

    if((sockfd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0)
    {
        err_log("fail to socket");
    }

    serveraddr.sun_family = AF_UNIX;
    strcpy(serveraddr.sun_path, "mysocket");

    if(bind(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0)
    {
        err_log("fail to bind");
    }


    while(1)
    {
        if(recvfrom(sockfd, buf, N, 0, (struct sockaddr*)&clientaddr, &addrlen) < 0)
        {
            err_log("fail to recvfrom");
        }

        printf("From clientaddr:%s --> %s\n", buf, clientaddr.sun_path);
        if(strncmp(buf, "quit", 4) == 0)
        {
            break;
        }

        strcpy(buf, "Message from server...");

        if(sendto(sockfd, buf, N, 0, (struct sockaddr*)&clientaddr, sizeof(clientaddr)) < 0)
        {
            err_log("fail to sendto");
        }
    }

    close(sockfd);


    
    return 0;
}

client.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/un.h>

#define  err_log(errlog)  do{perror(errlog); exit(1);}while(0)
#define   N  128

// ./send 192.168.0.255 10000
int main(int argc, const char *argv[])
{
    int sockfd;
    struct sockaddr_un serveraddr;
    struct sockaddr_un clientaddr;
    socklen_t addrlen = sizeof(clientaddr);
    char buf[N] = {};


    if((sockfd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0)
    {
        err_log("fail to socket");
    }

    serveraddr.sun_family = AF_UNIX;
    strcpy(serveraddr.sun_path, "mysocket");

    clientaddr.sun_family = AF_UNIX;
    strcpy(clientaddr.sun_path, "socket");

    if(bind(sockfd, (struct sockaddr*)&clientaddr, sizeof(clientaddr)) < 0)
    {
        err_log("fail to bind");
    }

    while(1)
    {
        printf("Input > ");
        fgets(buf, N, stdin);
        buf[strlen(buf)-1] = '\0';

        if(sendto(sockfd, buf, N, 0, (struct sockaddr *)&serveraddr, addrlen) < 0)
        {
            err_log("fail to sendto");
        }

        if(strncmp(buf, "quit", 4) == 0)
        {
            break;
        }

        if(recvfrom(sockfd, buf, N, 0, (struct sockaddr *)&serveraddr, &addrlen) < 0)
        {
            err_log("fail to recvfrom");
        }

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

    }

    close(sockfd);

    return 0;
}
View Code
相關文章
相關標籤/搜索