ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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");
}
}
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.
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);
}
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.