Re: send and recv()hang even after set to nonblock.



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 写道:
On Oct 21, 1:27 pm, David Schwartz <dav...@xxxxxxxxxxxxx> wrote:
On Oct 20, 11:42 pm, gNash <ganeshamu...@xxxxxxxxx> 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;
}
.



Relevant Pages

  • Re: send and recv()hang even after set to nonblock.
    ... I am using HTTP server in my application. ...     recv(Socket, response, 0); ... since sending and receiving zero bytes ... SO_RCVTIMEO and SO_SNDTIMEO don't make the socket non-blocking. ...
    (comp.os.linux.networking)
  • send and recv()hang even after set to nonblock.
    ... I am using HTTP server in my application. ... MessageSend(char *message, char *response) ... send (Socket, message, 0); ... The socket option has been successfully set to non block using ...
    (comp.os.linux.networking)
  • Re: communication between processes
    ... when I have a long process that return response and needs to do more ... The duppasses the socket to both ... stdin and stdout of the child. ... middle-man close stdin and stdout. ...
    (comp.lang.tcl)
  • Re: URL filtering using LSP
    ... Microsoft MVP, MCSD ... E.g. in your WSPRecv implementation. ... mark the socket as readable and return your own response ... If you want to close the socket, ...
    (microsoft.public.win32.programmer.networks)
  • Re: communication between processes
    ... when I have a long process that return response and needs to do more ... work xinetd does not disconnect the connection and waits for the ... The duppasses the socket to both ... communication could be done if there was a need to do it as I ...
    (comp.lang.tcl)