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"); } } |
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. |
I would also recommend re-coding this section:
Code:
recv_len = 0; Code:
#define MSG_SIZE 8 |
Hi tecknophreak,
I changed the code as per your suggestion. Iam getting the following error error while receiving: error Interrupted system call |
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) { |
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...?
|
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) { |
All times are GMT -5. The time now is 03:31 PM. |