Advertisement

02.01.2008 at 06:29PM PST, ID: 23131392
[x]
Attachment Details

Socket Programming in C - Problem with the recv() function

Asked by jonathanjeffrey in C Programming Language

Tags: c

I am writing a little program to learn about socket programming.  So far, it listens on a specific port, and I am able to telnet in and send a message.  It then recieves the message and displays it on the screen.  

My problem is this.  I believe I have to call recv in a while loop, as the entire message (if it is long) may not be recieved all at once.

The code below shows how I am handling the recieving of a message.  The problem is that recv() blocks until.... ?  I get stuck in the loop until I send over a bunch of characters (around 150).  How can I make sure that I get the entire message (if its big), but dont get blocked if the msg is small?

Thanks,
JonathanStart Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
int readData(int openSocket, char *buf, int bufsize)
{
  int bcount;  //total num bytes read
  int br;      //bytes read in this pass
  bcount= 0;
  br= 0;
  br = recv(openSocket, buf, bufsize-bcount,0);
  bcount += br;
  while (br > 0) //if 0 is returned it means the msg is complete?
  {            
    if ((br= recv(openSocket,buf,bufsize-bcount, 0)) > 0) {
      bcount += br;                
      buf += br;
    }
    else if (br < 0)     
  return(-1);
  }
return(bcount);
}
 
Loading Advertisement...
 
[+][-]02.01.2008 at 07:55PM PST, ID: 20802968

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C Programming Language
Tags: c
Sign Up Now!
Solution Provided By: jax79sg
Participating Experts: 2
Solution Grade: A
 
 
[+][-]02.02.2008 at 03:55AM PST, ID: 20804071

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.02.2008 at 12:44PM PST, ID: 20805849

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.02.2008 at 05:14PM PST, ID: 20806857

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.04.2008 at 09:25PM PST, ID: 20820826

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628