UDP - sendto() 错误:协议不支持地址系列

UDP - Error in sendto(): Address family not supported by protocol

以下代码是使用 UDP 套接字的编程示例。

有 3 个 .c 源文件(Client_UDP.cServer_UDP.cMessage_UDP.c)和 1 个 .h 头文件(Prototype.h).

我希望Server和Client只能通过Message_UDP中的函数相互通信。

事情的经过好像是这样的:

  1. 客户端发送第一个packet/message到服务器。
  2. 服务器从客户端接收数据包。
  3. 服务器尝试发送响应包,但显示错误:"Error in sendto(): Address family not supported by protocol"

有人知道为什么吗?我该如何解决?

Client_UDP.c

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

#include "Prototype.h"

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

    int fd_socket;

    struct sockaddr_in server;      
    socklen_t length_server = sizeof (server);

    fd_socket = socket(AF_INET, SOCK_DGRAM, 0);
    Error_A(fd_socket, "Error in socket()");

    server.sin_port = htons(8888);
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr("127.0.0.1");

    // send packet to server
    function_SEND(fd_socket, server, length_server, "CLIENT");

    // receive packet from server
    function_RECEIVE(fd_socket, server, length_server, "CLIENT");

    close(fd_socket);
    return EXIT_SUCCESS;
}

Server_UDP.c

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

#include "Prototype.h"

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

    int fd_socket;
    int rv; // return value

    struct sockaddr_in server;
    struct sockaddr_in client;

    socklen_t length_server = sizeof (server);
    socklen_t length_client = sizeof (client);

    fd_socket = socket(AF_INET, SOCK_DGRAM, 0);
    Error_A(fd_socket, "Errore in socket()");

    server.sin_port = htons(8888);
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;

    rv = bind(fd_socket, (struct sockaddr *) &server, length_server);
    Error_A(rv, "Error in bind()");

    // receive packet from client
    function_RECEIVE(fd_socket, client, length_client, "SERVER");

    // send packet to client
    function_SEND(fd_socket, client, length_client, "SERVER");

    close(fd_socket);
    return EXIT_SUCCESS;
}

Message_UDP.c

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

#include "Prototype.h"

void Error_A(int return_value, char *error_message) {

    if (return_value < 0) {
        perror(error_message);
        exit(EXIT_FAILURE);
    }
}

void function_SEND(int fd_socket, struct sockaddr_in host, int length_host, char *sender) {

    int rv; // return value
    char packet[100] = "PACKET";

    // send packet
    rv = sendto(fd_socket, packet, 100, 0, (struct sockaddr *) &host, length_host);
    Error_A(rv, "Error in sendto()");
    printf("<%s> -- package sent: <%s> \n", sender, packet);
}

void function_RECEIVE(int fd_socket, struct sockaddr_in host, int length_host, char* receiver) {

    int rv; // return value
    char packet[100] = "";

    // receive packet
    rv = recvfrom(fd_socket, packet, 100, 0, (struct sockaddr *) &host, &length_host);
    Error_A(rv, "Errore in recvfrom()");
    printf("<%s> -- packet received: <%s> \n", receiver, packet);
}

Prototype.h

void Error_A(int return_value, char *error_message);

void function_SEND(int fd_socket, struct sockaddr_in host, int length_host, char *sender);

void function_RECEIVE(int fd_socket, struct sockaddr_in host, int length_host, char* receiver);

这是客户端输出:

CLIENT -- package sent: PACKET

这是服务器输出:

SERVER -- package received: PACKET

Error in sendto(): Address family not supported by protocol

我可能遗漏了一些明显的东西。这是我注意到的。在您的 Server_UDP.c 中,client 没有初始化。 我看到声明 struct sockaddr_in client;,但是没有赋值。在这里,我假设客户端将被分配相同的端口号和环回 IP 地址,如下所示

client.sin_port = 8888;
client.sin_family = AF_INET;
client.sin_addr.s_addr = inet_addr("127.0.0.1");

这可能就是发送在服务器中失败的原因

我在Prototype.h中添加了这个结构:

struct Host {
    struct sockaddr_in host;
    socklen_t length_host;
} host_return;

void Error_A(int return_value, char *error_message);

void function_SEND(int fd_socket, struct sockaddr_in host, int length_host, char *sender);

struct Host function_RECEIVE(int fd_socket, struct sockaddr_in host, int length_host, char* receiver);

Message_UDP.c 中更改 function_RECEIVE()

struct Host function_RECEIVE(int fd_socket, struct sockaddr_in host, int length_host, char* receiver) {

    int rv; // return value
    char packet[100] = "";

    // receive packet
    rv = recvfrom(fd_socket, packet, 100, 0, (struct sockaddr *) &host, &length_host);
    Error_A(rv, "Errore in recvfrom()");
    printf("%s -- packet received: %s \n", receiver, packet);

    host_return.host = host;
    host_return.length_host = length_host;

    return host_return;
}

Server_UDP.c 中的代码:

// receive packet from client
host_return = function_RECEIVE(fd_socket, client, length_client, "SERVER");
    
// send packet to client
function_SEND(fd_socket, host_return.host, host_return.length_host,"SERVER");

而不是

// receive packet from client
function_RECEIVE(fd_socket, client, length_client, "SERVER");

// send packet to client
function_SEND(fd_socket, client, length_client, "SERVER");

它似乎有效,因为输出是(但我希望我没有运气):

CLIENT -- packet sent: PACKET

CLIENT -- packet received: PACKET

服务器:

SERVER -- packet received: PACKET

SERVER -- packet sent: PACKET