Closing sockets and closing connections

Closing sockets and closing connections

Post by marco stipe » Tue, 27 Apr 1999 04:00:00



Is there anway (not the reset button :-) kill a TCP connection
immediatly?

Im writing a TCP/IP server, but if I only close the socket I can't
restart within
1,5-2 minutes (cause the Java-Applet-client does not close) and binding
(bind()) fails. e.g. Apachie could restart immediatly. I've tried
serveral things (shutdown(fd,0) before close(fd); SO_REUSEADDR etc.
Nothing is working.

is there any sollutions?

 
 
 

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. Dbugging with DDD

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

4. Creating symbolic link to dir via argument?

5. socket close doesn't really close

6. DIMM-socketed Pentium Pro boards

7. How do I properly close a socket connection?

8. intermittent video card flicker

9. Checking for a closed socket connection

10. How to detect when socket connection closes?

11. Linux sockets problem (closed connections staying open?)

12. Sockets connections - proper way to close them?

13. How can I close unwanted socket connections?