2010-03-05 07:32:27     sockets

Document created by Aaronwu Employee on Aug 20, 2013
Version 1Show Document
  • View in full screen mode

2010-03-05 07:32:27     sockets

Filip Vanalme (BELGIUM)

Message: 86860   

 

Sometimes, I notice that our application stops running although there's nowhere an exit call (it shoudl normally continue forever). With a little test-program, I think I could reproduce the problem and I think the problem is related to socket behaviour.

When I successfully open a socket and do a send to it (before a connect), my test app quits and returns to the prompt. I know, this is not the way to do(send should be prohibited till connected), but is this behaviour of the send command normal ? I thaught a send could be used to check whether a socket is (still) connected ?

I think, in our real program, in a certain abnormal condition, a send is done on a socket that is not (yet?) connected, causing the application to quit.

I wonder if I would have the same behaviour if I do the send while the socket is connecting to the remote party (e.g. I do a connect in non-blocking mode). I will try to do the test.

TranslateQuoteReplyEditDelete

 

 

2010-03-05 07:37:14     Re: sockets

Mike Frysinger (UNITED STATES)

Message: 86861   

 

no, it should not quit.  it should return an error code like normal.

QuoteReplyEditDelete

 

 

2010-03-05 07:56:50     Re: sockets

Filip Vanalme (BELGIUM)

Message: 86862   

 

This is a code snippet of what I do in my test program :

 

 

 

  int sock = 0;

  unsigned char data[] = {'0', '1', '2', '3', '4'};

  int flags;

  struct  sockaddr_in addr;

 

 

  sock = socket(AF_INET, SOCK_STREAM, 0);

  if (sock < 0)

  {

    printf ("Failed to open socket : %s\n", strerror(errno));

    return;

  }

 

  printf ("Socket open\n");

 

  flags = fcntl(sock, F_GETFL, NULL);

  if (flags < 0)

  {

    printf ("Failed to get flags of socket : %s\n", strerror(errno));

    return;

  }

 

  printf ("Set to nonblocking\n");

 

  flags |= O_NONBLOCK;

  if (fcntl(sock, F_SETFL, flags) < 0)

  {

    printf ("Failed to set flags of socket : %s\n", strerror(errno));

    return;

  }

 

  printf ("sending data on socket\n");

 

  if (send (sock, data, sizeof(data), 0) < 0)

  {

    printf ("Failed to send data : %s\n", strerror(errno));

  }

 

  printf ("Closing socket...\n");

 

  close (sock);

 

  while (1)

  {

    printf ("*");

    sleep (1);

  }

 

 

This is what I get on the console output :

 

Socket open

Set to nonblocking

sending data on socket

root:/bin>

 

As you can see, it never reaches the close socket and the following while loop.

TranslateQuoteReplyEditDelete

 

 

2010-03-05 08:32:24     Re: sockets

Filip Vanalme (BELGIUM)

Message: 86865   

 

This is a code snippet of what I do in my test program :

 

 

 

  int sock = 0;

  unsigned char data[] = {'0', '1', '2', '3', '4'};

  int flags;

  struct  sockaddr_in addr;

 

 

  sock = socket(AF_INET, SOCK_STREAM, 0);

  if (sock < 0)

  {

    printf ("Failed to open socket : %s\n", strerror(errno));

    return;

  }

 

  printf ("Socket open\n");

 

  flags = fcntl(sock, F_GETFL, NULL);

  if (flags < 0)

  {

    printf ("Failed to get flags of socket : %s\n", strerror(errno));

    return;

  }

 

  printf ("Set to nonblocking\n");

 

  flags |= O_NONBLOCK;

  if (fcntl(sock, F_SETFL, flags) < 0)

  {

    printf ("Failed to set flags of socket : %s\n", strerror(errno));

    return;

  }

 

  printf ("sending data on socket\n");

 

  if (send (sock, data, sizeof(data), 0) < 0)

  {

    printf ("Failed to send data : %s\n", strerror(errno));

  }

 

  printf ("Closing socket...\n");

 

  close (sock);

 

  while (1)

  {

    printf ("*");

    sleep (1);

  }

 

 

This is what I get on the console output :

 

Socket open

Set to nonblocking

sending data on socket

root:/bin>

 

As you can see, it never reaches the close socket and the following while loop.

 

---

 

the part that sets the non-blocking mode makes no difference. Even a socket() followed by a send() causes the exit

TranslateQuoteReplyEditDelete

 

 

2010-03-08 11:08:48     Re: sockets

Ian Jeffray (UNITED KINGDOM)

Message: 86959   

 

This happens if your ethernet device isn't configured.   Not sure why it's a silent quit, but that's what I also see anyway.  Ensure your interface is ifconfig'd and working as intended first.

QuoteReplyEditDelete

 

 

2010-03-09 16:38:27     Re: sockets

Mike Frysinger (UNITED STATES)

Message: 87004   

 

not sure why you arent seeing the SIGPIPE, but the behavior you show is correct

 

root:/> strace /a.out

...

send(3, "01234", 5, 0)                  = -1 EPIPE (Broken pipe)

--- SIGPIPE (Broken pipe) @ 0 (0) ---

+++ killed by SIGPIPE +++

 

the send(2) man page covers this in pretty good detail:

       MSG_NOSIGNAL (since Linux 2.2)

              Requests not to send SIGPIPE on errors on stream oriented sockets when the other end breaks the connection.  The EPIPE error is still returned.

QuoteReplyEditDelete

 

 

2010-03-10 02:50:47     Re: sockets

Filip Vanalme (BELGIUM)

Message: 87014   

 

I do not agree with this. Even when the interface is correctly configured (ifconfig'd), if one does a send on a socket that is not connected, the application quits. See response of Mike for more info.

TranslateQuoteReplyEditDelete

 

 

2010-03-10 02:55:34     Re: sockets

Filip Vanalme (BELGIUM)

Message: 87015   

 

Thanks Mike !

 

That's it ! When I redo my tests with a send containing MSG_NOSIGNAL in the flags field, I get an error but the application is not quiting anymore.

Attachments

    Outcomes