Pregunta

I'm writing a C function to check if a socket connection from client is available. I use 'recv' function with MSG_PEEK not to alter the input buffer.

However, when the socket connection is closed by client, 'recv' is supposed to return -1, but it doesn't. After client closed, 'recv' in the function below returns 0 all the times.

char is_avail(int connection) {
  char buffer;

  int result = recv(connection,&buffer,1,MSG_PEEK);
  if (result<0)
    return 0;
  else
    return 1;
}

Any reason to this matter? and also I want to combine MSG_PEEK with MSG_WAITALL. I tried:

recv(connection,&buffer,1,MSG_PEEK|MSG_WAITALL);

but it doesn't take effect.

¿Fue útil?

Solución

recv does NOT return -1 when the socket is closed properly, but rather '0'.

0 -> graceful closing of the socket

-1-> An actual error occurred

> 0-> Data has been read.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top