Basic example ipv6 udp server and client

  • In this program, you are going to learn

  • How to create a Socket ?

  • How to bind a socket ?

  • How to send a data ?

  • How to recv a data ?

Before executing the program that relies on IPv6, enable the loopback interface for IPv6 using the following command:

sudo sysctl -w net.ipv6.conf.lo.disable_ipv6=0
  • This command sets the disable_ipv6 parameter to 0 for the loopback interface (lo), allowing IPv6 functionality.

  • Ensure to use this command cautiously and consider the implications, especially on production systems.

  • After enabling IPv6, proceed to execute your program that relies on IPv6 functionality.

Let us answer few basic questions in this socket

What does socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP) do?

When is it appropriate to use SOCK_DGRAM with IPv6?

What privileges are required to create an IPv6 UDP socket?

Can this socket be used for TCP communication?

How does an IPv6 UDP socket differ from an IPv4 UDP socket?

Can I use IPv6 UDP sockets in a mixed IPv4/IPv6 environment?

How do I specify a port number for an IPv6 UDP socket?

Can I use IPv6 UDP sockets for non-blocking I/O?

How do I handle connection establishment with IPv6 UDP sockets?

Are there any compatibility issues with older systems when using IPv6 UDP sockets?

How do I handle socket errors related to network communication?

When handling socket errors, is it important to close the socket?

https://www.plantuml.com/plantuml/svg/PP0_3u8m4CNtVegwc68wE3dW0YeOOXG2khWqq1wH29IqXS5FBrK6_qoNT-_U-oNdQPChqpSrcfaRD81rbtEeymAz3EHgoUzWY1-ow1ISLnHdyVR0TllKFr8S9KaQdsDssINE6hb5rGgYGGsWs3j7CT56zTnWgtCXrCAPJnTMKQoh1yYIVEmSMwM03wMIpMzTqPSbZB7D9Go3b6NHDQ1u-O_18uVN5RHcuipjO9PcwyaWA-7mMchu9eocDh937hHY_CeJ
  • There are many functions used in socket. We can classify those functions based on functionalities.

    • Create Socket

    • Bind Socket

    • Recvfrom data_packet

    • Close socket

  • socket() is used to create a new socket. For example,

server_socket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  • bind() is used to associate the socket with a specific address and port. For example,

ret = bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr));
  • recvfrom is commonly used with UDP sockets, where communication is connectionless. it provides information about the source (sender) of the data, including the sender’s IP address and port number. For example,

len = recvfrom(server_socket, buffer, BUFFER_SIZE, 0, (struct sockaddr*)&client_addr, &addr_size);
  • close is used to close the socket To free up system resources associated with the socket. For example,

(void)close(server_socket);
  • See the full program below,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define BUFFER_SIZE 1024

void validate_convert_port(
char *port_str,
struct sockaddr_in6 *sock_addr)
{
  int port;

  if (port_str == NULL) {
     perror("Invalid port_str\n");
     exit(EXIT_FAILURE);
 }

 if (sock_addr == NULL) {
     perror("Invalid sock_addr\n");
     exit(EXIT_FAILURE);
 }

 port = atoi(port_str);

 if (port == 0) {
   perror("Invalid port\n");
   exit(EXIT_FAILURE);
 }

 sock_addr->sin6_port = htons(
 (uint16_t)port);
 printf("Port: %d\n",
 ntohs(sock_addr->sin6_port));
}

void validate_convert_addr(
char *ip_str,
struct sockaddr_in6 *sock_addr)
{
  if (ip_str == NULL) {
     perror("Invalid ip_str\n");
     exit(EXIT_FAILURE);
 }

 if (sock_addr == NULL) {
     perror("Invalid sock_addr\n");
     exit(EXIT_FAILURE);
 }

 printf("IP Address: %s\n", ip_str);

 if (inet_pton(AF_INET6, ip_str,
 &(sock_addr->sin6_addr)) <= 0) {
    perror("Invalid address\n");
    exit(EXIT_FAILURE);
  }
}

