The functions of socketpair are as follows:
# Include <sys/types. h>
# Include <sys/socket. h>
Int socketpair (int domain, int type, int protocol, int sv [2]);
Sys/types. H file must be used to define some C macro constants. The sys/socket. H file must contain the etpair function prototype.
The socketpair function requires four workers. They are:
Set interface domain
Set interface type
Protocols used
Pointer to the storage file descriptor
The number of types of APIs declares the type of APIs we want to create. Select the socketpair function as follows:
SOCK_STREAM
SOCK_DGRAM
For the socketpair function, the number of protocol partitions must be 0.
The forward number sv [2] is an integer array that receives an array representing two sets of interfaces. Each file descriptor represents a set of interfaces, and there is no difference between them.
If the function is successful, 0 is returned. Otherwise, the system will return-1 indicating that the creation failed, and errno indicates the specific error number.
About the process. The socketpair () function creates two processes. After fork (), both processes will run the code in the main program. Pay attention to this! Especially for bind, if bind is used twice, an error will occur. Normally, a function with an endless loop is called in the sub-process. (This example will be explained in the comprehensive application)
A simple example is provided.
// Create a socket pair
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <stdlib. h>
# Include <stdio. h>
Int main ()
{
Int fd [2];
Int r = socketpair (AF_UNIX, SOCK_STREAM, 0, fd );
If (r <0 ){
Perror ("socketpair ()");
Exit (1 );
}
If (fork ()){
/* Parent process: echo client */
Int val = 0;
Close (fd [1]);
While (1 ){
Sleep (1 );
++ Val;
Printf ("Sending data: % d/n", val );
Write (fd [0], & val, sizeof (val ));
Read (fd [0], & val, sizeof (val ));
Printf ("Data encoded Ed: % d/n", val );
}
}
Else {
/* Child process: echo server */
Int val;
Close (fd [0]);
While (1 ){
Read (fd [1], & val, sizeof (val ));
++ Val;
Write (fd [1], & val, sizeof (val ));
}
}
}
An example of using sendmsg to transmit data is provided.
/*************************************** **
*
* Listing 1.2:
*
* Example semi Ming I/O on s socket pair:
*
**************************************** ***/
# 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;/* Status return code */
Int s [2];/* Pair of sockets */
Struct msghdr msg;
Struct iovec iov [1];
Char send_buf [100] = "TEST ";
Struct msghdr msgr;
Struct iovec iovr [1];
Char recv_buf [100];
/*
* Create a pair of local sockets:
*/
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;/* Failed */
}
/*
* Sendmsg s [1]:
*/
Bzero (& msg, sizeof (msg ));
Msg. msg_name = NULL;/* attention this is a pointer to void * type */
Msg. msg_namelen = 0;
Iov [0]. iov_base = send_buf;
Iov [0]. iov_len = sizeof (send_buf );
Msg. msg_iov = iov;
Msg. msg_iovlen = 1;
Printf ("sendmsg begin./n ");
Z = sendmsg (s [1], & msg, 0 );
If (z =-1)
{
Fprintf (stderr, "Sendmsg failed. errno: % s/n", strerror (errno ));
Return-1;
}
Printf ("Sendmsg Success! /N ");
/*
* Read from socket s [0]:
*/
Bzero (& msg, sizeof (msg ));
Msgr. msg_name = NULL;/* attention this is a pointer to void * type */
Msgr. msg_namelen = 0;
Iovr [0]. iov_base = & recv_buf;
Iovr [0]. iov_len = sizeof (recv_buf );
Msgr. msg_iov = iovr;
Msgr. msg_iovlen = 1;
Z = recvmsg (s [0], & msgr, 0 );
If (z =-1)
{
Fprintf (stderr, "Recvmsg failed. errno: % s/n", strerror (errno ));
Return-1;
}
Printf ("Recvmsg Success! /N ");
Printf ("recvmsg: % s/n", recv_buf );
/*
* Close the sockets:
*/
Close (s [0]);
Close (s [1]);
Puts ("Done ");
Return 0;
}
Use of socketpair