[ECOS] Update on the availability of socketpair()?

Sébastien COURET sebastien.couret@elios-informatique.fr
Thu Jan 22 11:18:00 GMT 2004


Hello,I have ran in the same sort of problem a few time ago.
So I have reused/modified a little the code for socketpair

May be U can use this :

int socketpair(int af,int type,int protocol,int fd[2])
{
 int listen_socket=0;
 struct sockaddr_in sin[2];
 int len=0;
 struct in_addr tmp;

 // The following is only valid if type == SOCK_STREAM
 if (type != SOCK_STREAM)
 {
  diag_printf("Not 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)
 {
  diag_printf("AF:%i TYPE:%i PROTO:%i
:%s",af,type,protocol,strerror(errno));
  return (-1);
 }

 memset(&sin[0], 0, sizeof(struct sockaddr_in));
 sin[0].sin_family = AF_INET;
 sin[0].sin_port = htons(1234); // Use any port number
 // Use looppack to fake pipe
 inet_aton("127.0.0.1",&tmp);
 sin[0].sin_addr.s_addr = tmp.s_addr;
 if (bind(listen_socket,(struct sockaddr *) &sin[0], sizeof(sin[0])) < 0)
 {
  diag_printf("Error while binding on %s
:%s",inet_ntoa(tmp),strerror(errno));
  return (-1);
 }
 len = sizeof(sin[0]);

 // Read the port number we got, so that our client can connect to it
 if (getsockname(listen_socket,(struct sockaddr *) &sin[0], &len) < 0)
 {
  diag_printf("getsockname :%s",strerror(errno));
  return (-1);
 }

 // Put the listen socket in listening mode
 if (listen(listen_socket, 5) < 0)
 {
  diag_printf("listen :%s",strerror(errno));
  return (-1);
 }

 // Create the client socket
 fd[1] = socket(af, type, protocol);
 if (fd[1] < 0)
 {
  diag_printf("creating client_socket :%s",strerror(errno));
  return (-1);
 }

 // Put the client socket in non-blocking connecting mode
 fcntl(fd[1], F_SETFL, fcntl(fd[1], F_GETFL, 0) | O_NONBLOCK);
 if (connect(fd[1],(struct sockaddr *) &sin[0], sizeof(sin[0])) < 0)
 {
  diag_printf("connect :%s",strerror(errno));
  return (-1);
 }

 // At the listen-side, accept the incoming connection we generated
 len = sizeof(sin[1]);
 if ((fd[0] = accept(listen_socket,(struct sockaddr *) &sin[1], &len)) < 0)
 {
  diag_printf("Error on accept :%s",strerror(errno));
  return (-1);
 }

 // Reset the client socket to blocking mode
 fcntl(fd[1], F_SETFL, fcntl(fd[1], F_GETFL, 0) & ~O_NONBLOCK);

 close(listen_socket);

 return (0);
}


Have a nice day.


-- 
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss



More information about the Ecos-discuss mailing list