UDP socket client not able to receive data

42 Views Asked by At

I am playing with simple examples of UDP sockets server and client code.

Sharing my code as below :

//server.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>

#define SOCKET_PATH "/tmp/my_socket"

int main() {
    int server_fd;
    struct sockaddr_un server_addr;
    int value = 0; // Initial integer value to send

    // Remove existing socket file if present
    unlink(SOCKET_PATH);

    // Create socket
    if ((server_fd = socket(AF_LOCAL, SOCK_DGRAM, 0)) == -1) {
        perror("socket");
        exit(EXIT_FAILURE);
    }

    // Set up server address
    memset(&server_addr, 0, sizeof(struct sockaddr_un));
    server_addr.sun_family = AF_LOCAL;
    strncpy(server_addr.sun_path, SOCKET_PATH, sizeof(server_addr.sun_path) - 1);

    // Bind socket
    if (bind(server_fd, (struct sockaddr*)&server_addr, sizeof(struct sockaddr_un)) == -1) {
        perror("bind");
        exit(EXIT_FAILURE);
    }

    printf("Server started. Writing integer values to socket continuously...\n");

    // Continuously write integer values to socket
    while (1) {
        // Print the integer value
        printf("Sending value to client: %d\n", value);

        // Convert integer value to string
        char buffer[32];
        snprintf(buffer, sizeof(buffer), "%d", value);

        // Send integer value to client
        if (sendto(server_fd, buffer, strlen(buffer), 0, (struct sockaddr*)&server_addr, sizeof(struct sockaddr_un)) == -1) {
            perror("sendto");
        }

        // Increment value for the next iteration
        value++;

        // Sleep for a short duration (e.g., 1 second)
        sleep(1);
    }

    // Close server socket (not reachable in this example)
    close(server_fd);

    return 0;
}

Which will send data continuously to a socket.

I think it works as expected, I am getting following output :

Server started. Writing integer values to socket continuously...
Sending value to client: 0
Sending value to client: 1
Sending value to client: 2
...

Now I tried to make a client code which should be able to receive data from server, as below :

//client.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <fcntl.h>

#define SOCKET_PATH "/tmp/my_socket"

int main() {
    int client_fd;
    struct sockaddr_un server_addr;
    char buffer[1024];

    // Create socket
    if ((client_fd = socket(AF_LOCAL, SOCK_DGRAM, 0)) == -1) {
        perror("socket");
        exit(EXIT_FAILURE);
    }

    // Set socket to non-blocking mode
    if (fcntl(client_fd, F_SETFL, O_NONBLOCK) == -1) {
        perror("fcntl");
        exit(EXIT_FAILURE);
    }

    // Set up server address
    memset(&server_addr, 0, sizeof(struct sockaddr_un));
    server_addr.sun_family = AF_LOCAL;
    strncpy(server_addr.sun_path, SOCKET_PATH, sizeof(server_addr.sun_path) - 1);

    printf("Client connected to server. Reading integer values...\n");

    // Continuously read integer values from server
    while (1) {
        // Receive integer value from server
        ssize_t bytes_received = recv(client_fd, buffer, sizeof(buffer), 0);
        if (bytes_received == -1) {
            if (errno == EAGAIN || errno == EWOULDBLOCK) {
                // No data available, continue loop
                printf("No data available\n");
                continue;
            } else {
                perror("recv");
                exit(EXIT_FAILURE);
            }
        } else if (bytes_received == 0) {
            printf("Server closed the connection\n");
            break;
        }

        // Null-terminate the received data
        buffer[bytes_received] = '\0';

        // Print received data
        printf("Received data from server: %s\n", buffer);
    }

    // Close client socket
    close(client_fd);

    return 0;
}

But I am getting continuously, the following output :

Client connected to server. Reading integer values...
No data available
No data available
No data available
...

It seems client is not able to get data written by server.

I am running these programs in Ubuntu from two different terminals.

Could anyone please tell how to debug/solution?

0

There are 0 best solutions below