int main(int argc, char *argv[]) 
{
  int server_socket;
  int ret;
  struct sockaddr_in6 
  server_addr, 
  client_addr;
  socklen_t addr_size = sizeof(
  client_addr);
  char buffer[BUFFER_SIZE];

  if (argc != 3) {
    printf("%s<port-number><ip-addr>",
    argv[0]);
    return -1;
  }

  server_socket = socket(AF_INET6,
                  SOCK_DGRAM,
                  IPPROTO_UDP);

  if (server_socket < 0) {
    perror("Socketfailed");
    return -2;
  }

  server_addr.sin6_family = AF_INET6;
  validate_convert_port(argv[1], 
  &server_addr);
  validate_convert_addr(argv[2], 
  &server_addr);

  ret = bind(server_socket,
  (struct sockaddr *)&server_addr,
  sizeof(server_addr));

  if (ret < 0) {
    perror("Bind failed");
    (void)close(server_socket);
    return -3;
  }

  ret = recvfrom(server_socket, 
  buffer, 
  BUFFER_SIZE, 0, 
  (struct sockaddr *)&client_addr, 
  &addr_size);

  if (ret < 0) {
    perror("recvfrom");
    (void)close(server_socket);
    return -4;
  }

  printf("received: %s\n", buffer);

  (void)close(server_socket);

  return 0;
}

1$ gcc -o server server.c
2
3$ sudo ./server 8080 ::1
4
5Port: 8080
6IP Address: ::1
7received: Hello from client!
https://www.plantuml.com/plantuml/svg/LKyz3u8m5Dpv5S_5jAQ3auCk6553Z48GvmRA8n8B9Us3mLzlaKN7-yhTXOuAIqEhWmKziKLmVQ5G5sNm5OAjC-g5nFOd6T-E-KR0FJbSP7JEzbS1SPfcIPx8HvJodVTYLv5XIZVOaPoJ0igXhj4AS6GrTco6NC1Q0FFSe0WcQr5LTiMN3ks8Lav80BZcZQPcFoIVYjXeceehRHp-jtdHaUc3q2_nfpu=
  • There are many functions used in socket. We can classify those functions based on functionalities.

    • Create Socket

    • Sendto data_packet

    • Close socket

  • socket is used to create a new socket. For example,

client_socket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  • sendto is used to send the encoded message to the specified server address and port using a UDP socket. For example,

ret = sendto(client_socket, buffer, strlen(buffer), 0, (struct sockaddr*)&server_addr, sizeof(server_addr));
  • close is used to close the socket To free up system resources associated with the socket. For example,

(void)close(client_socket);
  • See the full program below,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define BUFFER_SIZE 1024

void validate_convert_port(
char *port_str,
struct sockaddr_in6 *sock_addr)
{
  int port;

  if (port_str == NULL) {
     perror("Invalid port_str\n");
     exit(EXIT_FAILURE);
 }

 if (sock_addr == NULL) {
     perror("Invalid sock_addr\n");
     exit(EXIT_FAILURE);
 }

 port = atoi(port_str);

 if (port == 0) {
   perror("Invalid port\n");
   exit(EXIT_FAILURE);
 }

 sock_addr->sin6_port = htons(
 (uint16_t)port);
 printf("Port: %d\n",
 ntohs(sock_addr->sin6_port));
}

void validate_convert_addr(
char *ip_str,
struct sockaddr_in6 *sock_addr)
{
  if (ip_str == NULL) {
     perror("Invalid ip_str\n");
     exit(EXIT_FAILURE);
 }

 if (sock_addr == NULL) {
     perror("Invalid sock_addr\n");
     exit(EXIT_FAILURE);
 }
 
 printf("IP Address: %s\n", ip_str);

 if (inet_pton(AF_INET6, ip_str,
 &(sock_addr->sin6_addr)) <= 0) {
    perror("Invalid address\n");
    exit(EXIT_FAILURE);
  }
}

