| by Arround The Web | No comments

How to Create a Socket in C

A socket plays an important part in the computer networking. A socket enables flawless communication between various applications that run on different computers. A socket mainly serves as a software endpoint and it is defined as a dedicated communication channel that facilitates the exchange of data. Having the ability to provide reliable, efficient, and flexible means of transmitting information over networks, sockets have become very much important tools to develop the networked applications. Sockets act as a bridge that connects these applications that enable them to send and receive information across the network easily. We can use the sockets to create powerful applications that communicate with each other, irrespective of their physical location.

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:

  1. Domain of the Socket
  2. Type of the Socket
  3. 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 <stdio.h>

#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:

$ gcc soc.c -o soc

$ ./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 <stdio.h>

#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:

$ gcc soc.c -o soc

$ ./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/types.h>

#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:

gcc server.c -o soc

$ ./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.

Share Button

Source: linuxhint.com

Leave a Reply