C套接字- UDP广播,无法发送消息

eivnm1vs  于 2023-01-25  发布在  其他
关注(0)|答案(1)|浏览(217)

这个程序应该从本地套接字(tcp)接收东西并广播它,反之亦然。

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

#define BUFFER_SIZE 1024
#define PORT_TCP 12312
#define PORT_UDP 5678

int main() {
    int tcp_sock, udp_sock, client_tcp_sock, bytes_received_udp, bytes_received_tcp;
    struct sockaddr_in server_tcp, client_tcp, server_udp, client_udp;
    socklen_t client_len;
    char buffer[BUFFER_SIZE];

    memset(buffer, 0, BUFFER_SIZE); // emptying buffer

    // Create TCP socket to talk with python game
    tcp_sock = socket(AF_INET, SOCK_STREAM, 0);
    if (tcp_sock < 0) {
        perror("Error creating TCP socket");
        return EXIT_FAILURE;
    }

    // Create UDP socket for broadcasting
    udp_sock = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
    if (udp_sock < 0) {
        perror("Error creating UDP socket");
        return EXIT_FAILURE;
    }

    //enable le broadcast ?
    socklen_t broadcastEnable = 1;
    if(setsockopt(udp_sock, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable)) < 0)
    {
        perror("Reusing ADDR failed()");
        exit(2);
    }

    // Bind TCP socket to server address
    memset((char *) &server_tcp, 0, sizeof(server_tcp));
    server_tcp.sin_family = AF_INET;
    server_tcp.sin_port = htons(PORT_TCP);
    server_tcp.sin_addr.s_addr = INADDR_ANY;
    if (bind(tcp_sock, (struct sockaddr *) &server_tcp, sizeof(server_tcp)) < 0) {
        perror("Error binding TCP socket to server address");
        return EXIT_FAILURE;
    }

    // Bind UDP socket to server address
    memset((char *) &server_udp, 0, sizeof(server_udp));
    server_udp.sin_family = AF_INET;
    server_udp.sin_port = htons(PORT_UDP);
    server_udp.sin_addr.s_addr = INADDR_ANY;
    if (bind(udp_sock, (struct sockaddr *) &server_udp, sizeof(server_udp)) < 0) {
        perror("Error binding UDP socket to server address");
        return EXIT_FAILURE;
    }

    // Listen for incoming connections on TCP socket
    if (listen(tcp_sock, 5) < 0) {
        perror("Error listening for incoming connections on TCP socket");
        return EXIT_FAILURE;
    } else {
        printf("Listening for incoming connections on TCP socket (port %i).\n", PORT_TCP);
    }

    // Accept incoming connection on TCP socket
    client_len = sizeof(client_tcp);
    client_tcp_sock = accept(tcp_sock, (struct sockaddr *) &client_tcp, &client_len);
    if (client_tcp_sock < 0) {
        perror("Error accepting incoming connection on TCP socket");
        return EXIT_FAILURE;
    } else {
        printf("Accepted connection on TCP socket !\n");
    }

    while (1) {
        // Receive data from UDP socket and send to TCP socket
        bytes_received_udp = recvfrom(udp_sock, buffer, sizeof(buffer), 0, (struct sockaddr *) &client_udp, &client_len);
        if (bytes_received_udp > 0) {
            printf("Data received from UDP : %s\n", buffer);

            if (send(client_tcp_sock, buffer, bytes_received_udp, 0) < 0) {
                perror("Error sending data to TCP socket");
                return EXIT_FAILURE;
            } else {
                printf("Sent data to TCP socket.\n");
                //memset(buffer, 0, BUFFER_SIZE); // emptying buffer
            }
        }

        // Receive data from TCP socket and broadcast via UDP socket
        bytes_received_tcp = recv(client_tcp_sock, buffer, sizeof(buffer), MSG_DONTWAIT);
        
        if (bytes_received_tcp == 0) { // connection closed
            printf("TCP connection closed !\n");
            close(client_tcp_sock);
            return EXIT_FAILURE;
        } else if (bytes_received_tcp > 0) {
            printf("Data received from TCP : %s\n", buffer);

            if (sendto(udp_sock, buffer, bytes_received_tcp, 0, (struct sockaddr *) &server_udp, sizeof(server_udp)) < 0) {
                perror("Error sending data to UDP socket");
                return EXIT_FAILURE;
            } else {
                printf("Sent data to UDP socket.\n");
                //memset(buffer, 0, BUFFER_SIZE); // emptying buffer
            }
        }
    }

    // Close sockets
    close(tcp_sock);
    close(udp_sock);

    return EXIT_SUCCESS;
}

当我用netcat(nc localhost 12312)测试它时,它看起来像是在发送消息(当我收到它们时),但它们并没有真正被广播(也用netcat测试过,其他机器没有收到)。
另一种方式工作正常(nc-bu www.example.com5678),消息被发送到本地套接字,甚至在另一台机器上。255.255.255.255 5678), messages are sent to the local socket, even on another machine.
我哪里做错了?

dgtucam1

dgtucam11#

UDP套接字没有发送广播数据包,因为您没有发送到广播地址。
SO_BROADCAST选项不会自动广播所有的输出数据包,它只是让你发送广播。
如果要发送到广播地址,则需要显式地执行此操作。

struct sockaddr_in udp_broadcast;
memset(&udp_broadcast, 0, sizeof(udp_broadcast));
udp_broadcast.sin_family = AF_INET;
udp_broadcast.sin_port = htons(PORT_UDP);
udp_broadcast.sin_addr.s_addr = INADDR_BROADCAST;   // same as 255.255.255.255

...

if (sendto(udp_sock, buffer, bytes_received_tcp, 0, 
           (struct sockaddr *) &udp_broadcast, sizeof(udp_broadcast)) < 0) {
...

相关问题