Octave socket close - sockets

I'm trying to use Octave to open a simple socket server. While debugging, my script crashed after it had bound to a port. Of course, subsequent binds to the same port now fail. How can I close the socket so that I can reuse the port? Right now all I can do is close active entirely, which kills the process that is running the listener.
Ric

To prevent this from happening in the future, you can use onCleanup or unwind_protect to ensure that the socket-closing code always happens, even if your script errors out unexpectedly.

Related

How is Ctrl+C key behaves in a TCP connection

I'm studying network and specifically tcp connection and i wondering in a situation that you connect remotely to a server using tcp connection and sending command line to execute some actions, How they handle sending a ctrl+c signals?
Is it sends a normal tcp package that in data section describes ctrl+c hits?
or is it sends a package that have RST flag turned on or FIN flag to cut or close the connection?
There's no such thing as sending a signal over TCP.
Ctrl+C is a terminal generated signal. Assuming you (or the running process) didn't change the terminal's settings, this means that the terminal driver transforms the Ctrl+C key combination into a kill(x, SIGINT), where x is the process group ID of the foreground process group (and as such, SIGINT is delivered to every process in the foreground process group, which, in your case, is probably just one process).
What the process does when the signal is delivered is not the terminal driver's business. The process may have ignored the signal, so nothing happens. Or it may have installed a signal handler, and do some work inside the signal handler (like writing something to the socket that when read by the receiver will cause it to send SIGINT to itself - this emulates a "remote signal delivery"). Or it may have blocked the signal - in that case, the signal is delivered when the process unblocks it, or it is canceled if the process ignores it in the meantime.
If, on the other hand, you (or the running process) changed the terminal settings such that Ctrl+C is not interpreted as a signal-generating key combination, then the process will read Ctrl+C from input. Of course, what happens depends on what the process does with the input that it reads.
In short, if you didn't change the default behavior for SIGINT and you didn't change your terminal's settings, Ctrl+C raises SIGINT; the default action is to terminate the process, and so the socket will be closed and the connection terminated.

Racket not closing TCP port

I've written a simple HTTP echo server in Racket. When I run the server from within DrRacket and then click the Stop button, my program terminates, but the port that was being used takes an annoyingly long time to close. If I run lsof -i :<port> in my terminal after terminating the program, I don't see anything bound to that port, but DrRacket disagrees and refuses to let me restart my program, telling me that something is already bound to that port.
Is this a bug in Racket, or is there something that I'm missing?
If you are using tcp-listen directly (meaning that you handle all the low-level socket stuff yourself, and manually handle HTTP too), you need to call it with the reuse? parameter set to #t.
If you are using the web-server module, it already sets reuse? to #t so it should already work.

Why does Eclipse want incoming network connections (using OS X)?

Does anyone know why Eclipse is asking for incoming network connections? I searched around and saw mentions of Code Completion with PyDev, but I disabled code completion in PyDev, restarted, and was still prompted for incoming network connections.
PyDev spawns a shell and connects to it to request information on builtin modules (besides using it to debug a process).
I.e.: The shell is spawn at https://github.com/fabioz/Pydev/blob/master/plugins/org.python.pydev/src_completions/org/python/pydev/editor/codecompletion/shell/PythonShell.java
and used in:
https://github.com/fabioz/Pydev/blob/master/plugins/org.python.pydev/src_completions/org/python/pydev/editor/codecompletion/revisited/modules/CompiledModule.java
So, this may be triggered at multiple places in PyDev (whenever a code completion, code analysis or indexing is done it may be requested and a request for any builtin module is done -- i.e.: anything in forced builtins as explained in http://www.pydev.org/manual_101_interpreter.html).
So, in order to function properly, PyDev really needs that connection to work (otherwise completions and code analysis may not work properly), which means you really have to clear it in your firewall (at least for local connections -- remote connections are only needed if you're going to use the remote debugger).
I'm not sure if this is the only reason but at least one of them could be that PyDev's debugger listens for connections from clients being debugged (this is on 3.9.1 and 3.9.2).
That is, when you debug an application, the TCP flow is from the application to PyDev/Eclipse. Hence it could be that Eclipse needs it just in case you'll want to debug things in the future.
What port is it, 5678? That's the debugger's port.
This is just a wild guess, perhaps this is it in your situation?

Passive socket of IOLib throws out EADDRINUSE

IOLib allows to create a passive socket to listen the clients' connection, before listen is called, we need to call (bind-address) to bind the socket to an specified address/port.
Well, the problem is that the first time I bind the socket to a port, it runs well, then I use C-c C-c in slime to terminate the thread, and run the program again, this time it throws out exception of EADDRINUSE:
<SOCKET-ADDRESS-IN-USE-ERROR 98 :EADDRINUSE "address already in use", FD: 10>
I already set the reuse_addr option to bind-address like that:
(bind-address socket
+ipv4-unspecified+
:port 1080
:reuse-addr t)
But I don't think this is the problem, because when I did the same thing in C, I use Ctrl+C to terminate the process, I can rebind the port, but in slime, the only solution is to restart emacs, it's really not conveninent, so How can I solve this problem, thanks.
When you exit a process, any open file descriptors (including network sockets) are closed, which is why it seems to work in C but not in CL. When a thread terminates, however, this doesn't happen. You'll find that you'll get the desired behavior by using the restart-inferior-lisp command in SLIME.
Not all is lost, however. If you wrap the function in the thread in an UNWIND-PROTECT form, you can arrange for the socket to be closed when the function is exited.

How can I force a refresh of what ports have listeners

I'm trying to re-launch a WCF service that I killed earlier, but I'm getting an AddressAlreadyInUseException. The port it's attempting to use is 1819.
I ran netstat -nao from the command line, and have found there is a listening process on port 1819, that has a PID of 4840. I went into Process Explorer (from SysInternals) to try to kill PID 4840, but it's not there.
I'm guessing PID 4840 was the WCF service running earlier (that I killed) but it didn't clear out the connections. How can I force a refresh of these ports being listened in on? Otherwise I'll have to reboot every time this happens.
It doesn't look like there's a way to refresh it. For now I have reconfigured the service to use another port until it's more convenient for me to restart.
I had same problem, the only way to make my port free and refreshed is just by restarting the computer. It is a bit tedious but that was the only way for me to solve the problem