【问题标题】:Not receiving UDP and outputting data in multi-threaded program在多线程程序中不接收UDP并输出数据
【发布时间】:2014-05-22 07:38:10
【问题描述】:
uint64_t GetTimeStamp()
{
    struct timespec start;

    if ((clock_gettime( CLOCK_MONOTONIC, &start)) == -1)
    {
        perror("clock gettime\n");
    }

    return(start.tv_sec + start.tv_nsec * 1e-9);                // seconds
}

const struct sigevent *intHandler(void *arg, int id)
{
    start_clock = ClockCycles();
    // printf("start clock: %lld\n", start_clock);

    return(&event);
}

void *ConfigureISR()
{
    // Get IO privilege
    ThreadCtl( _NTO_TCTL_IO, 0 );
    // Setup COID and event
    SIGEV_INTR_INIT( &event);

    interruptID = InterruptAttach(intrNum, intHandler, NULL, 0, 0);
    if (interruptID == -1)
    {
        fprintf(stderr, "can't attach to IRQ %d\n", intrNum );
        perror(NULL);
        exit(EXIT_FAILURE);
    }

    while (loop)
    {
        InterruptWait(0, NULL);
        end_clock = ClockCycles();
        InterruptLatency = (uint32) ((end_clock - start_clock) * 1000000 / (SYSPAGE_ENTRY(qtime)->cycles_per_sec));
        printf("Current Interrupt Latency microseconds: %ld\n", InterruptLatency);
        InterruptUnmask(intrNum, interruptID);
        measurements[17] = InterruptLatency;
    }
    return(EXIT_SUCCESS);
}

int CreateSocket()
{
    // pthread_attr_t attr;
    // Socket creation for UDP

    acceptSocket = socket(AF_INET, SOCK_DGRAM, 0);

    if (acceptSocket == -1)
    {
        printf("Failure: socket creation is failed, failure code\n");
        return 1;
    }
    else
    {
        printf("Socket started!\n");
    }

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

    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);

    rc = bind(acceptSocket, (struct sockaddr *)&addr, sizeof(addr));

    fcntl(acceptSocket, O_NONBLOCK);

    if (rc == -1)
    {
        printf("Oh dear, something went wrong with bind()! %s\n", strerror(errno));
        return -1;
    }
    else
    {
        printf("Socket an port %d \n", port);
    }
    return acceptSocket;
}

int main(int argc, char *argv[])
{
    pthread_t thread_id, thread_id1;

    Xcp_Initialize();

    pthread_create(&thread_id1, NULL, &ConfigureISR, NULL);

    if ((sock = CreateSocket()) < 0)
    {
        perror("Create_socket");
        exit(1);
    }

    if (pthread_create(&thread_id, NULL, &rastertask, NULL))
    {
        perror("pthread_create");
        exit(1);
    }

    do
    {
        socklen_t len;
        len = sizeof(client);
        printf("NEW DATA ARRIVED\n");
        // non blocking mode : MSG_DONTWAIT
        rc = recvfrom(sock, buf, 256, 0, (struct sockaddr *) &client, &len);
        Receive = GetTimeStamp();
        receiveTime = (uint32) (Receive / 1000000);
        printf("Receive time: %lu\n", receiveTime);
        //  printf("RECEIVE from Time in microseconds: %lu\n",  ReceiveTimestamp);
        // measurements[19] = ReceiveTimestamp;
        if (rc == 0)
        {
            printf("Server has no connection..\n");
            loop = 0;
            break;
        }
        if (rc == -1)
        {
            if (errno == SIGINT)
                continue;
            printf("Oh dear, something went wrong with read()! s\n", strerror(errno));
            loop = 0;
            break;
        }

        XcpIp_RxCallback( (uint16) rc, (uint8 *) buf, (uint16) port );
    } while (1);


    close(sock);

    return 0;
}

上面是一个服务器代码,通过ip地址和端口号接收数据。

稍后将数据发送回客户端。我正在接收数据,但在接收到数据后,取时间戳(您可以在上面的代码中看到:Send= GetTimeStamp())。

为什么不打印任何东西?我通过套接字接收数据,我给了printf("new data arrived\n");,然后它也没有打印。我也没有收到任何时间!有人可以告诉我可能是什么原因吗?

【问题讨论】:

  • 在主函数中接收到数据后:它没有打印为接收到的数据。有点奇怪:(
  • format 你的代码(使用适当的缩进),remove 不相关的部分,拿出一个最小的例子。

标签: c linux multithreading udp qnx


【解决方案1】:

您的GetTimeStamp() 函数返回uint64_t;返回语句是:

return (start.tv_sec + start.tv_nsec * 1e-9);

这会创建一个 double 值(1e-9 是一个 double 常量),然后将其截断为整数,就像您返回了一样:

return (start.tv_sec);

请注意,这会为您提供以秒为单位的时间(从 Epoch — 1970-01-01 00:00:00 +00:00 开始)。

也许你想要:

return (start.tv_sec * 1E9 + start.tv_nsec);

这为您提供了自 Epoch 以来的总纳秒数。这可能与您遇到的其他问题无关,但没有发送合理的值意味着您将无法接收到合理的值,因此您应该确定您期望从GetTimeStamp() 获得什么值。例如:

    Receive = GetTimeStamp();
    receiveTime = (uint32) (Receive / 1000000);
    printf("Receive time: %lu\n", receiveTime);

这将打印一个值,例如 1400(因为当前时间戳约为 1400755363 = 2014-05-22 10:42:43),因此该值除以 100 万就是 1400。

【讨论】:

  • 那是几秒钟吗??
  • 为什么收到数据后不打印!!我可以在调试模式下看到数据,但它没有打印任何东西:(
猜你喜欢
  • 2013-03-19
  • 2017-07-05
  • 2019-02-20
  • 1970-01-01
  • 2018-05-09
  • 2013-03-23
  • 2018-02-27
  • 1970-01-01
  • 2012-12-08
相关资源
最近更新 更多