Unix网络编程,协议不支持的地址族

Unix network programming, Address family not supported by protocol

我正在尝试使用 Unix 套接字编写简单的程序,我的服务器程序似乎可以工作,但客户端(几乎相同)不能。 我收到错误 "Address family not supported by protocol"。

事实上,我不确定我是否可以按照我的方式使用它,即只是 运行 来自两个终端的那些程序,但我看不出这有什么问题。

库文件network.h:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>

#include <arpa/inet.h>
#include <netdb.h>


#define PORT_NUMBER_IN  14760
#define PORT_NUMBER_OUT 14761

typedef struct params_socket
{
        int domain_type; /*AF_UNIX lub AF_INET*/
        int link_type; /*SOCK_STREAM połączeniowy SOCK_DGRAM bezpoł. SOCK_RAW bezpośredni*/
        int protocol; /*zero - domyślny, inne w /etc/protocols*/
} params_socket;


typedef enum CONNECTION_TYPE
{
        CLIENT, SERVER
}CONNECTION_TYPE;


int create_socket(params_socket params);
int bind_socket(int my_socket, CONNECTION_TYPE connection_type);
int bind_socket_client(int my_socket);
int bind_socket_server(int my_socket);
struct sockaddr* get_address_structure_in();
struct sockaddr* get_address_structure_out();

int create_socket(params_socket params)
{
        int my_socket = socket(params.domain_type, params.link_type, params.protocol);
        if(0 > my_socket)
        {
                perror("socket");
        }
        return my_socket;
}

int bind_socket(int my_socket, CONNECTION_TYPE connection_type)
{
        int binded_socket = 1;
        struct sockaddr* address_structure;

        if(connection_type == SERVER)
        {
                address_structure = get_address_structure_in();
        }
        else if (connection_type == CLIENT)
        {
                address_structure = get_address_structure_out();
        }

        binded_socket = bind(my_socket, address_structure, sizeof(*address_structure));
        if(0 > binded_socket)
        {
                perror("bind");
        }

        return binded_socket;
}

int bind_socket_server(int my_socket)
{
        return bind_socket(my_socket, SERVER);
}

int bind_socket_client(int my_socket)
{
        return bind_socket(my_socket, CLIENT);
}


struct sockaddr* get_address_structure_in()
{

        struct sockaddr_in* address_structure = (struct sockaddr_in*) malloc(sizeof(struct sockaddr_in));

        bzero((char *) address_structure, sizeof(*address_structure));
        address_structure->sin_family      = AF_INET;
        address_structure->sin_addr.s_addr = htonl(INADDR_ANY);
        address_structure->sin_port        = htons(PORT_NUMBER_IN);

        return (struct sockaddr*) address_structure;
}

struct sockaddr* get_address_structure_out()
{

        struct sockaddr_in* address_structure = (struct sockaddr_in*) malloc(sizeof(struct sockaddr_in));

        bzero((char *) address_structure, sizeof(*address_structure));
        address_structure->sin_family      = AF_INET;
        address_structure->sin_addr.s_addr = inet_addr("127.0.0.1");
        address_structure->sin_port        = htons(PORT_NUMBER_OUT);

        return (struct sockaddr*) address_structure;
}

服务器文件(我猜是可以的,至少没有错误)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "network.h"


int main(int argc, char** argv)
{

    params_socket params = {AF_INET, SOCK_STREAM, 0};

    int my_socket;
    int client_socket;
    int binded_socket;

    struct sockaddr_in client_address;
    socklen_t address_size = sizeof(client_address);


    my_socket = create_socket(params);
    binded_socket = bind_socket_server(my_socket);
    printf("socket: %d\nbinded_socket: %d\n", my_socket, binded_socket);


    listen(my_socket, 5);
    client_socket = accept(my_socket, (struct sockaddr *) &client_address, &address_size);
    if(0 > client_socket)
    {
        perror("connection");
    }

    return 0;
}

client 文件,唯一的区别是我使用bind_socket_client而不是bind_socket_server,后来使用connect

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "network.h"


int main(int argc, char** argv)
{
    params_socket params = {AF_INET, SOCK_STREAM, 0};
    int my_socket;
    int server_socket;
    int binded_socket;

    struct sockaddr_in server_address;
    socklen_t address_size = sizeof(server_address);


    my_socket = create_socket(params);
    binded_socket = bind_socket_client(my_socket);
    printf("socket: %d\nbinded_socket: %d\n", my_socket, binded_socket);


    server_socket = connect(my_socket, (struct sockaddr *) &server_address, address_size);
    if(0 > server_socket)
    {
        perror("connection");
    }

    return 0;
}

问题是你没有初始化server_address,在连接之前在客户端添加这个

memset(&server_address, 0, sizeof(server_address));

server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
server_address.sin_port = htons(PORT_NUMBER_IN);