Is it possible to close a socket by using some sort of shell command/program? I've seen plenty of examples of manipulating sockets using C and other languages but have been unable to find a program dedicated to it.
This is mostly out of curiosity rather than any real need. Although it might be useful for stress-testing my application or closing a tcp socket that I don't want to be open.
I am mainly interested in Linux/Unix, but a way to do this on Windows would be nice also.
You can close a socket in sh just like any other file:
exec 42>&-
where 42 is the file descriptor. What you can't do (except in a few shells that provide an extension for it) is open a socket. But of course that closes the socket in the shell, not in another process.Closing a socket in a running process would disrupt its behavior in a way that the author of the program is not supposed to expect — it's like going in and modifying a piece of memory. Still, it can be done by connecting to the process with a debugger and making it execute a socket closing API call (that would be the
close
orshutdown
system call on unix).