使用c ++套接字时无法接收多条消息

时间:2018-01-25 06:57:12

标签: c++ sockets

我附上了以下代码,标题说明了一切。我能够收到一条消息,然后在第一封消息发送或未读入后发送所有其他消息。

客户端,send_msg被多次调用,cppClientSocket是建立连接的构造函数。

void cppClientSocket::send_msg(const char msg[], int size){
    sendto(sockfd ,msg, size , 0, (struct sockaddr *)&serv, sizeof(serv));
}

cppClientSocket::cppClientSocket(int port){
    sockfd = socket(AF_INET,SOCK_DGRAM,0);

    serv.sin_family = AF_INET;
    serv.sin_port = htons(port);
    serv.sin_addr.s_addr = inet_addr("127.0.0.1");
};

服务器,cppServerSocket用于构造UDP套接字的另一端。从另一个类(在while循环中)连续调用Retrieve。

cppServerSocket::cppServerSocket(int port){
    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if(fd<0){
        std::cout << "AN error occured when creating the socket" << std::endl;
        return;
    }

    addrlen = sizeof(remaddr); /* length of addresses */ 

    /* bind the socket to any valid IP address and a specific port */ 
    memset((char *)&myaddr, 0, sizeof(myaddr)); 
    //set to udp
    myaddr.sin_family = AF_INET;
    //host to network - long : convert a number into a 32-bit network representation.
    myaddr.sin_addr.s_addr = htonl(INADDR_ANY); 
    //host to network - short : convert a number into a 16-bit network representation.
    myaddr.sin_port = htons(port);

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

std::string cppServerSocket::retrieve(){
    int count = recvfrom(fd, buf, BUFSIZE-1, 0, (struct sockaddr *)&remaddr, &addrlen); 
    if(!displayedError && count < 0){
        std::cout << "An Error occured: " << strerror(errno) <<  std::endl;
        displayedError = true;
        return "";
    }
    if (count >= 0){
        printf("received message: \"%s\"\n", buf);
        std::string rcv(buf,count);
        return rcv;
    }
}

感谢帮助!

UPDATE 如果我修改检索,使其像一个服务器,只是打印它收到的每一条消息,它工作正常,没有错误......

std::string cppServerSocket::retrieve(){
    for(;;){
    int count = recvfrom(fd, buf, BUFSIZE-1, 0, (struct sockaddr *)&remaddr, &addrlen); 
    if(!displayedError && count < 0){
        std::cout << "An Error occured: " << strerror(errno) <<  std::endl;
        displayedError = true;
        //return "";
    }else if (count >= 0){
        printf("received message: \"%s\"\n", buf);
        std::string rcv(buf,count);
        //return rcv;
    }
    }
    return "";
}

下面添加了用于调用原始retrieve()方法的代码。

void check_socket(cppServerSocket css){
    string temp;

    temp.append(css.retrieve());
    if(temp.size()>0){
        std::cout << temp << std::endl; //for debugging
        debug.push_back(temp);
        debug.pop_front();
    }
}

下面是main中调用check_socket()方法的代码

    //code inside of main
    cppServerSocket css(3000);
    for(int i=0; i< test_time; i++){
        printScreen(test_devices, test_dev_status, false);
        t = clock();
        while(clock() - t < CLOCKS_PER_SEC){
            check_socket(css);
        }
    }

1 个答案:

答案 0 :(得分:1)

GOT IT !!!

问题出在check_socket(cppServerSocket css)中,因为我没有通过引用传递我的cppServerSocket的descructor被调用,并且在该析构函数中我指定了套接字应该关闭。所以在第一条消息收到后,袜子将被关闭。为了解决这个问题,我通过引用传递了cppServerSocket

void check_socket(cppServerSocket &css){
    string temp;

    temp.append(css.retrieve());
    if(temp.size()>0){
        std::cout << temp << std::endl; //for debugging
        debug.push_back(temp);
        debug.pop_front();
    }
}

感谢大家的帮助!