How to Create a Socket in C
In this article, we will explain the different approaches to create sockets in the C programming language. These approaches provide different methods to create sockets, each with its own advantages and own uses.
Use the “Socket()” System Call to Create a Socket
This system call is the most popular and easiest way to create a socket in C. We can pass three arguments to this system call:
- Domain of the Socket
- Type of the Socket
- Type of the Protocol
First, the domain defines the socket’s communication domain which might be AF_INET for IPv4 or AF_INET6 for IPv6. The type argument might be SOCK_STREAM (used for TCP socket) or SOCK_DGRAM (used for a UDP socket). Lastly, the protocol argument indicates the protocol to be used.
We can create a code for TCP socket using the socket() system call:
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
int main () {
int socketDescriptor = socket (AF_INET, SOCK_STREAM, 0);
if (socketDescriptor < 0) {
perror ("Error creating socket");
return 1;
}
// Use the socket for communication
// here we print a message indicating the successful creation of the socket
printf("TCP socket created successfully!\n");
// Close the socket
close (socketDescriptor);
return 0;
}
Output:
$ ./soc
TCP socket created successfully!
In this programming example, the program creates a TCP socket using the socket() function. If the socket creation fails, an error message is displayed. A success message is printed if the socket is created. Lastly, we can use the close() function to close the socket.
Use the Setsockopt() Function to Create a Socket
This function allows us to set the various socket-level options to modify the behaviour of the socket. Here, we use the setsockopt() function to create a socket:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main() {
int sockfd = socket (AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
perror ("Error creating socket");
return 1;
}
// Set additional socket options
int reuse = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1) {
perror ("Error setting socket options");
close (sockfd);
return 1;
}
// Continue with socket usage
printf ("Socket created successfully using setsockopt () function \n");
close (sockfd);
return 0;
}
Output:
$ ./soc
Socket created successfully using setsockopt () function
Here in the programming example, the program uses the “setsockopt()” function to create a socket in C. The code generates a TCP socket. Then, it enables the reuse of local addresses using the setsockopt() function. A success message is printed before closing the socket.
Use the Socketpair() Function to Create a Socket
This function is a UNIX system call that allows us to create and join two sockets. This method generates a pair of socket identifiers that are linked together to provide a two-way communication channel.
We can use the socketpair() function to create a socket.
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
int main () {
int sockfd [2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd) < 0) {
perror ("Error creating socket pair");
return 1;
}
// Socket pair created successfully
printf ("Socket pair created!\n");
// Close the sockets
close (sockfd[0]);
close (sockfd[1]);
return 0;
}
Output:
$ ./soc
Socket pair created!
Here, the example uses the “socketpair()” function to create a pair of sockets in C with the “AF_UNIX” address family and the “SOCK_STREAM” type. Lastly, it displays a success message and closes both sockets.
Conclusion
The sockets are a very important tool to build the networked applications. In this article, we discussed three different approaches to create a socket in C. We explained the “socket()” system call, the “setsockopt()” function, and the “socketpair()” function. By understanding these approaches that are provided in this article, we can create the sockets in C and build the robust networked applications very easily.
Source: linuxhint.com