1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
int readSocket( std::string &sockResponseString) {
struct timeval timeout;
memset(&timeout, 0, sizeof(timeout));
// char buf[BUFSIZ];
char buf[8]; // test if reading small chunks functions
fd_set read_Master_set;
FD_ZERO(&read_Master_set);
FD_SET(Master_sfd, &read_Master_set);
int ret;
memset(&buf, 0, sizeof(buf));
select(Master_sfd + 1, &read_Master_set, NULL, NULL, &timeout);
while (1) {
errno = 0;
ret = read(Master_sfd, buf, sizeof(buf));
if (ret == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
break;
} else {
perror("Read");
exit(EXIT_FAILURE);
}
}
if (ret == 0) break;
sockResponseString.append(buf, ret);
if (sizeof(buf) > 0) cout << buf << endl;
// I read somewhere that 'select()' is necessary before each read ?...
memset(&buf, 0, sizeof(buf));
select(Master_sfd + 1, &read_Master_set, NULL, NULL, &timeout);
}
return sockResponseString.size();
}
|