UDP Server/client example

The interested reader can refer an introduction on UDP socket connection in the previous article <A UDP Socket Connection>.

In this article we look at the server/client code example and examine its intricacies in the following article.

In UDP socket connection establishment – the following occurs

  1. The UDP server binds itself to a port.
  2. The client sends data to the server port.
  3. From Point 2, we understand that the client needs to know the exact address and port of the server to send data to the server
  4. On receipt of a datagram from the client, the server can send a response back to the client address received in the recvfrom API 

The server and client code do not perform an accept and connect routine to establish a 1:1 connection as performed in a TCP Socket connection.

Server CodeClient Code

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>

struct sockaddr_in *sock_addr = NULL;
struct sockaddr_in *client_addr = NULL;

int sock_udp_server;
#define UDPPORT 55555

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

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

int main () {
    socklen_t length, num_of_bytes;
    char buffer[50] = {0};
    char *string = “Hello client\n”;

    signal (SIGINT, interrupt_handler);
    signal (SIGTERM, interrupt_handler);

    /* Part 1: create the server socket */

    if (0 > (sock_udp_server = socket(AF_INET, SOCK_DGRAM, 0))) {
        printf(“Unable to create a socket\n”);
        exit(0);
    }

    /* part 2: fill the structure and bind to socket */

    sock_addr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
    if (sock_addr == NULL) {
        printf(“unable to allocate memory\n”);
        goto end;
    }

    sock_addr->sin_family = AF_INET;
    sock_addr->sin_port = htons(UDPPORT);
    sock_addr->sin_addr.s_addr = inet_addr(“127.0.0.1”);

    if (0 != (bind(sock_udp_server, (struct sockaddr *)sock_addr, sizeof(struct             sockaddr_in)))) {
        printf(“unable to bind the socket\n”);
        goto end2;
    }

    /* part 3: read and write data */

    client_addr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
    if (client_addr == NULL) {
        printf(“Unable to allocate memory to client address socket\n”);
        goto end2;
    }

    while (1) {
        memset(buffer, 0, sizeof(buffer));
        num_of_bytes = recvfrom(sock_udp_server, buffer, sizeof(buffer), 0,                                                               (struct sockaddr *)client_addr, &length);
        if (num_of_bytes == -1) {
            printf(“unable to read from client\n”);
            goto end1;
       }

    printf(“%s\n”, buffer);
    memset(buffer, 0, sizeof(buffer));
    snprintf(buffer, (strlen(string)+1), “%s”, string);
    num_of_bytes = sendto(sock_udp_server, buffer, sizeof(buffer), 0, (struct                                                   sockaddr *)client_addr, length );
    if (num_of_bytes == -1) {
        printf(“unable to send Message to the client\n”);
        goto end1;
        }

    }
end1:
    free(client_addr);
end2:
    free(sock_addr);
end:
    close(sock_udp_server);

    return 0;
}

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

int sock_fd;
struct sockaddr_in *server_addr = NULL;
#define PORT 55555

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

int main () {
    int length, number_of_bytes;
    char buffer[50];
    char *string = “Hello from client”;

    signal (SIGINT, interrupt_handler);
    signal (SIGTERM, interrupt_handler);

    /* Part 1 – create socket */
    sock_fd = socket(AF_INET, SOCK_DGRAM, 0);

    if (sock_fd < 0) {
        printf(“unable to create socket\n”);
        exit(0);
    }

    /* Part 2 – fill the server structure for the specific interface */
    server_addr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
    if (server_addr == NULL) {
        printf(“Unable to allocate memory\n”);
        goto end;
    }

    server_addr->sin_family = AF_INET;
    server_addr->sin_addr.s_addr = inet_addr(“127.0.0.1”);
    server_addr->sin_port = htons(PORT);

    /* Part 3 – read and write data */
    while (1) {
        memset(buffer, 0, sizeof(buffer));
        snprintf(buffer, (strlen(string)+1), “%s”, string);
        if (sendto(sock_fd, buffer, sizeof(buffer),
            0, (struct sockaddr *)server_addr, sizeof(struct sockaddr_in)) !=                        sizeof(buffer)) {
            printf(“send to client failed\n”);
            goto end1;
        }

    memset(buffer, 0, sizeof(buffer));
    printf(“waiting to read from port\n”);

    number_of_bytes = recvfrom(sock_fd, buffer, sizeof(buffer),
                                                          0, NULL, NULL);

    if (number_of_bytes == -1) {
         printf(“Read failed\n”);
         goto end1;
    }
        printf(“%s\n”, buffer);
    }

end1:
    free(server_addr);
end:
    close(sock_fd);
return 0;
}

Analyzing the UDP socket code

Comments

  1. Pingback: A UDP Socket connection | Hitch Hiker's Guide to Learning

  2. Pingback: Analyzing the UDP Socket connection | Hitch Hiker's Guide to Learning

Leave a Reply

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