944,079 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 8820
  • C++ RSS
Apr 16th, 2007
0

Recv()ing a long message

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  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. =(
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Savs is offline Offline
5 posts
since Apr 2007
Apr 16th, 2007
0

Re: Recv()ing a long message

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Apr 17th, 2007
0

Re: Recv()ing a long message

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Savs is offline Offline
5 posts
since Apr 2007
Apr 17th, 2007
0

Re: Recv()ing a long message

Click to Expand / Collapse  Quote originally posted by Salem ...
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.
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Apr 17th, 2007
0

Re: Recv()ing a long message

You can specify a timeout with select().

I wasn't going to bother answering the post without code tags.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Apr 17th, 2007
0

Re: Recv()ing a long message

Click to Expand / Collapse  Quote originally posted by thekashyap ...
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):
C++ Syntax (Toggle Plain Text)
  1. if(!FD_ISSET(skt, &fd_cnt))break;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Savs is offline Offline
5 posts
since Apr 2007
Apr 18th, 2007
0

Re: Recv()ing a long message

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.
C++ Syntax (Toggle Plain Text)
  1. while((n=recv(skt, buf, BUFFER_SIZE, MSG_DONTWAIT))>0){
  2. buf[n]='\0';
  3. std::cout<<buf;
  4. }

thank you for assistance =)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Savs is offline Offline
5 posts
since Apr 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ programming Home Work HELP...please immediate assistance.assistance.
Next Thread in C++ Forum Timeline: WHat does this mean?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC