简体   繁体   English

C ++套接字不断接收相同的数据

[英]C++ socket keeps receiving the same data

I am using this code to receive data from a sensor through sockets. 我正在使用此代码通过套接字从传感器接收数据。 The problem is that I keep receiving the same output for every iteration of the for loop. 问题是,对于for循环的每次迭代,我都会不断收到相同的输出。 However I receive a different number for every time I run the code but again, the same number keeps repeating. 但是,每次运行代码时,我都会收到一个不同的数字,但是同样,这个数字会不断重复。 The sensor should send different every time data but thats not the case here. 传感器每次发送的数据均应不同,但实际情况并非如此。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "port.h"

#define BUFSIZE 2048

int
main(int argc, char **argv)
{
    struct sockaddr_in myaddr;  /* our address */
    struct sockaddr_in remaddr; /* remote address */
    socklen_t addrlen = sizeof(remaddr);        /* length of addresses */
    int recvlen;            /* # bytes received */
    int fd;             /* our socket */
    int msgcnt = 0;         /* count # of messages we received */
    unsigned char buf[BUFSIZE]; /* receive buffer */


    /* create a UDP socket */

    if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("cannot create socket\n");
        return 0;
    }

    /* bind the socket to any valid IP address and a specific port */

    memset((char *)&myaddr, 0, sizeof(myaddr));
    myaddr.sin_family = AF_INET;
    myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    myaddr.sin_port = htons(SERVICE_PORT);

    if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
        perror("bind failed");
        return 0;
    }

    /* now loop, receiving data and printing what we received */

    printf("waiting on port %d\n", SERVICE_PORT);
        printf("%s \n \n", "We recieve 10 packets just to confirm the communication");


    recvlen = recvfrom(fd, buf, BUFSIZE, 0, (struct sockaddr *)&remaddr, &addrlen);
        if (recvlen > 0) {
            buf[recvlen] = 0;
            printf("received message: \"%u\" (%d bytes)\n", buf, recvlen);
        }
        else
            printf("uh oh - something went wrong!\n");
        sprintf(buf, "ack %d", msgcnt++);
        printf("sending response \"%u\"\n", buf);
        if (sendto(fd, buf, strlen(buf), 0, (struct sockaddr *)&remaddr, addrlen) < 0)
            perror("sendto");

    int temp = recvlen;

    for (;;) {

    recvlen = recvfrom(fd, buf, BUFSIZE, 0, (struct sockaddr *)&remaddr, &addrlen);
    if (recvlen > 0) {
            buf[recvlen] = 0;
            printf("received message: \"%u\" (%d bytes)\n", buf, recvlen);

    }



}
}

Edit: Here is the output when i ran the code two seperate times: trial run and trial run 2 编辑:这是我分别运行两次代码时的输出: 试运行试运行2

I believe the problem isn't with your networking code but rather with your printf() calls: 我相信问题不在于您的网络代码,而在于您的printf()调用:

        printf("received message: \"%u\" (%d bytes)\n", buf, recvlen);

You are specifying %u to print out the contents of buf , but buf is a char array (not an unsigned integer), so you probably want to be using %s instead. 您正在指定%u打印出buf的内容,但是buf是一个char数组(不是无符号整数),因此您可能想使用%s代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM