socket close doesn't really close

socket close doesn't really close

Post by Matthias Scheidegge » Sat, 15 May 1999 04:00:00



Why can an inet port stay occupied even if I close the socket?

I have an inet socket bound to a port, a thread listening on it and a
signal waiter thread.
When I send a SIGINT the waiter catches it, cancels the listener thread,
closes the socket and exits.
After the program terminates, netstat tells me it is still open....

any ideas why?

Matthias

 
 
 

socket close doesn't really close

Post by Barry Margoli » Sat, 15 May 1999 04:00:00




Quote:>Why can an inet port stay occupied even if I close the socket?

>I have an inet socket bound to a port, a thread listening on it and a
>signal waiter thread.
>When I send a SIGINT the waiter catches it, cancels the listener thread,
>closes the socket and exits.
>After the program terminates, netstat tells me it is still open....

>any ideas why?

Download the "lsof" utility and it will tell you what processes have a
socket open.  Maybe the process forked a child and didn't close the socket
in the child process.  Or maybe the above process is a child of a server
process that originally accepted the socket, and the server didn't close it
after forking the child.

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

1. shutdown(s, 2) vs close(s) for closing unix domain sockets

The specification of the shutdown(s, 2) system call is to be "more
dramatic" than the close(s) system call, since any pending data is
lost. However, if you use shutdown(s, 2) instead of close(s) under
Solaris, you will have the nasty surprise to notice that the socket
isn't released at all. This mean that the following client/server
program will block at some stage. Furtermore, if you examine the
socket name space, by netstat -f unix, you will notice that more
than one socket with name "/tmp/foo" is registered by the system...

server.c
--------

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>

main()
{
  int sock, newsock, length;
  struct sockaddr_un name;
  char* buf[1024];

  for(;;)
  {
    sock = socket(AF_UNIX, SOCK_STREAM, 0);

    name.sun_family = AF_UNIX;
    strcpy(name.sun_path, "/tmp/foo");

    bind(sock, (struct sockaddr *)&name, sizeof name);
    listen(sock, 1);
    newsock = accept(sock, (struct sockaddr *) 0, (int *) 0);
    read(newsock, buf, 1024);
    printf("%s", buf);

    shutdown(sock, 2);
    shutdown(newsock, 2);
    unlink(name.sun_path);
  }

/* gcc server.c -o server -lsocket */

client.c
--------

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#define DATA "hello world\n"

main()
{
  int sock, length;
  struct sockaddr_un name;

  for(;;)
  {
    sleep(5);
    sock = socket(AF_UNIX, SOCK_STREAM, 0);

    name.sun_family = AF_UNIX;
    strcpy(name.sun_path, "/tmp/foo");

    connect(sock, (struct sockaddr *)&name, strlen(name.sun_path) +
                                          sizeof(name.sun_family));

    write(sock, DATA, sizeof(DATA));
    shutdown(sock, 2);
  }

/* gcc client.c -o client -lsocket */

Now if you simply replace all shutdown(s, 2) by close(s), it works
very well. Under SunOS-4, both calls lead to client/server program
that works. I haven't tested it with internet domain sockets yet.  

Any comment?

          \\|//
          |^ ^|          
          (O O)        
----oo0o---(_)---o0oo-------------------------------------------------------

Benoit Garbinato

Departement d'Informatique               (DI)   phone:  + 41 21 693 52 74
Ecole Polytechnique Federale de Lausanne (EPFL) fax:    + 41 21 693 39 09
CH-1015 LAUSANNE
(Switzerland)

2. How to check if a file is modified in the last hour in ksh?

3. Sockets: close() does not close!?

4. edonkey - serverlistenupdater

5. Closing sockets and closing connections

6. Creative 3D Banshee AGP 16 - Can't get setup.

7. NFS Problem: Files aren't closed on Server (when closed on client)

8. C menu Program

9. closing 'listening' sockets

10. close rndc service, so port 953 will close

11. How to avoid closing of child tasks when closing window ??

12. Mandrake & Red Hat - How close is close?

13. Closing ports by closing services