Question

When I read from a SOCK_STREAM socket like this:

int t;
while ((t = read(clientsocket, buff, 128) > 0))
{
  write(1, buff, t);
}

read always return 1, but if I look into buff with gdb I can see the whole line I sent. I'm using netcat to send data to the server.

Was it helpful?

Solution

This is incorrect due to operator precedence:

while ((t = read(clientsocket, buff, 128) > 0))

and results in t being assigned the result of read(clientsocket, buff, 128) > 0, which will be 0 or 1.

To correct, change to:

while ((t = read(clientsocket, buff, 128)) > 0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top