6

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.

1 Answer 1

6

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 or shutdown system call on unix).

2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .