There's no clean way to close an open file (network port or otherwise) in an application that doesn't expect it.
There is a way to close the file under its nose, but the application might not react well. There's a good chance it will crash, which would defeat the purpose. You can execute a system call in a remote process with the
ptrace system call. Use
netstat to find the file descriptor you're interested in. Then attach your favorite debugger to the process in question and make it execute a
shutdown) system call.
# Usage: shutdown-in-process PID FD
gdb -n -pid "$1" -batch -x /dev/stdin <<EOF
As this has a good chance of crashing the application, because its interface with its environment will no longer match its internal data structure, consider other approaches. In particular, if the purpose is to have a different application listening on a UDP or TCP port, you could redirect traffic to a different port at the level of the network layer (
iptables under Linux,
pfctl under BSD, …).