clinuxunixunix-socket

read() seems to block, even though O_NONBLOCK flag is passed


I am trying to write a chat program using UNIX Sockets. It is supposed to work similar to nc -U '/tmp/...' -l. Currently I am only testing a unidirectional approach, but can't seem to get it working.

My problem right now is, that the read for the server-socket is (apparently) blocking, and I don't know why. Just running the server-socket code should print out a infinite "One loop"-loop, but when you actually do start it, it just blocks at read.

Any idea why?

Server-socket:

// standard includes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// includes for socket()
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
// include for read()
#include <unistd.h>
// include for fnctl() (non-block)
#include <fcntl.h>

int main() {
  // variables
  int ret;
  int server_socket;
  struct sockaddr_un server_addr;
  char buffer[16]; //16byte transmission
  size_t byteSize;

  // create server_socket
  server_socket = socket(AF_UNIX, SOCK_DGRAM, 0);

  // make server socket non-blocking
    int status = fcntl(server_socket, F_SETFL, fcntl(server_socket, F_GETFL, 0) | O_NONBLOCK);

    if (status == -1){
        perror("calling fcntl");
    }

  // bind server_socket
  server_addr.sun_family = AF_UNIX;
  strcpy(server_addr.sun_path, "/tmp/bidirec.socket");
  ret =
      bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr));
  if (ret == -1) {
    perror("bind");
    exit(EXIT_FAILURE);
  }

  // masterloop
  while (1) {
        while(read(0, &buffer, 16) > 0) {
            write(server_socket, &buffer, 16);
            memset(&buffer, '0', sizeof(buffer));
            break;
        }
        printf("One loop\n");
    }
}

Client-socket:

// standard includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// includes for socket()
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
// include for write()
#include <unistd.h>

int main() {
  int ret;
  int client_socket;
  struct sockaddr_un client_addr;
  char buffer[16];

  // create client_socket
  client_socket = socket(AF_UNIX, SOCK_DGRAM, 0);

  // connect to server_socket
  client_addr.sun_family = AF_UNIX;
  strncpy(client_addr.sun_path, "/tmp/bidirec.socket",
          sizeof(client_addr.sun_path) - 1);
  ret = connect(client_socket, (struct sockaddr *)&client_addr,
                sizeof(client_addr));
  if (ret == -1) {
    fprintf(stderr, "The server is down.\n");
    exit(EXIT_FAILURE);
  }

  // masterloop
  while (1) {
      while(read(client_socket, &buffer, 16) > 0) {
            printf("%s", buffer);
            memset(&buffer, 0, sizeof(buffer));
            break;
        }
    }
}

Solution

  • Answer: Instead of making the socket unblocking, I should have made the fd for 'stdin' unblocking. The first read isn't depended on the actual socket.