Tyler Weiss
Tyler Weiss

Reputation: 169

Cannot receive more than one message when using c++ sockets

I have attached the code below, the title says it all. I am able to receive one message and then all other messages sent after the first don't get sent or arent read in.

Client, send_msg is called multiple times and cppClientSocket is the constructor that sets up the connection.

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");
};

Server, cppServerSocket is used to construct the other side of the UDP socket. Retrieve is called continuously from another class (within a while loop).

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;
    }
}

Appreciate the help!

UPDATE If I modify the retrieve so that it acts like a server that will simply print every message it recieves, it works fine without error...

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 "";
}

Below added the code that was used to call the original retrieve() method.

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();
    }
}

Below is the code inside of main that calls the check_socket() method

    //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);
        }
    }

Upvotes: 0

Views: 122

Answers (1)

Tyler Weiss
Tyler Weiss

Reputation: 169

GOT IT!!!

The problem was in check_socket(cppServerSocket css), because I was not passing by reference the descructor of my cppServerSocket was getting called, and in that destructor I had specified that the socket should close. So after the first message was recieved the socked would be closed. To fix this i passed cppServerSocket by reference

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();
    }
}

Thanks to all for the help!

Upvotes: 1

Related Questions