При попытке запустить сервер -unix выдается error: socket bind failed. А с IP-сокетом все окей. Что я не так делаю с unix-сокетом?

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/un.h>


int PORT = 4356;
char PATH[128] = "/home/k.../Documents/lr5-6/socket";
int MESSAGE_SIZE = 256;


int main(int argc, char *argv[]) {
    // Выбор типа сокета с помощью аргументов командной строки:
    // -unix - использовать unix-socket
    // -ip - использовать ip-socket
    char socket_type;
    if (argc != 2) {
        printf("Incorrect args. Use [name] [-unix/-ip]\n");
        return 1;
    }
    else if (strcmp(argv[1], "-ip") == 0) socket_type = 'i';
    else if (strcmp(argv[1], "-unix") == 0) socket_type = 'u';
    else {
        printf("Incorrect args. Use [name] [-unix/-ip]\n");
        return 1;
    }


    // Объявление необходимых переменных
    int listener_, bind_, sock, bytes_read;
    struct sockaddr_in addr_i;
    struct sockaddr_un addr_u;
    char message[MESSAGE_SIZE];
    char login[MESSAGE_SIZE];


    // Создание серверного сокета
    if (socket_type == 'i') listener_ = socket(AF_INET, SOCK_STREAM, 0);
    else if (socket_type == 'u') listener_= socket(AF_UNIX, SOCK_STREAM, 0);
    else {
        printf("Error: incorrect socket_type.\n");
        return 1;
    }
    if (listener_ < 0) {
        printf("Error: socket failed.\n");
        return 1;
    } else if (socket_type == 'i') {
        addr_i.sin_family = AF_INET;
        addr_i.sin_port = htons(PORT);
        addr_i.sin_addr.s_addr = htonl(INADDR_ANY);
        bind_ = bind(listener_, (struct sockaddr*)&addr_i, sizeof(addr_i));
    } else {
        addr_u.sun_family = AF_UNIX;
        strcpy(addr_u.sun_path, PATH);
        unlink(PATH);
        bind_ = bind(listener_, (struct sockaddr*)&addr_u, sizeof(addr_u));
    }


    // Запуск сервера
    if (bind_ < 0) {
        printf("Error: socket bind failed.\n");
        return 1;      
    } else {
        listen(listener_, 1);
        printf("Server started\n");
    }


    // Ожидание подключения
    sock = accept(listener_, NULL, NULL);
        if (sock < 0) {
            printf("Error: accept failed.\n");
            return 1;     
        }


    // Цикл обмена сообщениями, пока не получена команда -exit
    int exit = 0;
    int is_identified = 0;
    while(!exit) {
        // Первым сообщением сервер принимает логин для идентификации
        if (!is_identified) {
            send(sock, "Server: enter your login: ", 30, 0);
            bytes_read = recv(sock, login, MESSAGE_SIZE, 0);
            login[strlen(login)-1] = 0;
            if (bytes_read > 0)
            {
                printf("Server: %s has connected.\n", login);
                char welcome[512] = "Server: Hello, ";
                strcat(welcome, login);
                strcat(welcome, "!\n");
                send(sock, welcome, MESSAGE_SIZE + 16, 0);
                is_identified = 1;
            }
        }
        bytes_read = recv(sock, message, MESSAGE_SIZE, 0);
        if (bytes_read > 0) {
            printf("%s: %s", login, message);
        }
        if (strncmp(message, "-exit", 5) == 0) {
            printf("Server stoopped.\n");
            exit = 1;
            close(sock);
        }       
    }
    close(listener_);
    return 0;
}

Ответы (0 шт):