I was asked if I can explain my recommendations in the code. Here it goes. Take it with a grain of salt.
int Socket::RecvLine(std::string strBuffer)
{
while (1) {
char ch;
switch(rc = recv(s_, &buffer, 1, 0)) {
case 0: // End-of-stream, the peer closed connection
return DONE;
case 1: // Got something
strBuffer += ch;
if (ch == '\n')
return OK;
break;
case -1: // Some kind of error; the code below is just for reference.
// Don't use it in production.
if (errno == EINTR || errno == EAGAIN)
continue;
return ERROR;
}
}
}
As 200_success 200_success mentioned, recv
is expensive, and you really want to read as much as availabe, which means a secondary buffering is in order of the day. That's next step.
I was asked if I can explain my recommendations in the code. Here it goes. Take it with a grain of salt.
int Socket::RecvLine(std::string strBuffer)
{
while (1) {
char ch;
switch(rc = recv(s_, &buffer, 1, 0)) {
case 0: // End-of-stream, the peer closed connection
return DONE;
case 1: // Got something
strBuffer += ch;
if (ch == '\n')
return OK;
break;
case -1: // Some kind of error; the code below is just for reference.
// Don't use it in production.
if (errno == EINTR || errno == EAGAIN)
continue;
return ERROR;
}
}
}
As 200_success mentioned, recv
is expensive, and you really want to read as much as availabe, which means a secondary buffering is in order of the day. That's next step.
I was asked if I can explain my recommendations in the code. Here it goes. Take it with a grain of salt.
int Socket::RecvLine(std::string strBuffer)
{
while (1) {
char ch;
switch(rc = recv(s_, &buffer, 1, 0)) {
case 0: // End-of-stream, the peer closed connection
return DONE;
case 1: // Got something
strBuffer += ch;
if (ch == '\n')
return OK;
break;
case -1: // Some kind of error; the code below is just for reference.
// Don't use it in production.
if (errno == EINTR || errno == EAGAIN)
continue;
return ERROR;
}
}
}
As 200_success mentioned, recv
is expensive, and you really want to read as much as availabe, which means a secondary buffering is in order of the day. That's next step.
I was asked if I can explain my recommendations in the code. Here it goes. Take it with a grain of salt.
int Socket::RecvLine(std::string strBuffer)
{
while (1) {
char ch;
switch(rc = recv(s_, &buffer, 1, 0)) {
case 0: // End-of-stream, the peer closed connection
return DONE;
case 1: // Got something
strBuffer += ch;
if (ch == '\n')
return OK;
break;
case -1: // Some kind of error; the code below is just for reference.
// Don't use it in production.
if (errno == EINTR || errno == EAGAIN)
continue;
return ERROR;
}
}
}
As 200_success mentioned, recv
is expensive, and you really want to read as much as availabe, which means a secondary buffering is in order of the day. That's next step.