1

我编写了一个非常小的 C 代码来打开一个 UDP 多播套接字,它在 32 位平台上运行良好,但是当我重新编译我的代码并在 linux 64 位平台上尝试它时它不起作用。该程序在 recvfrom() 函数上无限期挂起。我使用 tcpdump 检查了是否在指定的网络接口上实际收到了 udp 帧,但一切正常。有人知道我的代码有什么问题吗?

这是第一个代码(在您的评论之前):

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

static char* server = "231.180.0.1";
static char* network = "66.46.40.10";
static int port = 50001;

static struct sockaddr_in socketAddr;
static unsigned int socketDesc;

long toLong (unsigned char* msg, int offset);

int main (void) {
    struct ip_mreq mreq;
    int bindDesc, socketOptDesc;
    int reuse = 1;
    unsigned int socketLength = sizeof(socketAddr);

    // Allocation
    memset((char *) &socketAddr, 0, sizeof(socketAddr));
    memset(&mreq, 0, sizeof(struct ip_mreq));

    /*
     * Create a datagram socket on which to receive.
     */
    printf("# Init socket (server=%s  network=%s  port=%d)\n", server, network, port);
    socketDesc = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
    if (socketDesc < 0) {
        perror("socket() failed");
    } else {
        /*
         * Enable SO_REUSEADDR to allow multiple instances of this
         * application to receive copies of the multicast datagrams.
         */
        socketOptDesc = setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, (char *) &reuse, sizeof(reuse));
        if (socketOptDesc < 0) {
            perror("setsockopt() failed");
        } else {
            /*
             * Bind to the proper port number with the IP address
             * specified as INADDR_ANY.
             */
            socketAddr.sin_family = AF_INET;
            socketAddr.sin_port = htons(port);
            socketAddr.sin_addr.s_addr = INADDR_ANY;
            bindDesc = bind(socketDesc, (struct sockaddr*) &socketAddr, sizeof(socketAddr));
            if (bindDesc < 0) {
                perror("bind() failed");
            } else {

                /*
                 * Join the multicast group on the local interface.
                 * Note that this IP_ADD_MEMBERSHIP option must be
                 * called for each local interface over which the multicast
                 * datagrams are to be received.
                 */
                mreq.imr_multiaddr.s_addr = inet_addr(server);
                mreq.imr_interface.s_addr = inet_addr(network);
                socketOptDesc = setsockopt(socketDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mreq, sizeof(mreq));
                if (socketOptDesc < 0) {
                    perror("setsockopt() failed");
                } else {
                    printf("# Socket created successfully !\n");
                }
            }
        }
    }

    /*
     * Acquisition Loop
     */
    printf("# Starting reception loop...\n");
    long lastFrameNumber = -1;
    int nbDots = 0;
    while (1) {
        long frameNumber = -1;
        unsigned char buffer[65536];

        // Frame Acquisition
        int ret = recvfrom(socketDesc, buffer, 65536, 0, (struct sockaddr *) &socketAddr, &socketLength);
        if (ret < 0) {
            perror("recvfrom() failed");
        }
        // Reading frame number
        frameNumber = toLong(buffer, 28);

        if (frameNumber < 0) {
            // Context Frame
        } else if (frameNumber == 0) {
            printf("Invalid frame (frameNumber=0)\n");
        } else {
            if (frameNumber > 1 && frameNumber != (lastFrameNumber + 1)) {
                printf("%ld frame(s) lost from frame %ld\n", frameNumber - lastFrameNumber - 1, lastFrameNumber + 1);
            }
        }
        lastFrameNumber = frameNumber;

        if (frameNumber == 1) {
            if (nbDots > 50) {
                printf(".\n");
                nbDots = 0;
            } else {
                printf(".");
                fflush(stdout);
            }
            nbDots++;
        }
    }
    return EXIT_SUCCESS;
}

/* ======================================================================
 * Read 4 bytes from the specified offset and convert it to a long value.
 *
 * @input msg
 *          Byte array representing the message.
 * @input offset
 *          Byte offset.
 * @return
 *          Long value representing the frame number.
 * ====================================================================*/
