socketpair函数

来源:互联网 发布:fx3u 48m编程手册中文 编辑:程序博客网 时间:2024/05/21 02:21

本文转自:http://blog.csdn.net/xjtuse_mal/archive/2007/06/27/1668599.aspx

一.概念及用途

一个问题:如何创建全双工管道?
直接的办法当然是pipe两次,创建两组管道,但是有没有更简单的呢?
socketpair就可以了,man socketpair:
socketpair - create a pair of connected sockets, The two sockets are indistinguishable,也就是说,用socketpair创建出来的两个描述符应该是等价的。 二.用法
#define DATA1 "test string 1"
#define DATA2 "test string 2"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>main()
{
int sockets[2], child;
char buf[1024];
/* Get the socket pair */
if (socketpair(AF_UNIX, SOCK_STREAM,
        0, sockets) < 0) {
    printf("error %d on socketpair ", errno);
    exit(1);
}
/* create child process */
if ((child = fork()) == -1) {
    printf("fork error %d ", errno);
    exit(1);
}
if (child != 0) { /* this is the parent */
    /* close child's end of socket */
    close(sockets[0]);
    /* read message from child */
    if (read(sockets[1], buf, sizeof(buf)) < 0) {
      printf("error %d reading socket ", errno);
      exit(1);
    }
    printf("parent:-->%s \n", buf);
    /* write message to child */
    if (write(sockets[1], DATA1, sizeof(DATA1)) < 0) {
      printf("error %d writing socket ", errno);
      exit(1);
    }
    /* finished */
    close(sockets[1]);
} else { /* the child */
    /* close parent's end of socket */
    close(sockets[1]);
    /* send message to parent */
    if (write(sockets[0], DATA2, sizeof(DATA1)) < 0) {
      printf("error %d writing socket ", errno);
      exit(1);
    }
    /* get message from parent */
    if (read(sockets[0], buf, sizeof(buf)) < 0) {
      printf("error %d reading socket ", errno);
      exit(1);
    }
    printf("child: -->%s\n ", buf);
    /* finished */
    close(sockets[0]);
}
}
从上面的代码中,我们可以发现,父进程从sockets[1]中读写,子进程从sockets[0]中读写,的确是全双工形态。
唯一值得考虑的是为什么在父子进程中,要分别关闭sockets[0]和sockets[1]呢? 三.一个实现
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <stdio.h>socketpair(af, type, protocol, fd)
int af;
int type;
int protocol;
int fd[2];
{
int listen_socket;
struct sockaddr_in sin[2];
int len;/* The following is only valid if type == SOCK_STREAM */
if (type != SOCK_STREAM)
   return -1;/* Create a temporary listen socket; temporary, so any port is good */
listen_socket = socket(af, type, protocol);
if (listen_socket < 0)
   {
   perror("creating listen_socket");
   return -1;
   }
sin[0].sin_family = af;
sin[0].sin_port = 0; /* Use any port number */
sin[0].sin_addr.s_addr = inet_makeaddr(INADDR_ANY, 0);
if (bind(listen_socket, &sin[0], sizeof(sin[0])) < 0)
   {
   perror("bind");
   return -1;
   }
len = sizeof(sin[0]);/* Read the port number we got, so that our client can connect to it */
if (getsockname(listen_socket, &sin[0], &len) < 0)
   {
   perror("getsockname");
   return -1;
   }/* Put the listen socket in listening mode */
if (listen(listen_socket, 5) < 0)
   {
   perror("listen");
   return -1;
   }/* Create the client socket */
fd[1] = socket(af, type, protocol);
if (fd[1] < 0)
   {
   perror("creating client_socket");
   return -1;
   }/* Put the client socket in non-blocking connecting mode */
fcntl(fd[1], F_SETFL, fcntl(fd[1], F_GETFL, 0) | O_NDELAY);
if (connect(fd[1], &sin[0], sizeof(sin[0])) < 0)
   {
   perror("connect");
   return -1;
   }/* At the listen-side, accept the incoming connection we generated */
len = sizeof(sin[1]);
if ((fd[0] = accept(listen_socket, &sin[1], &len)) < 0)
   {
   perror("accept");
   return -1;
   }/* Reset the client socket to blocking mode */
fcntl(fd[1], F_SETFL, fcntl(fd[1], F_GETFL, 0) & ~O_NDELAY);close(listen_socket);return 0;
}

采 用的是unix domain socket的技术,首先创建一个监听socket,因为是临时性的,其绑定的端口和Ip地址都可以任意(由系统指定即可), 然后执行listen;接 着创建client socket,其描述符为fd[1],执行connect;最后返回服务端,执行accept,返回的描述符就是实际通信的描述符fd[0]。 四.补充
socketpair还常用于描述符传递的处理中。

五.创建套接口 socketpair()
例1.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
int main(int argc,char **argv)
{
int z;
int s[2];
z=socketpair(AF_LOCAL,SOCK_STREAM,0,s);
if(z==-1)
   {
    fprintf(stderr,"%s:socketpair(AF_LOCAL,SOCK_STREAM,0)\n",strerror(errno));
    return 1;
   }
   printf("s[0]=%d;\n",s[0]);
   printf("s[1]=%d;\n",s[1]);
   system("nslookup --unix -p");
   return 0;
}
例2.
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
int main(int argc,char**argv)
{
int z;
int s[2];
char *cp;
char buf[80];
   z=socketpair(AF_LOCAL,SOCK_STREAM,0,s);
   if(z==-1)
    { 
fprintf(stderr,"%s:socketpair(AF_LOCAL,SOCK_STREAM,""0)\n",strerror(errno));
   return 1;
    }
z=write(s[1],cp="Hello?",6);
     if(z<0)
    {
fprintf(stderr,"%s:write(%d,\"%s\",%d)\n",strerror(errno),s[1],cp,strlen(cp));
    return 2;
    }
   printf("Wrote message'%s'on s[1]\n",cp);
   z=read(s[0],buf,sizeof buf);
   if(z<0)
    {
     fprintf(stderr,"%s:read(%d,buf,%d)\n",strerror(errno),s[0],sizeof buf);
    return 3;
    }
   buf[z]=0;
   printf("Receive message '%s'from socket s[0]\n",buf);
   z=write(s[0],cp="Go away!",8);
   if(z<0)
     {
fprintf(stderr,"%s:write(%d,\"%s\",%d)\n",strerror(errno),s[0],cp,strlen(cp));
    return 4;
     }
     printf("Wrote message '%s' on s[0]\n",cp);
z=read(s[1],buf,sizeof buf);
    if(z<0)
    {
     fprintf(stderr,"%s:read(%d,buf,%d)\n",strerror(errno),s[1],sizeof buf);
   return 3;
    }
   buf[z]=0;
    printf("Receive message '%s' from socket s[1]\n",buf);
   close(s[0]);
   close(s[1]);
   puts("Done.");
return 0;
}
原创粉丝点击