Domanda

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.

È stato utile?

Soluzione

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)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top