int main(int argc, char *argv[]) 
{
  int client_socket;
  int ret;
  struct sockaddr_in6 
  server_addr;
  char buffer[BUFFER_SIZE] = 
  "Hello from client!";

  if (argc != 3) {
    printf("%s<port-number><ip-addr>\n",
    argv[0]);
    return -1;
  }

  client_socket = socket(AF_INET6,
                  SOCK_DGRAM,
                  IPPROTO_UDP);

  if (client_socket < 0) {
    perror("Socket failed");
    return -2;
  }

  server_addr.sin6_family = AF_INET6;
  validate_convert_port(argv[1], 
  &server_addr);
  validate_convert_addr(argv[2], 
  &server_addr);

  ret = sendto(client_socket, 
  buffer, 
  strlen(buffer), 0, 
  (struct sockaddr *)&server_addr, 
  sizeof(server_addr));

  if (ret < 0) {
    perror("sendto");
    (void)close(client_socket);
    return -3;
  }

  printf("Messagesent: %s\n", 
  buffer);

  (void)close(client_socket);

  return 0;
}

1$ gcc -o client client.c
2
3$ sudo ./client 8080 ::1
4
5Port: 8080
6IP Address: ::1
7Messagesent: Hello from client!

$ sudo ./server 8080 ::1

$ sudo ./client 8080 ::1

program to run with elevated privileges, listen on port 8080, and bind to the loopback address ::1.

<port_number> <ip_address> decided by the user based on the connection.

https://www.plantuml.com/plantuml/svg/RP0nRuCm48Lt_uhh4jaDGwOEq4gX3KIe8Y08InSBpA5O1HpPXghzzSLfXgRgT7ftx-vzkgsnjRRJq9CxUy81mPnhWNtTaCk0-4Q9TxGqZ7boY8uF7fJPoolVxOimzI39yo8xPhpQvImWVYF7bXhKCsh-i-S1DLPFmeBJTTlgUxQwEfpomCWlL1tzrMFkb8Gds0JauoHx17ej4XxXKAKyZSeot4SbOo0Dq4yqZFWQnTnfDVm5QAQkmoNekOhZgE1byXPviFaNJFGIHti5Mxd27PxZMQzbG71iBoJ4fxEIBHEzCdYRjtYDLMUoNRpBLxy1
  • There are many functions used in socket. We can classify those functions based on functionalities.

    • Create Socket

    • Bind Socket

    • Recvfrom data_packet

    • Close socket

  • socket() is used to create a new socket. For example,

server_socket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  • bind() is used to associate the socket with a specific address and port. For example,

ret = bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr));
  • recvfrom is commonly used with UDP sockets, where communication is connectionless. it provides information about the source (sender) of the data, including the sender’s IP address and port number. For example,

len = recvfrom(server_socket, buffer, BUFFER_SIZE, 0, (struct sockaddr*)&client_addr, &addr_size);
  • close is used to close the socket To free up system resources associated with the socket. For example,

(void)close(server_socket);
  • See the full program below,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define BUFFER_SIZE 1024
#define NUM_MESSAGES 10

void validate_convert_port(
char *port_str,
struct sockaddr_in6 *sock_addr)
{
  int port;

  if (port_str == NULL) {
   perror("Invalid port_str\n");
   exit(EXIT_FAILURE);
  }

  if (sock_addr == NULL) {
   perror("Invalid sock_addr\n");
   exit(EXIT_FAILURE);
  }

  port = atoi(port_str);

  if (port == 0) {
    perror("Invalid port\n");
    exit(EXIT_FAILURE);
  }

  sock_addr->sin6_port = htons(
  (uint16_t)port);
  printf("Port: %d\n",
  ntohs(sock_addr->sin6_port));
}

void validate_convert_addr(
char *ip_str,
struct sockaddr_in6 *sock_addr)
{
  if (ip_str == NULL) {
     perror("Invalid ip_str\n");
     exit(EXIT_FAILURE);
 }

