How can I close a Socket "quickly"

tawab at net4.UUCP tawab at net4.UUCP
Sun Aug 6 06:25:38 AEST 1989


Hi,

can somebody out there tell me how to close a socket "quickly".
The problem I have is that after talking over a socket, I won't
need the socket any more and so I just close it, but it keeps
hanging around for about 2-3 min and blocks the queue for new
incoming messages. I know that a TCP-socket does something like
that. So I tried "shutdown(socket, 2)", but there is no difference.

The main body looks like this

	sock = socket(AF_INET, SOCK_STREAM, 0);
	server.sin_family = AF_INET;
	server.sin_addr.s_addr = INADDR_ANY;
	server.sin_port = xx;

	...... and so on

	setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)0, 0)
	.... and
	setsockopt(sock, SOL_SOCKET, SO_DONTLINGER, (char *)0, 0)
	listen(sock, 5);
	do {
		if ((msgsock = accept(sock, 0, 0)) == -1)
			perror("accept");
		else do {
			bzero(buf, sizeof(buf));
			if ((rval = read(msgsock, buf, 1024)) < 0) {
				perror("reading msgsock");
			}
			if (rval > 0) { /* got message */
				doanything_with(buf);
			}
		} while (rval != 0);
		close(msgsock);
		/* or shutdown(msgsock, 2); no difference */
	} while(1);

but it doesn't do what I want.
to test it I send messages in a loop to the server, (send is the client prog)

main()
{
	int i;
	for (i=0; i<=100; i++) {
		system("send host text");
		sleep(1); 	/* If I enter this line, it will give the
			        best performance, whith or without
				the setsockoptions and with or without
				shutdown(socket,[0-2]) */
	}
}

Is this a bug ? I am uesing BSD 4.2 and SYS V.2.
Am I doing something wrong ?
can anyone give me a solution/help/hint ?

I can't make the client sleep, or is there a systemcall to make a
user fall asleep ?? :-)

Thanks for any answer.

Robin Tawab
NIXDORF COMPUTER AG
Dep DTSW 2-4
Berliner Str. 95
D-8000 Munic (West Germany)
Tel.: xxx-89/3601-3171
eunet: tawab.muc at nixpbe.UUCP
!USA: mcvax!unido!nixpbe!tawab.muc
USA:  uunet!linus!nixbur!tawab.muc



More information about the Comp.unix.questions mailing list