LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-01-2011, 06:23 AM   #1
manohar
Member
 
Registered: Dec 2010
Posts: 42

Rep: Reputation: 2
Smile Is recv() is blocking function...


Hi,

Is recv() in socket is a blocking function...?

Basically iam waiting for data on socket connection using recv(). Some times iam receiving recv_data as < 0. Could you guys please tell me the reasons for this.
while(1) {
recv_len = 0;
do{
recv_len += recv(sockId, &Rx_buffer[recv_len], 8, 0);
}while((recv_len>0) && (recv_len < 8));
if(recv_len < 0) {
printf("error while receiving: error %d",recv_len);
}else if(recv_len == 0) {
close(sockId);
}else {
printf("Received data");
}
}
 
Old 02-01-2011, 07:32 AM   #2
tecknophreak
LQ Newbie
 
Registered: Mar 2010
Posts: 2

Rep: Reputation: 0
No and yes, depending on how you open your connection, which was not shown. I'm assuming that you have opened it as blocking, as this is the default action. You are not showing any of your output, which would help identify what the problem is. You can take the line:

printf("error while receiving: error %d",recv_len);

and change it to either

perror("Error while receiving);
or
printf("error while receiving: error %s", strerror(errno));

Then check the error against the possible return values for recv, which can be seen in the man page for recv. Usually the error string shown will shed light on what issue your program is having.
 
Old 02-01-2011, 07:45 AM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I would also recommend re-coding this section:
Code:
recv_len = 0;
do{
recv_len += recv(sockId, &Rx_buffer[recv_len], 8, 0);
}while((recv_len>0) && (recv_len < 8));
Try something like:
Code:
#define MSG_SIZE 8
...

recv_len = 0

do
{
   ssize_t rtn = recv(sockId, Rx_buffer + recv_len, MSG_SIZE - recv_len, 0);

   if (rtn > 0)
   {
     recv_len += rtn;   /* only increment recv_len when you know that you have valid data! */
   }
   else if (rtn == 0)
   {
      /* peer disconnected */
      break;
   }
   else
   {
      if (errno != EAGAIN && errno != EINTR)   /* decide if you want to check for EINTR (interrupt) */
      {
         perror("Error with socket!");
      }
   }
} while (recv_len < MSG_SIZE);

Last edited by dwhitney67; 02-01-2011 at 07:48 AM.
 
Old 02-01-2011, 11:32 PM   #4
manohar
Member
 
Registered: Dec 2010
Posts: 42

Original Poster
Rep: Reputation: 2
Cool

Hi tecknophreak,

I changed the code as per your suggestion. Iam getting the following error
error while receiving: error Interrupted system call
 
Old 02-01-2011, 11:37 PM   #5
manohar
Member
 
Registered: Dec 2010
Posts: 42

Original Poster
Rep: Reputation: 2
Smile

This is the code iam using, tell me recv() is the blocking/non-blocking function
sockId = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sockId < 0) {
printf("Failed to crete listen socket\n");
return NULL;
}

lSockAddr.sin_family = AF_INET;
lSockAddr.sin_port = htons(params->sock_port);
lSockAddr.sin_addr.s_addr = INADDR_ANY;
while(connect(sockId, (struct sockaddr_in *)&lSockAddr,
sizeof(lSockAddr)) != 0) {
/* try to connect always*/
printf("Failed to connect on port =%d", sock_port);
sleep(1);
}
while(1) {
recv_len = 0;
do{
recv_len += recv(sockId, &Rx_buffer[recv_len], 8, 0);
}while((recv_len>0) && (recv_len < 8));
if(recv_len < 0) {
printf("error while receiving: error %d",recv_len);
}else if(recv_len == 0) {
close(sockId);
}else {
printf("Received data");
}
}
 
Old 02-01-2011, 11:40 PM   #6
manohar
Member
 
Registered: Dec 2010
Posts: 42

Original Poster
Rep: Reputation: 2
Smile

In man pages i found "EINTR The receive was interrupted by delivery of a signal before any data were availabl". what is this exactly means...?
 
Old 02-02-2011, 03:46 AM   #7
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Earlier, I provided a suggestion on how to fix the following section of code; I suggest you employ it. You cannot update 'recv_len' as you have below -- you must check for the return value from recv() before you do that.
Code:
while(1) {
recv_len = 0;
do{
recv_len += recv(sockId, &Rx_buffer[recv_len], 8, 0);
}while((recv_len>0) && (recv_len < 8));
if(recv_len < 0) {
printf("error while receiving: error %d",recv_len);
}else if(recv_len == 0) {
close(sockId);
}else {
printf("Received data");
}
}
As for the EINTR, it could (and probably is) caused by a signal interrupt to your application. I suspect that there is more to your application than you are showing here on the forum.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] blocking and non blocking TCP send/recv problem golden_boy615 Programming 5 12-27-2010 03:27 PM
Infomration regarding the send() and recv() function karimasif Linux - Networking 1 01-13-2008 10:49 AM
recv() is not coming out of blocking after closing the socket. arunka Linux - Networking 2 06-30-2006 09:52 PM
recv() function of SOCKET ashucool83 Programming 1 10-08-2005 07:38 PM
function recv() in socket husniteja Programming 1 08-18-2004 09:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration