문제

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.

도움이 되었습니까?

해결책

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top