Recv()ing a long message

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 5
Reputation: Savs is an unknown quantity at this point 
Solved Threads: 0
Savs Savs is offline Offline
Newbie Poster

Recv()ing a long message

 
0
  #1
Apr 16th, 2007
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.
  1. while(true){
  2. n=recv(skt, buf, BUFFER_SIZE,0);
  3. if(n<=0)break;
  4. buf[n]='\0';
  5. std::cout<<buf;
  6. }
So, how to break from while(true) before recv() blocks? My experiments with select() failed. =(
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Recv()ing a long message

 
0
  #2
Apr 16th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 5
Reputation: Savs is an unknown quantity at this point 
Solved Threads: 0
Savs Savs is offline Offline
Newbie Poster

Re: Recv()ing a long message

 
0
  #3
Apr 17th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Recv()ing a long message

 
0
  #4
Apr 17th, 2007
Originally Posted by Salem View Post
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..
Last edited by thekashyap; Apr 17th, 2007 at 4:35 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Recv()ing a long message

 
0
  #5
Apr 17th, 2007
You can specify a timeout with select().

I wasn't going to bother answering the post without code tags.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 5
Reputation: Savs is an unknown quantity at this point 
Solved Threads: 0
Savs Savs is offline Offline
Newbie Poster

Re: Recv()ing a long message

 
0
  #6
Apr 17th, 2007
Originally Posted by thekashyap View Post
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):
  1. if(!FD_ISSET(skt, &fd_cnt))break;
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 5
Reputation: Savs is an unknown quantity at this point 
Solved Threads: 0
Savs Savs is offline Offline
Newbie Poster

Re: Recv()ing a long message

 
0
  #7
Apr 18th, 2007
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.
  1. while((n=recv(skt, buf, BUFFER_SIZE, MSG_DONTWAIT))>0){
  2. buf[n]='\0';
  3. std::cout<<buf;
  4. }

thank you for assistance =)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC