Frage

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.

War es hilfreich?

Lösung

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)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top