Warning: main(/www/www/htdocs/style/globals.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/s/socketpair.html on line 1

Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/globals.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/s/socketpair.html on line 1

Warning: main(/www/www/htdocs/style/header.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/s/socketpair.html on line 8

Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/header.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/s/socketpair.html on line 8

socketpair()

Create a pair of connected sockets

Synopsis:

#include <sys/types.h>
#include <sys/socket.h>

int socketpair( int domain, 
                int type, 
                int protocol, 
                int * fd[2] );

Arguments:

domain
The communications domain where the sockets are to be created.
type
The type of sockets to create.
protocol
The protocol to use with the sockets. A protocol of 0 causes socketpair() to use an unspecified default protocol appropriate for the requested socket type.
fd[2]
The 2-digit integer array where the file descriptors of the created socket pair are to be held.

Library:

libsocket

Use the -l socket option to qcc to link against this library.

Description:

The socketpair() call creates an unnamed pair of connected sockets in the specified domain, of the specified type, using the optionally specified protocol argument. The file descriptors are returned in the vector fd and are identical.

Valid types are described in socket().

If the protocol argument is nonzero, it must be a protocol that's understood by the address family. No such protocols are defined at this time.

Returns:

0
Success.
-1
An error occurred (errno is set).

Errors:

EAFNOSUPPORT
The specified address family isn't supported on this machine.
EFAULT
The address sv doesn't specify a valid part of the process address space.
EMFILE
Too many descriptors are in use by this process.
EOPNOTSUPP
The specified protocol doesn't support creation of socket pairs.
EPROTONOSUPPORT
The specified protocol isn't supported on this machine.

Examples:

#include <stdio.h>
#include <sys/socket.h>

#define CHAR_BUFSIZE 20
int main(int argc, char **argv) {
int fd[2], len;
char message[CHAR_BUFSIZE];

if(socketpair(AF_LOCAL, SOCK_STREAM, 0, fd) == -1) {
return 1;
}

/* If you write to fd[0], you read from fd[1], and vice versa. */

/* Print a message into one end of the socket */
snprintf(message, CHAR_BUFSIZE, "First message");
write(fd[0], message, strlen(message) + 1);

/* Print a message into the other end of the socket */
snprintf(message, CHAR_BUFSIZE, "Second message");
write(fd[1], message, strlen(message) + 1);

/* Read back the data written to the first socket */
len = read(fd[0], message, CHAR_BUFSIZE-1);
message[len] = '\0';
printf("Read [%s] from first fd \n", message); 

/* Read back the data written to the second socket */
len = read(fd[1], message, CHAR_BUFSIZE-1);
message[len] = '\0';
printf("Read [%s] from second fd \n", message); 

close(fd[0]);
close(fd[1]);

return 0;
} 

Classification:

POSIX 1003.1

Safety:
Cancellation point Yes
Interrupt handler No
Signal handler No
Thread Yes

See also:

read(), socket(), write()


Warning: main(/www/www/htdocs/style/footer.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/s/socketpair.html on line 223

Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/footer.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/s/socketpair.html on line 223