Finding used socket receive-buffer size

I have set the receive buffer size of socket to max.

setsockopt(sd,SOL_SOCKET, SO_RCVBUF,&max,optval);

Am reading data from the socket in a loop(say max 100 bytes per recv)

while(1)
  {
   int rlen=recv(sd,(void *)buf, 100 , 0);
   //err handle and processing
  }

Assume my process is slow and the buffer(unread message size) is getting full.

Is there a way to find the unread bytes in the receive buffer(without reading them all),just to detect bottleneck?

There a a number of techniques available. One way is to use the MSG_PEEK flag with recv() to peek at the data that is available to be read. See Section 13.7 Volume 1 2nd edition, UNIX network Programming by Richard Stevens for a detailed discussion.

Thanks,
I also got one more soln. from another forum

ioctl( sock_fd, SIOCINQ, &numBytes )