Use of socketpair

Source: Internet
Author: User
Tags sendmsg


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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.