I have two classes:
* BasexClient is used to manage all data between the client and an instance of BasexSocket
* BasexSocket is used for communication with the socket.
Each BasexClient has one private member Socket.
The readSocket function that kbw (9475) wrote for me (see
https://cplusplus.com/forum/beginner/285198/) uses this lamda-function to test if the socket is read for reading:
1 2 3 4 5 6 7 8
|
auto can_read = [](int s) -> bool {
fd_set read_set;
FD_ZERO(&read_set);
FD_SET(s, &read_set);
struct timeval timeout {};
int rc = select(s + 1, &read_set, NULL, NULL, &timeout);
return (rc == 1) && FD_ISSET(s, &read_set);
};
|
Since I am using a non-blocking connection, in the authentication procedure I had to introduce a wait() function between sending credentials and reading the result.
I transformed the lamda-function to this wait() function
1 2 3 4 5 6 7 8 9 10 11 12 13
|
bool wait(int s) {
fd_set read_set;
struct timeval timeout {};
memset(&timeout, 0, sizeof(timeout));
bool done{};
while (!done ) {
FD_ZERO(&read_set);
FD_SET(s, &read_set);
int rc = select(s + 1, &read_set, NULL, NULL, &timeout);
done = (rc == 1) && FD_ISSET(s, &read_set);
};
return done;
};
|
The authentication procedure gives no problems.
1 2 3 4
|
bytes_sent = writeData(auth);
wait(Master_sfd); // Avoid race-conditions
sock_read_string.clear();
bytes_read = readSocket( sock_read_string);
|
One of the methods from BasexClient is the Command method:
1 2 3 4 5
|
void BasexClient::Command(const std::string command) {
std:: string exec, result;
std::string response;
addVoid(command, exec).handShake(exec, result).splitResponse(result, response);
};
|
And this the handshake method:
1 2 3 4 5 6 7
|
BasexClient BasexClient::handShake(std::string Input, std::string &result) {
int bytes_sent = Socket.writeData(Input);
int sock = Socket.get_Socket();
Socket.wait(sock);
int bytes_read = Socket.readSocket( result);
return *this;
}
|
Line 9 in the wait() function gives problems.
When used in BasexSocket::Authenticate, int rc = select(s + 1, &read_set, NULL, NULL, &timeout); rc has value 4.
When used in the handshake Socket.wait(sock), rc has value 0.
When skipping Socket.waith() line 6 in the lambda gives the same result.
The arguments to select() are identical in both calls.
Why does select() returns different values?