Basic example of socketpair
In this program, you are going to learn
How to create a socket ?
How to create a fork ?
How to send a data ?
How to recv a data?
Topics in this section,
Let us answer few basic questions in this socket
Why use socketpair in the program?
See Answer
socketpair creates a pair of connected sockets in the specified domain, allowing communication between processes.
What parameters does socketpair take?
See Answer
socketpair(domain, type, protocol, sv), where domain is typically AF_UNIX for local communication.
Why use AF_UNIX as the domain for socketpair?
See Answer
AF_UNIX is suitable for local communication within the same system.
What is the significance of SOCK_STREAM as the socket type?
See Answer
SOCK_STREAM provides a reliable, connection-oriented communication channel.
Can socketpair be used for network communication?
See Answer
No, socketpair is designed for local communication between processes on the same system.
Why is protocol often specified as 0 in the socketpair call?
See Answer
protocol is typically set to 0 to allow the system to choose the default protocol for the specified domain and type.
What is the purpose of the sv array in the socketpair call?
See Answer
It holds the file descriptors for the created sockets.
How does socketpair handle errors during execution?
See Answer
It returns -1 on error, and you can use perror to print an error message.
Can the created sockets be used for communication between unrelated processes?
See Answer
No, socketpair is typically used for communication between related processes (e.g., parent and child).
Can the created sockets be passed between processes using
fork?
See Answer
Yes, both the parent and child processes have access to the created sockets.
How does socketpair differ from other socket creation functions like socket?
See Answer
socketpair creates a pair of connected sockets, while socket creates a single socket for network communication.
How is data sent from the parent to the child process?
See Answer
The parent uses the send function to transmit data over the socket pair.
How is data received by the child process?
See Answer
The child uses the recv function to receive data from the parent.
Why is close called for each socket in both parent and child processes?
See Answer
close is used to close unnecessary file descriptors, releasing resources.
There are many functions used in socketpair. We can classify those functions based on functionalities.
Create Socket
Bind Socket
Recv data_packet
Close socket
socketpaircreates a pair of connected sockets for local inter-process communication. For example,
sockfd = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
forkcreates a child process, and both the parent and child processes can communicate through the socket pair. For example,
pid_t pid = fork();
recvis used in network programming to receive data from a connected socket. For example,
ret = recv(sv[1], buffer, sizeof(buffer), 0);
sendis used in network programming to send data over a connected socket. For example,
ret = send(sv[0], buffer, strlen(buffer), 0);
closeis used to close the socket To free up system resources associated with the socket. For example,
(void)close(sv[0]);
(void)close(sv[1]);
See the full program below,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#define BUFFER_SIZE 1024
int main()
{
int sv[2], ret;
int sockfd;
char buffer[BUFFER_SIZE];
sockfd = socketpair(AF_UNIX,
SOCK_STREAM, 0, sv);
if (sockfd < 0) {
perror("socketpair");
return -1;
}
printf("Socket pair created\n");
pid_t pid = fork();
if (pid == -1) {
perror("fork");
return -2;
}
if (pid == 0) {
// Child process (Server)
(void)close(sv[0]);
ret = recv(sv[1],
buffer, sizeof(buffer), 0);
if (ret < 0) {
perror("recv");
(void)close(sv[1]);
return -3;
}
buffer[ret] = '\0';
printf("Received from server: %s\n",
buffer);
(void)close(sv[1]);
} else {
// Parent process (Client)
(void)close(sv[1]);
memset(buffer, 0,
sizeof(buffer));
strncpy(buffer, "hello child",
strlen("hello child") + 1);
buffer[strlen(buffer) + 1] = '\0';
ret = send(sv[0], buffer,
strlen(buffer), 0);
if (ret == -1) {
perror("send");
(void)close(sv[0]);
return -4;
}
printf("Sending client buffer = %s\n",
buffer);
sleep(1);
}
(void)close(sv[0]);
(void)close(sv[1]);
return 0;
}
1$ gcc -o socketpair socketpair.c
2
3$ sudo ./socketpair
4
5Socket pair created
6Sending client buffer = hello child
7Received from server: hello child
There are many functions used in socketpair. We can classify those functions based on functionalities.
Create Socket
Fork
Recv data_packet
Send data_packet
Close socket
socketpaircreates a pair of connected sockets for local inter-process communication. For example,
sockfd = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
forkcreates a child process, and both the parent and child processes can communicate through the socket pair. For example,
pid_t pid = fork();
recvis used in network programming to receive data from a connected socket. For example,
ret = recv(sv[1], buffer, sizeof(buffer), 0);
sendis used in network programming to send data over a connected socket. For example,
ret = send(sv[0], buffer, strlen(buffer), 0);
closeis used to close the socket To free up system resources associated with the socket. For example,
(void)close(sv[0]);
(void)close(sv[1]);
See the full program below,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#define BUFFER_SIZE 1024
#define NUM_MESSAGES 10
int main()
{
int sv[2], ret;
int sockfd;
int i, j;
char buffer[BUFFER_SIZE];
sockfd = socketpair(AF_UNIX,
SOCK_STREAM, 0, sv);
if (sockfd < 0) {
perror("socketpair");
return -1;
}
printf("Socket pair created\n");
pid_t pid = fork();
if (pid == -1) {
perror("fork");
return -2;
}
if (pid == 0) {
// Child process (Server)
(void)close(sv[0]);
i = 0;
while (i < NUM_MESSAGES) {
ret = recv(sv[1],
buffer, sizeof(buffer), 0);
if (ret < 0) {
perror("recv");
(void)close(sv[1]);
return -3;
}
buffer[ret] = '\0';
printf("Received from server: %s\n",
buffer);
++i;
}
(void)close(sv[1]);
} else {
// Parent process (Client)
(void)close(sv[1]);
j = 0;
while (j < NUM_MESSAGES) {
memset(buffer, 0,
sizeof(buffer));
strncpy(buffer, "hello child",
strlen("hello child") + 1);
buffer[strlen(buffer) + 1] = '\0';
ret = send(sv[0], buffer,
strlen(buffer), 0);
if (ret == -1) {
perror("send");
(void)close(sv[0]);
return -4;
}
printf("Sending client buffer = %s\n",
buffer);
sleep(1);
++j;
}
}
(void)close(sv[0]);
(void)close(sv[1]);
return 0;
}
1$ gcc -o socketpair socketpair.c
2
3$ sudo ./socketpair
4
5Socket pair created
6Sending client buffer = hello child
7Received from server: hello child
8Sending client buffer = hello child
9Received from server: hello child
10Sending client buffer = hello child
11Received from server: hello child
12Sending client buffer = hello child
13Received from server: hello child
14Sending client buffer = hello child
15Received from server: hello child
16Sending client buffer = hello child
17Received from server: hello child
18Sending client buffer = hello child
19Received from server: hello child
20Sending client buffer = hello child
21Received from server: hello child
22Sending client buffer = hello child
23Received from server: hello child
24Sending client buffer = hello child
25Received from server: hello child
Socket API |
Learning |
|---|---|
socketpair |
creates a pair of connected sockets for local inter-process communication. |
fork |
creates a child process, and both the parent and child processes can communicate through the socket pair. |
recv |
Receive data from a connected socket. |
send |
Send data over a connected socket. |
Previous topic
Current topic
Next IPC