Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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).

share|improve this answer
    
Just a note: it seems that bash can open TCP ports. catonmat.net/blog/tcp-port-scanner-in-bash Also as a sidenote, zsh doesn't support /dev/tcp –  Jason Axelson Aug 31 '12 at 0:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.