 |
|
| Linux Forum Index » Linux Networking » send and recv()hang even after set to nonblock.... |
|
Page 1 of 1 |
|
| Author |
Message |
| gNash... |
Posted: Tue Oct 20, 2009 8:42 pm |
|
|
|
Guest
|
Hi all.
I had some strange problem problem with my application.
I am using HTTP server in my application. Application been fine since
3 months.
I was tried with some new HTTP server then after application going
freeze.
I found the problem as below.
MessageSend(char *message, char *response)
{
send (Socket, message, 0);
recv(Socket, response, 0);
}
recv() not at all returned and whole system it self getting freeze
not only application. but when i add some amount of sleep between the
call whole application working normally.
MessageSend(char *message, char *response)
{
send (Socket, message, 0);
usleep(1000);
recv(Socket, response, 0);
}
Note: The socket option has been successfully set to non block using
SO_RCVTIMEO, SO_SNDTIMEO options for 2 seconds. (KEEP ALIVE) also
enabled.
Any guess for the hang ? is it bug itself with Kernel ? Please share
your thoughts.
Thanks in Advance,
Ganesh. |
|
|
| Back to top |
|
|
|
| David Schwartz... |
Posted: Tue Oct 20, 2009 10:27 pm |
|
|
|
Guest
|
On Oct 20, 11:42 pm, gNash <ganeshamu... at (no spam) gmail.com> wrote:
Quote: Hi all.
I had some strange problem problem with my application.
I am using HTTP server in my application. Application been fine since
3 months.
I was tried with some new HTTP server then after application going
freeze.
I found the problem as below.
MessageSend(char *message, char *response)
{
send (Socket, message, 0);
recv(Socket, response, 0);
}
Are these the system's send/recv functions? What's the point of
sending zero bytes?
Quote: recv() not at all returned and whole system it self getting freeze
not only application.
Sounds like you should troubleshoot that and figure out why the system
froze.
Quote: but when i add some amount of sleep between the
call whole application working normally.
MessageSend(char *message, char *response)
{
send (Socket, message, 0);
usleep(1000);
recv(Socket, response, 0);
}
Surprising that would work, since sending and receiving zero bytes
doesn't make any sense.
Quote: Note: The socket option has been successfully set to non block using
SO_RCVTIMEO, SO_SNDTIMEO options for 2 seconds. (KEEP ALIVE) also
enabled.
SO_RCVTIMEO and SO_SNDTIMEO don't make the socket non-blocking. Did
you actually set the socket non-blocking or not? If you did, that
would explain your problem, since your code is not designed for non-
blocking operations. (What happens if the send needs to block? You
just skip it.)
Quote: Any guess for the hang ? is it bug itself with Kernel ? Please share
your thoughts.
If your system as a whole is hanging, there's a problem with the
system itself. But your code looks very buggy. Can you paste the
actual code -- including any socket option settings? The code you did
paste makes no sense (send and receive zero bytes?) and is
contradicted by your description (not designed for non-blocking
operations, but you say you set the socket non-blocking -- but then
why set timeouts with socket options?).
DS |
|
|
| Back to top |
|
|
|
| gNash... |
Posted: Wed Oct 21, 2009 12:12 am |
|
|
|
Guest
|
On Oct 21, 1:27 pm, David Schwartz <dav... at (no spam) webmaster.com> wrote:
Quote: On Oct 20, 11:42 pm, gNash <ganeshamu... at (no spam) gmail.com> wrote:
Hi all.
I had some strange problem problem with my application.
I am using HTTP server in my application. Application been fine since
3 months.
I was tried with some new HTTP server then after application going
freeze.
I found the problem as below.
MessageSend(char *message, char *response)
{
send (Socket, message, 0);
recv(Socket, response, 0);
}
Are these the system's send/recv functions? What's the point of
sending zero bytes?
recv() not at all returned and whole system it self getting freeze
not only application.
Sounds like you should troubleshoot that and figure out why the system
froze.
but when i add some amount of sleep between the
call whole application working normally.
MessageSend(char *message, char *response)
{
send (Socket, message, 0);
usleep(1000);
recv(Socket, response, 0);
}
Surprising that would work, since sending and receiving zero bytes
doesn't make any sense.
Note: The socket option has been successfully set to non block using
SO_RCVTIMEO, SO_SNDTIMEO options for 2 seconds. (KEEP ALIVE) also
enabled.
SO_RCVTIMEO and SO_SNDTIMEO don't make the socket non-blocking. Did
you actually set the socket non-blocking or not? If you did, that
would explain your problem, since your code is not designed for non-
blocking operations. (What happens if the send needs to block? You
just skip it.)
Any guess for the hang ? is it bug itself with Kernel ? Please share
your thoughts.
If your system as a whole is hanging, there's a problem with the
system itself. But your code looks very buggy. Can you paste the
actual code -- including any socket option settings? The code you did
paste makes no sense (send and receive zero bytes?) and is
contradicted by your description (not designed for non-blocking
operations, but you say you set the socket non-blocking -- but then
why set timeouts with socket options?).
DS
Hi David,
I was posted pseudo code on my previous mail. here the actual code
below.
if would i add sleep(1) between send() and recv() program executing
very fine.
but if would i remove the sleep(1) some time i could the very partial
print message of response line.
Am i doing any wrong when setting timeout unblock, keepAlive options,
connecting.. any other issue ???
Please help me..
Thanks for your help.
Ganesh.
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
send_and_recv_message(char *address, char *message, int msgLength)
{
int socketId = 0;
int result= 0;
struct addrinfo hints;
struct addrinfo *addrResult = NULL;
struct sockaddr_in sockaddr4, *sock4 = NULL;
int portNumber = 80;
char response[4096];
struct timeval s_timeout;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
/** Get the family from given address / Name */
result= getaddrinfo(address, NULL, &hints, &addrResult);
if(result < 0)
{
printf("Error : %d \n",__LINE__);
return -1;
}
/** Create Socket **/
socketId = socket(AF_INET, SOCK_STREAM, 0);
if (socketId < 0)
{
perror("");
return -1;
}
/** Set Keep Alive **/
int keepAlive = 1;
result = setsockopt(socketId, SOL_SOCKET, SO_KEEPALIVE,
&keepAlive, (socklen_t)sizeof
(keepAlive));
if (result < 0)
{
perror("");
return -1;
}
/** Connect **/
sock4 = (struct sockaddr_in*)addrResult->ai_addr;
memset( &sockaddr4, 0, sizeof(struct sockaddr_in));
sockaddr4.sin_family = AF_INET;
sockaddr4.sin_addr.s_addr = sock4->sin_addr.s_addr;
sockaddr4.sin_port = htons(portNumber);
result = connect(socketId,(struct sockaddr *)&sockaddr4, sizeof
(sockaddr4));
if (result < 0)
{
perror("");
return -1;
}
/** Set TimeOut based unblock **/
s_timeout.tv_sec = 2;
s_timeout.tv_usec = 0;
result = setsockopt(socketId, SOL_SOCKET, SO_RCVTIMEO,
&s_timeout, (socklen_t)sizeof
(s_timeout));
if (result < 0)
{
perror("");
return -1;
}
result = setsockopt(socketId, SOL_SOCKET, SO_SNDTIMEO,
&s_timeout, (socklen_t)sizeof
(s_timeout));
if (result < 0)
{
perror("");
return -1;
}
/* Send and Recv */
result= send(socketId, message, msgLength, 0);
if(result< 0)
{
perror("");
return -1;
}
result= recv(socketId, response, sizeof(response), 0); //
NOT RETURNING //
if(result< 0)
{
perror("");
return -1;
}
response[result] = '\0';
printf("The Response from Server %s",response);
return 0;
} |
|
|
| Back to top |
|
|
|
| David Schwartz... |
Posted: Wed Oct 21, 2009 1:24 am |
|
|
|
Guest
|
On Oct 21, 3:12 am, gNash <ganeshamu... at (no spam) gmail.com> wrote:
How did you establish that 'recv' wasn't returning?
DS |
|
|
| Back to top |
|
|
|
| David Schwartz... |
Posted: Wed Oct 21, 2009 1:26 am |
|
|
|
Guest
|
On Oct 21, 3:12 am, gNash <ganeshamu... at (no spam) gmail.com> wrote:
Quote: but if would i remove the sleep(1) some time i could the very partial
print message of response line.
Where is the code that determines whether a message is complete or
partial? It looks like you made the biggest mistake is it possible to
make -- you forgot to implement a protocol.
If you have some logic that says "this is a complete message", "this
is a partial message", you have to actually WRITE THAT CODE. It won't
magically work by itself.
DS |
|
|
| Back to top |
|
|
|
| gNash... |
Posted: Wed Oct 21, 2009 1:37 am |
|
|
|
Guest
|
On Oct 21, 4:24 pm, David Schwartz <dav... at (no spam) webmaster.com> wrote:
Quote: On Oct 21, 3:12 am, gNash <ganeshamu... at (no spam) gmail.com> wrote:
How did you establish that 'recv' wasn't returning?
DS
Hi David,
when it was hang. i used printf message just before and and after the
recv() called.
ex :
printf("BEFORE\n");
recv(...);
printf("AFTER\n");
I could see many times only the "BEFORE" then system get hangs message
not "AFTER" .
but very rarely "BEFORE" and "AFT" ( partial message on "AFTER") then
system get hangs.
In the application the protocol handling been implemented completely.
is there any problem / bug/ hole in handling socket option() ?
Please tell me..
Ganesh |
|
|
| Back to top |
|
|
|
| David Schwartz... |
Posted: Wed Oct 21, 2009 4:40 am |
|
|
|
Guest
|
On Oct 21, 4:37 am, gNash <ganeshamu... at (no spam) gmail.com> wrote:
e many times only the "BEFORE" then system get hangs message
Quote: not "AFTER" .
but very rarely "BEFORE" and "AFT" ( partial message on "AFTER") then
system get hangs.
Are you sure this was with the code you posted? You weren't, for
example, calling 'recv' in a loop?
Quote: In the application the protocol handling been implemented completely.
Okay, because your example code utterly fails to actually implement
any protocol. You say it is getting partial messages, but there's no
code anywhere to determine what is or isn't a full message, so of
course it will get partial messages. (A message protocol won't work
unless you implement it.)
Quote: is there any problem / bug/ hole in handling socket option() ?
Not that I know of. My bet is your bug is elsewhere, most likely in
your failure to properly implement your protocol. (What does the code
to break the incoming byte stream into messages look like? What is the
protocol? How is the code supposed to know when it gets a complete
message?)
DS |
|
|
| Back to top |
|
|
|
| gNash... |
Posted: Wed Oct 21, 2009 9:51 pm |
|
|
|
Guest
|
On Oct 21, 7:40 pm, David Schwartz <dav... at (no spam) webmaster.com> wrote:
Quote: On Oct 21, 4:37 am, gNash <ganeshamu... at (no spam) gmail.com> wrote:
e many times only the "BEFORE" then system get hangs message
not "AFTER" .
but very rarely "BEFORE" and "AFT" ( partial message on "AFTER") then
system get hangs.
Are you sure this was with the code you posted? You weren't, for
example, calling 'recv' in a loop?
there is one while loop as bellow (pseudo code)
send(request message);
//Sleep(1) //After adding sleep(1);
application running fine.
recv(response message); // Hang point
//Parse the response message;
// Http library function.
----
----
----
while(xbytes)
{
byte = recv( data); // size request will vary from 1 byte to 4K;
xbytes -= byte;
}
Quote: In the application the protocol handling been implemented completely.
Okay, because your example code utterly fails to actually implement
any protocol. You say it is getting partial messages, but there's no
code anywhere to determine what is or isn't a full message, so of
course it will get partial messages. (A message protocol won't work
unless you implement it.)
is there any problem / bug/ hole in handling socket option() ?
Not that I know of. My bet is your bug is elsewhere, most likely in
Yes. I hope so. but printf messages and sleep() making me to suspect
on response recv.
Quote: your failure to properly implement your protocol. (What does the code
to break the incoming byte stream into messages look like? What is the
protocol?
HTTP
How is the code supposed to know when it gets a complete
Quote: message?)
Response message will give the information with total number of bytes
could download from server.
This is will download / dumped in second while loop as shown as above.
Ganesh.
|
|
|
| Back to top |
|
|
|
| David Schwartz... |
Posted: Thu Oct 22, 2009 9:09 am |
|
|
|
Guest
|
On Oct 22, 12:51 am, gNash <ganeshamu... at (no spam) gmail.com> wrote:
Quote: Response message will give the information with total number of bytes
could download from server.
This is will download / dumped in second while loop as shown as above.
My bet is that your bug is in that code, calling 'recv' when it
shouldn't.
DS |
|
|
| Back to top |
|
|
|
| ZHOU Xiaobo... |
Posted: Sat Oct 31, 2009 8:27 am |
|
|
|
Guest
|
I think it's reason may be that
your client's os doesn't support so_recvtimo option.
And you new http server has something wrong and doesn't resonponse,
so your client's recv operation can't return.
gNash 写é“:
Quote: On Oct 21, 1:27 pm, David Schwartz <dav... at (no spam) webmaster.com> wrote:
On Oct 20, 11:42 pm, gNash <ganeshamu... at (no spam) gmail.com> wrote:
Hi all.
I had some strange problem problem with my application.
I am using HTTP server in my application. Application been fine since
3 months.
I was tried with some new HTTP server then after application going
freeze.
I found the problem as below.
MessageSend(char *message, char *response)
{
send (Socket, message, 0);
recv(Socket, response, 0);
}
Are these the system's send/recv functions? What's the point of
sending zero bytes?
recv() not at all returned and whole system it self getting freeze
not only application.
Sounds like you should troubleshoot that and figure out why the system
froze.
but when i add some amount of sleep between the
call whole application working normally.
MessageSend(char *message, char *response)
{
send (Socket, message, 0);
usleep(1000);
recv(Socket, response, 0);
}
Surprising that would work, since sending and receiving zero bytes
doesn't make any sense.
Note: The socket option has been successfully set to non block using
SO_RCVTIMEO, SO_SNDTIMEO options for 2 seconds. (KEEP ALIVE) also
enabled.
SO_RCVTIMEO and SO_SNDTIMEO don't make the socket non-blocking. Did
you actually set the socket non-blocking or not? If you did, that
would explain your problem, since your code is not designed for non-
blocking operations. (What happens if the send needs to block? You
just skip it.)
Any guess for the hang ? is it bug itself with Kernel ? Please share
your thoughts.
If your system as a whole is hanging, there's a problem with the
system itself. But your code looks very buggy. Can you paste the
actual code -- including any socket option settings? The code you did
paste makes no sense (send and receive zero bytes?) and is
contradicted by your description (not designed for non-blocking
operations, but you say you set the socket non-blocking -- but then
why set timeouts with socket options?).
DS
Hi David,
I was posted pseudo code on my previous mail. here the actual code
below.
if would i add sleep(1) between send() and recv() program executing
very fine.
but if would i remove the sleep(1) some time i could the very partial
print message of response line.
Am i doing any wrong when setting timeout unblock, keepAlive options,
connecting.. any other issue ???
Please help me..
Thanks for your help.
Ganesh.
#include <stdio.h
#include <sys/socket.h
#include <sys/types.h
#include <netdb.h
#include <stdlib.h
#include <string.h
send_and_recv_message(char *address, char *message, int msgLength)
{
int socketId = 0;
int result= 0;
struct addrinfo hints;
struct addrinfo *addrResult = NULL;
struct sockaddr_in sockaddr4, *sock4 = NULL;
int portNumber = 80;
char response[4096];
struct timeval s_timeout;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
/** Get the family from given address / Name */
result= getaddrinfo(address, NULL, &hints, &addrResult);
if(result < 0)
{
printf("Error : %d \n",__LINE__);
return -1;
}
/** Create Socket **/
socketId = socket(AF_INET, SOCK_STREAM, 0);
if (socketId < 0)
{
perror("");
return -1;
}
/** Set Keep Alive **/
int keepAlive = 1;
result = setsockopt(socketId, SOL_SOCKET, SO_KEEPALIVE,
&keepAlive, (socklen_t)sizeof
(keepAlive));
if (result < 0)
{
perror("");
return -1;
}
/** Connect **/
sock4 = (struct sockaddr_in*)addrResult->ai_addr;
memset( &sockaddr4, 0, sizeof(struct sockaddr_in));
sockaddr4.sin_family = AF_INET;
sockaddr4.sin_addr.s_addr = sock4->sin_addr.s_addr;
sockaddr4.sin_port = htons(portNumber);
result = connect(socketId,(struct sockaddr *)&sockaddr4, sizeof
(sockaddr4));
if (result < 0)
{
perror("");
return -1;
}
/** Set TimeOut based unblock **/
s_timeout.tv_sec = 2;
s_timeout.tv_usec = 0;
result = setsockopt(socketId, SOL_SOCKET, SO_RCVTIMEO,
&s_timeout, (socklen_t)sizeof
(s_timeout));
if (result < 0)
{
perror("");
return -1;
}
result = setsockopt(socketId, SOL_SOCKET, SO_SNDTIMEO,
&s_timeout, (socklen_t)sizeof
(s_timeout));
if (result < 0)
{
perror("");
return -1;
}
/* Send and Recv */
result= send(socketId, message, msgLength, 0);
if(result< 0)
{
perror("");
return -1;
}
result= recv(socketId, response, sizeof(response), 0); //
NOT RETURNING //
if(result< 0)
{
perror("");
return -1;
}
response[result] = '\0';
printf("The Response from Server %s",response);
return 0;
} |
|
|
| Back to top |
|
|
|
|
|
All times are GMT - 5 Hours
The time now is Fri Nov 27, 2009 12:32 pm
|
|