Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: msg size wrong #14

Open
IOTeule opened this issue Jan 18, 2022 · 2 comments
Open

bug: msg size wrong #14

IOTeule opened this issue Jan 18, 2022 · 2 comments

Comments

@IOTeule
Copy link

IOTeule commented Jan 18, 2022

There seems to be a bug in the server example about incoming msg size.

To reproduce:

  1. build master
  2. start client_example and server_example
  3. client: send message '1234356'
  4. server receives '123456'
  5. client send message ' 4'
  6. server receives '423456', but should be '4' only

seems to be a bug in message size

@IOTeule
Copy link
Author

IOTeule commented Jan 18, 2022

I think, this should fix it:

void Client::receiveTask() {
    while(isConnected()) {
        const fd_wait::Result waitResult = fd_wait::waitFor(_sockfd);

        if (waitResult == fd_wait::Result::FAILURE) {
            throw std::runtime_error(strerror(errno));
        } else if (waitResult == fd_wait::Result::TIMEOUT) {
            continue;
        }

        char receivedMessage[MAX_PACKET_SIZE];
        const size_t numOfBytesReceived = recv(_sockfd.get(), receivedMessage, MAX_PACKET_SIZE, 0);

        if(numOfBytesReceived < 1) {
            const bool clientClosedConnection = (numOfBytesReceived == 0);
            std::string disconnectionMessage;
            if (clientClosedConnection) {
                disconnectionMessage = "Client closed connection";
            } else {
                disconnectionMessage = strerror(errno);
            }
            setConnected(false);
            publishEvent(ClientEvent::DISCONNECTED, disconnectionMessage);
            return;
        } else {
            // fix here
            receivedMessage[numOfBytesReceived] = '\0';
            // fix here
            publishEvent(ClientEvent::INCOMING_MSG, receivedMessage);
        }
    }
}

@IOTeule
Copy link
Author

IOTeule commented Jan 18, 2022

see PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant