954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Recv()ing a long message

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. =(

Savs
Newbie Poster
5 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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<

Savs
Newbie Poster
5 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 
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 socketwill 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..

thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

You can specify a timeout with select().

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

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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;
Savs
Newbie Poster
5 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

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 =)

Savs
Newbie Poster
5 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You