elhayra / tcp_server_client

A thin and simple C++ TCP client server
MIT License
159 stars 53 forks source link

String with space char leads into crash #22

Open guitronimo opened 3 months ago

guitronimo commented 3 months ago

Hey yall!

Learning C++ at the moment and i found that this a great project to build on to! Thanks for that.

I come a cross an issue that i dont understand.

When sending "Hello Wolrd" as message i get:

joe@devel:/media/nas/Development/cpp/Interface/src/TCPCS/build$ ./tcp_client
Client connected successfully
select one of the following options:
1. send message to server
2. close client and exit
1
enter message to send:
Hello World
message was sent successfuly
select one of the following options:
1. send message to server
2. close client and exit
terminate called after throwing an instance of 'std::runtime_error'
  what():  invalid menu input. expected a number, but got something else
Aborted (core dumped)
joe@devel:/media/nas/Development/cpp/Interface/src/TCPCS/build$

Its like as if it jumps back automatically to menu on space characters and thinks that next input is "World", where it expects a number again.

Can you point me towards the code that i can check debugging?

Thx

guitronimo commented 3 months ago

After some reasearch this appears to be a std::cin problem.

Solution appears to be:


        case 1: { // send message to server
            std::cout << "enter message to send:\n";
            std::string message;
            std::getline(std::cin, message);    //std::cin >> message; // Bugged in g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
            pipe_ret_t sendRet = client.sendMsg(message.c_str(), message.size());
            if (!sendRet.isSuccessful()) {
                std::cout << "Failed to send message: " << sendRet.message() << "\n";
            } else {
                std::cout << "message was sent successfuly\n";
            }
            break;
        }```