 if (sock_addr == NULL) {
     perror("Invalid sock_addr\n");
     exit(EXIT_FAILURE);
 }

 printf("IP Address: %s\n", ip_str);

 if (inet_pton(AF_INET6, ip_str,
 &(sock_addr->sin6_addr)) <= 0) {
    perror("Invalid address\n");
    exit(EXIT_FAILURE);
  }
}


int main(int argc, char *argv[]) 
{
  int server_socket;
  int i, ret;
  struct sockaddr_in6 
  server_addr, 
  client_addr;
  socklen_t addr_size = sizeof(
  client_addr);
  char buffer[BUFFER_SIZE];

  if (argc != 3) {
    printf("%s<port-number><ip-addr>", 
    argv[0]);
    return -1;
  }

  server_socket = socket(AF_INET6,
                  SOCK_DGRAM,
                  IPPROTO_UDP);

  if (server_socket < 0) {
    perror("Socket failed");
    return -2;
  }

  server_addr.sin6_family = AF_INET6;
  validate_convert_port(argv[1], 
  &server_addr);
  validate_convert_addr(argv[2], 
  &server_addr);

  ret = bind(server_socket,
  (struct sockaddr *)&server_addr,
  sizeof(server_addr));

  if (ret < 0) {
    perror("Bind failed");
    (void)close(server_socket);
    return -3;
  }

  i = 0;
  while (i < NUM_MESSAGES) { 
    memset(buffer, 0, 
    sizeof(buffer));

    ret = recvfrom(server_socket, 
    buffer,
    BUFFER_SIZE, 0,
    (struct sockaddr *)&client_addr,
    &addr_size);

    if (ret < 0) {
      perror("recvfrom");
      (void)close(server_socket);
      return -4;
    }

    printf("Received: %s\n", 
    buffer);
    ++i;
  }

  (void)close(server_socket);

  return 0;
}

 1$ gcc -o server server.c
 2
 3$ sudo ./server 8080 ::1
 4
 5Port: 8080
 6IP Address: ::1
 7Received: Hello 1 from server!
 8Received: Hello 2 from server!
 9Received: Hello 3 from server!
10Received: Hello 4 from server!
11Received: Hello 5 from server!
12Received: Hello 6 from server!
13Received: Hello 7 from server!
14Received: Hello 8 from server!
15Received: Hello 9 from server!
16Received: Hello 10 from server!
https://www.plantuml.com/plantuml/svg/LP0nRy8m48Lt_uhhWkpYWQb3GYKYI16gGY82inMSYx0mCV8vLEslnu4ExNHwxpxTUxe5XSQ7uMBPIpZX1O6kZKRR7DbZmH9o-eo1PnzgiyttRnBgSlcfLkjTLaZOLDMkt9VgiAf4oX83xp1BsTV9M0J-EmpCONieL97NTRREQo704F1l916lOzy6nxKrs0Vrp99m7BeElGGAtcBFdr98c4dWqHjqW15jsjQ_YWcXlw5Nev80P7xGTVoFASPgqwb9MGnxb689lpdJ2cqTuV_mo59mLxQ8RFpA7G==
  • There are many functions used in socket. We can classify those functions based on functionalities.

    • Create Socket

    • Sendto data_packet

    • Close socket

  • socket is used to create a new socket. For example,

client_socket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  • sendto is used to send the encoded message to the specified server address and port using a UDP socket. For example,

ret = sendto(client_socket, buffer, strlen(buffer), 0, (struct sockaddr*)&server_addr, sizeof(server_addr));
  • close is used to close the socket To free up system resources associated with the socket. For example,

(void)close(client_socket);
  • See the full program below,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define BUFFER_SIZE 1024
#define NUM_MESSAGES 10

void validate_convert_port(
char *port_str,
struct sockaddr_in6 *sock_addr)
{
  int port;

  if (port_str == NULL) {
     perror("Invalid port_str\n");
     exit(EXIT_FAILURE);
 }

 if (sock_addr == NULL) {
     perror("Invalid sock_addr\n");
     exit(EXIT_FAILURE);
 }
  
 port = atoi(port_str);

 if (port == 0) {
   perror("Invalid port\n");
   exit(EXIT_FAILURE);
 }

 sock_addr->sin6_port = htons(
 (uint16_t)port);
 printf("Port: %d\n",
 ntohs(sock_addr->sin6_port));
}

void validate_convert_addr(
char *ip_str,
struct sockaddr_in6 *sock_addr)
{
  if (ip_str == NULL) {
     perror("Invalid ip_str\n");
     exit(EXIT_FAILURE);
 }

 if (sock_addr == NULL) {
     perror("Invalid sock_addr\n");
     exit(EXIT_FAILURE);
 }

 printf("IP Address: %s\n", ip_str);

 if (inet_pton(AF_INET6, ip_str,
 &(sock_addr->sin6_addr)) <= 0) {
    perror("Invalid address\n");
    exit(EXIT_FAILURE);
  }
}

int main(int argc, char *argv[]) 
{
  int client_socket;
  int i, ret;
  struct sockaddr_in6 
  server_addr;
  char buffer[BUFFER_SIZE]; 
  socklen_t addr_size = sizeof(
  server_addr);

  if (argc != 3) {
    printf("%s<port-number><ip-addr>\n",
    argv[0]);
    return -1;
  }

  client_socket = socket(AF_INET6,
                  SOCK_DGRAM,
                  IPPROTO_UDP);

  if (client_socket < 0) {
    perror("Socket failed");
    return -2;
  }

  server_addr.sin6_family = AF_INET6;
  validate_convert_port(argv[1], 
  &server_addr);
  validate_convert_addr(argv[2], 
  &server_addr);

  i = 0;
  while (i < NUM_MESSAGES) {  
   snprintf(buffer, sizeof(buffer),
        "Hello %d from server!",
        i + 1);

   ret = sendto(client_socket, buffer,
   strlen(buffer), 0,
   (struct sockaddr *)&server_addr,
   sizeof(server_addr));

   if (ret < 0) {
     perror("sendto");
     (void)close(client_socket);
     return -3;
   }

   printf("Message %d sent: %s\n",
   i + 1, buffer);
   ++i;
  }

  (void)close(client_socket);

  return 0;
}

 1$ gcc -o client client.c
 2
 3$ sudo ./client 8080 ::1
 4
 5Port: 8080
 6IP Address: ::1
 7Message 1 sent: Hello 1 from server!
 8Message 2 sent: Hello 2 from server!
 9Message 3 sent: Hello 3 from server!
10Message 4 sent: Hello 4 from server!
11Message 5 sent: Hello 5 from server!
12Message 6 sent: Hello 6 from server!
13Message 7 sent: Hello 7 from server!
14Message 8 sent: Hello 8 from server!
15Message 9 sent: Hello 9 from server!
16Message 10 sent: Hello 10 from server!

$ sudo ./server 8080 ::1

$ sudo ./client 8080 ::1

program to run with elevated privileges, listen on port 8080, and bind to the loopback address ::1.

<port_number> <ip_address> decided by the user based on the connection.

Default Domain:

By default, the socket is configured to work in the AF_INET6 domain, handling all types of network data.

Additional Domain Support:

We expand the socket’s capabilities to also function in the PF_INET6 domain, allowing it to operate similarly to AF_INET6.

Socket Creation:

We set up a network connection point known as a socket using socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP).

Working Scenario:

Despite the change in domain to PF_INET6, the socket continues to operate the same way, handling general network data.

Socket API

Learning

socket

Create a new socket

bind

Associate the socket with a specific address and port

recvfrom

It provides information about the source (sender) of the data, including the sender’s IP address and port number.

sendto

Send the encoded message to the specified server address and port using a UDP socket.