The Connected UDP Socket Code Example

Some interesting aspects of the code for an UDP connected socket code are as below

  • Both bind to their respective endpoints
  • Both endpoints invoke connect command to connect to one another
  • both know the endpoint that they are going to connect to

The client port chosen in the sample code is 50001 and the server port is 50000. The Server IP address is 192.168.0.11 and the client IP address is 192.168.0.14.

The code is provided below. The code nomenclature is still as client and server, but it is more appropriate to probably name it as endpoint 1 and endpoint 2 code.

Endpoint 1 CodeEndpoint 2 code

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

struct sockaddr_in *servaddr = NULL, *client_addr = NULL;
int sock_fd;

#define PORT 50000
#define PORT_CLIENT 50001

int main () {
        char buffer[50];
        int length;
        char *string = “Hello client”;
        int num_of_bytes;
        int error;

        /* Part 1: create the socket */
        sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
        if(0 > sock_fd) {
                printf(“unable to create socket\n”);
                exit(0);
        }

        servaddr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
        if (servaddr == NULL) {
                printf(“could not allocate memory\n”);
                goto end;
        }

        servaddr->sin_family = AF_INET;
        servaddr->sin_port = PORT;
        servaddr->sin_addr.s_addr = inet_addr(“192.168.0.11”);

        /* Part 2 – fill data structure and bind to socket */
        if (0 != (bind(sock_fd, (struct sockaddr *)servaddr, sizeof(struct sockaddr_in)))) {
                printf(“could not bind server socket to address\n”);
                goto end1;
        }

        /* 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;
        }

        client_addr->sin_family = AF_INET;
        client_addr->sin_port = PORT_CLIENT;
        client_addr->sin_addr.s_addr = inet_addr(“192.168.0.14”);

        error = connect(sock_fd, (struct sockaddr *)client_addr, sizeof(struct sockaddr_in));
        if (error != 0) {
                printf(“error %d”, errno);
                printf(“connect returned error\n”);
                goto end2;
        }

        memset(buffer, 0, sizeof(buffer));
        read(sock_fd, buffer, sizeof(buffer));
        printf(“%s\n”, buffer);

        memset(buffer, 0, sizeof(buffer));
        snprintf(buffer, strlen(string)+1, “%s”, string);
        write(sock_fd, buffer, sizeof(buffer));

end2:
        free(client_addr);
end1:
        free(servaddr);
end:
        close(sock_fd);

        return 0;
}

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

struct sockaddr_in *serveraddr = NULL, *clientaddr;
int sockfd;

#define PORT 50001
#define SERVER_PORT 50000

int main () {
        char buffer[50];
        int length;
        char *string = “Hello server”;
        int error;

        /* Part 1: create the socket */
        sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        if(0 > sockfd) {
                printf(“unable to create socket\n”);
                exit(0);
        }

        clientaddr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
        if (clientaddr == NULL) {
                printf(“could not allocate memory\n”);
                goto end;
        }

        clientaddr->sin_family = AF_INET;
        clientaddr->sin_port = PORT;
        clientaddr->sin_addr.s_addr = inet_addr(“192.168.0.14”);

        /* Part 2 – fill data structure and bind to socket */
        if (0 != (bind(sockfd, (struct sockaddr *)clientaddr, sizeof(struct sockaddr_in)))) {
                printf(“could not bind server socket to address\n”);
                goto end1;
        }

        /* part 3: read and write data */
        serveraddr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
        if (serveraddr == NULL) {
                printf(“Unable to allocate memory to server address socket\n”);
                goto end2;
        }
        serveraddr->sin_family = AF_INET;
        serveraddr->sin_port = SERVER_PORT;
        serveraddr->sin_addr.s_addr = inet_addr(“192.168.0.11”);

        error = connect(sockfd, (struct sockaddr *)serveraddr, sizeof(struct sockaddr_in));
        if (error != 0) {
                printf(“error %d”, errno);
                printf(“connect returned error\n”);
        }
        memset(buffer, 0, sizeof(buffer));
        snprintf(buffer, strlen(string)+1, “%s”, string);
        write(sockfd, buffer, sizeof(buffer));

        memset(buffer, 0, sizeof(buffer));
        read(sockfd, buffer, sizeof(buffer));

        printf(“%s\n”, buffer);

end2:
        free(serveraddr);
end1:
        free(clientaddr);
end:
        close(sockfd);

        return 0;
}

Analyzing the Connected UDP socket code example

Comments

  1. Pingback: The Connected UDP socket code – setup | Hitch Hiker's Guide to Learning

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

Leave a Reply

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