IPv6 Socket Code Example

All the previous examples of socket programming in this website have been based on IPv4. In this code example, we see a stream IPv6 socket connection. The code for IPv6 is very similar to the IPv4 connection with minor changes in socket structure. For the interested reader, the IPv4 Socket example for stream sockets is available at this link.

All other IPv4 examples should work with changes in the socket structure for IPv6. The current code should allow the reader to experiment with additional socket code implementions for IPv6.

The Socket code for IPv6 is provided below

Server CodeClient Code

#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
/* Internet socket structures
* are defined in below header */
#include <netinet/in.h>
/* inet_addr is defined in
* below header */
#include <arpa/inet.h>
#include <signal.h>

/* for Read/write API calls*/
#include <unistd.h>

#define PORT 55000
#define TRUE 1

/* IPv6 socket structures for
* server and client */

struct sockaddr_in6 serveraddr, clientaddr;

/* socket file descriptor */
int socket_fd, clientfd;

/* Interrupt_handler – so that CTRL + C can be used to
* exit the program */
void interrupt_handler (int signum) {
        close(socket_fd);
        close(clientfd);

        printf(“socket connection closed\n”);
        exit(0);
}

int main () {
        /* length of client structure received on accept */
        int length;
        char buffer[50];
        char *string = “Hello from Server”;

        /* signal handler to exit out of while 1 loop */
        signal (SIGINT, interrupt_handler);
        signal (SIGPIPE, interrupt_handler);

        /* Part 1 – create the socket */

        if ((socket_fd = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
                printf(“unable to create the socket\n”);
                exit(0);
        }

        /* Part 2 – create the server connection – fill the structure*/
        serveraddr.sin6_family = AF_INET6;

        /* use loopback address (127.0.0.1)so that
        * the server/client connection
        * can be created on the same machine */
        inet_pton(AF_INET6, “::1”, (void *)(&serveraddr.sin6_addr));
        serveraddr.sin6_port = htons(PORT);

        /* Part 3 – bind and start accepting connections */

        if(0 != bind(socket_fd, (struct sockaddr *)&serveraddr, sizeof(struct sockaddr_in6))) {
                printf(“unable to bind to the socket\n”);
                goto end;
        }

        /* accept upto 5 pending requests
        * and make the server socket a passive socket
        */
        if (listen(socket_fd, 5) == -1) {
                printf(“Listen Failed\n”);
                goto end;
        }

        /* accept is a blocking call and the call would stop further invocation of            code till a client connection is accepted */
        printf(“waiting for connection\n”);
        clientfd = accept(socket_fd, (struct sockaddr *)&clientaddr, &length);

        if (clientfd < 0) {
                 printf(“Unable to connect to Client FD\n”);
                goto end;
        }

        /* read and write data from/to client socket */
        while (TRUE) {
                 /* Use the client socket FD to read data from the client */
                 memset(buffer, 0, sizeof(buffer));
                 read(clientfd, buffer, sizeof(buffer));
                 printf(“%s\n”, buffer);

                 /* write data to the client */
                memset(buffer, 0, sizeof(buffer));
                snprintf(buffer, (strlen(string) + 1), “%s\n”, string);
                write(clientfd, buffer, sizeof(buffer));
        }
end:
        if (clientfd > 0)
                close(clientfd);
        close(socket_fd);

        return 0;
}

#include <stdio.h>
#include <sys/socket.h>
/* Internet socket structures
* are defined in below header */
#include <netinet/in.h>
/* inet_addr is defined in
* below header */
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>

/* For read/write APIs*/
#include <unistd.h>
#define PORT 55000

/* socket file descriptor */
int socket_fd;
/* IPv6 socket structure */
struct sockaddr_in6 sockaddrin;

/* Interrupt_handler so that CTRL +C can be used
* to exit the program */
void interrupt_handler (int signum) {
        close(socket_fd);
        printf(“socket connection closed\n”);
        exit(0);
}

int main () {
        char buffer[50] = {0};
        char *string = “Hello from client”;

        /* signal handler to exit out of while 1 loop */
        signal (SIGINT, interrupt_handler);
        signal (SIGPIPE, interrupt_handler);

        /* Part 1 – create a socket */
        /* create the socket FD */
        socket_fd = socket(AF_INET6, SOCK_STREAM, 0);

        if (socket_fd < 0) {
                printf(“The Socket API call failed\n”);
                exit(0);
        }

        /* Part 2 – allocate server connection details
        * structure for connection */

        /* fill the sockaddr_in6 structure
        * so that it can be connected to the socket */

        sockaddrin.sin6_family = AF_INET6;
        sockaddrin.sin6_port = htons(PORT);

        /* use loopback address (127.0.0.1)so that
        * the server/client connection
        * can be created on the same machine */
        inet_pton(AF_INET6, “::1”, (void *)(&sockaddrin.sin6_addr));

        /* part 3 – connect to the server defined by IP address and port*/
        /* connect to the server */
        if (0 != connect(socket_fd, (struct sockaddr *)&sockaddrin,
(socklen_t)(sizeof(struct sockaddr_in6)))) {
                printf(“Unable to connect\n”);
                /* unable to connect to server */
                goto end;
        }

        /* Part 4 – send/receive data */
        /* read and write data from/to client socket */
        while (1) {

                memset(buffer, 0, sizeof(buffer));
                /* write data to the server */
                snprintf(buffer, (strlen(string) + 1), “%s\n”, string);
                write(socket_fd, buffer, sizeof(buffer));

                memset(buffer, 0, sizeof(buffer));
                read(socket_fd, buffer, sizeof(buffer));
                printf(“%s\n”, buffer);
        }
end:
        close(socket_fd);
}

The output of the “ss -6” shell command provides the output of the sample code IPv6 socket connection.

Netid   State     Recv-Q    Send-Q    Local Address:Port          Peer Address:Port

tcp       ESTAB     50           0               [::1]:55000                           [::1]:49206
tcp       ESTAB     0             50             [::1]:49206                           [::1]:55000

Select and Poll API

Comments

  1. Pingback: Raw Socket Code – IP_HDRINCL Code example | Hitch Hiker's Guide to Learning

Leave a Reply

Your email address will not be published. Required fields are marked *