long toLong (unsigned char* msg, int offset) {
    long value;
    int byte0; // bits 31..24
    int byte1; // bits 23..16
    int byte2; // bits 15..8
    int byte3; // bits 7..0
    byte0 = (0x000000FF & ((int) msg[offset + 0]));
    byte1 = (0x000000FF & ((int) msg[offset + 1]));
    byte2 = (0x000000FF & ((int) msg[offset + 2]));
    byte3 = (0x000000FF & ((int) msg[offset + 3]));
    value = ((long) (byte0 << 24 | byte1 << 16 | byte2 << 8 | byte3)) & 0xFFFFFFFFL;
    return value;
}

编辑:我用你的评论更新了我的代码,但它也不起作用:(另外,我忘了说网络正在使用 VLAN。网络接口是 66.46.40.100 的 eth.40,但它在 32 位平台上工作所以这可能不是问题。

这是新代码:

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

static char* server = "231.180.0.1";
static char* network = "66.46.40.100";
static uint16_t port = 50001;

long toLong (unsigned char* msg, int offset);

int main (void) {
    struct sockaddr_in socketAddr;
    struct ip_mreq mreq;
    int bindDesc, socketDesc, socketOptDesc;
    socklen_t reuse = 1;
    socklen_t socketLength = sizeof(socketAddr);

    // Allocation
    memset((char *) &socketAddr, 0, sizeof(socketAddr));
    memset(&mreq, 0, sizeof(struct ip_mreq));

    /*
     * Create a datagram socket on which to receive.
     */
    printf("# Init socket (server=%s  network=%s  port=%d)\n", server, network, port);
    socketDesc = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
    if (socketDesc < 0) {
        perror("socket() failed");
    } else {
        /*
         * Enable SO_REUSEADDR to allow multiple instances of this
         * application to receive copies of the multicast datagrams.
         */
        socketOptDesc = setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, (void *) &reuse, sizeof(reuse));
        if (socketOptDesc < 0) {
            perror("setsockopt() failed");
        } else {
            /*
             * Bind to the proper port number with the IP address
             * specified as INADDR_ANY.
             */
            socketAddr.sin_family = AF_INET;
            socketAddr.sin_port = htons(port);
            socketAddr.sin_addr.s_addr = INADDR_ANY;
            bindDesc = bind(socketDesc, (struct sockaddr*) &socketAddr, sizeof(socketAddr));
            if (bindDesc < 0) {
                perror("bind() failed");
            } else {

                /*
                 * Join the multicast group on the local interface.
                 * Note that this IP_ADD_MEMBERSHIP option must be
                 * called for each local interface over which the multicast
                 * datagrams are to be received.
                 */
                mreq.imr_multiaddr.s_addr = inet_addr(server);
                mreq.imr_interface.s_addr = inet_addr(network);
                socketOptDesc = setsockopt(socketDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mreq, sizeof(mreq));
                if (socketOptDesc < 0) {
                    perror("setsockopt() failed");
                } else {
                    printf("# Socket created successfully !\n");
                }
            }
        }
    }

    /*
     * Acquisition Loop
     */
    printf("# Starting reception loop...\n");
    long lastFrameNumber = -1;
    int nbDots = 0;
    while (1) {
        unsigned char buffer[65536];

        // Frame Acquisition
        ssize_t ret = recvfrom(socketDesc, buffer, 65536, 0, (struct sockaddr *) &socketAddr, &socketLength);
        if (ret < 0) {
            perror("recvfrom() failed");
        } else {
            printf("# Receiving frame\n");
        }
    }
    return EXIT_SUCCESS;
}
4

1 回答 1

1

您在代码中遇到的一个明显的 64 位问题是 recvfrom 的最后一个参数应该是指向 a 的指针,socklen_t而不是unsigned int您的代码中的指针。socklen_t在 64 位机器上很可能是 64 位变量,而unsigned int很可能是 32 位。

另一个问题是 socketDesc 未签名。文件描述符总是被签名的,这样你就可以从返回它们的函数中实际检测到错误。您对所有函数的错误检查将不起作用,因此您的代码可能更早失败并且您没有注意到。

另一个可能的问题是您的toLong函数,long通常是 64 位平台上的 64 位,而您将其视为 32 位值。

尝试使用警告构建,编译器应该很有帮助。这绝对是您的编译器会警告您的事情。如果这没有帮助,请仔细检查您调用的所有函数的手册页,并检查类型是否正确。

于 2015-01-26T09:29:23.160 回答