Is there a way to read the long answer from server not using non-blocking sockets?

The problem is that recv blocks if server stops to send.

while(true){
		n=recv(skt, buf, BUFFER_SIZE,0);
                if(n<=0)break;
		buf[n]='\0';
		std::cout<<buf;
	}

So, how to break from while(true) before recv() blocks? My experiments with select() failed. =(

Erm, you use select(), and you post some kind of attempt at using select() so we can figure out where you went wrong.

Saying which OS and compiler you're using would be a good idea as well.

I'm using gcc 3.x on FreeBSD 6.1

I tried select in this way:

fd_set fd_cnt;
FD_ZERO(&fd_cnt);
FD_SET(skt, &fd_cnt);
while(true){
        select(skt+1, &fd_cnt, NULL, NULL, 0);
        if(!FD_ISSET(skt, &fd_cnt))break;
                n=recv(skt, buf, BUFFER_SIZE,0);
                if(n<=0)break;
                buf[n]='\0';
                std::cout<<buf;
        }

but recv() still blocks.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Erm, you use select(), and you post some kind of attempt at using select() so we can figure out where you went wrong.

Wouldn't select() itself block !? If it does it anyway can't be used for this problem..

from what I know a blocking socket will be blocking. If you're not creating the socket then try making it non-blocking while you're using it and then make it blocking once you're done with your work.. not so nice to do though..
Use fcntl to make it non/blocking..

You can specify a timeout with select().

I wasn't going to bother answering the post without code tags.

Wouldn't select() itself block !? If it does it anyway can't be used for this problem..

from what I know a blocking socket will be blocking. If you're not creating the socket then try making it non-blocking while you're using it and then make it blocking once you're done with your work.. not so nice to do though..
Use fcntl to make it non/blocking..

thank you. I used select() to check for any data that is ready to be recieved. So if there is no any data, we can leave from while(true):

if(!FD_ISSET(skt, &fd_cnt))break;

found a simple solvation.
man recv really helped me out =)
so, in simple case we can bypass non-blocking sockets and select(). There is a way to tell recv() not to block by using MSG_DONTWAIT value.

while((n=recv(skt, buf, BUFFER_SIZE, MSG_DONTWAIT))>0){
                buf[n]='\0';
                std::cout<<buf;
}

thank you for assistance =)

This article has been dead for over six months. Start a new discussion instead.