I have some code like the below, using java sockets from matlab. I don't think the solution needs to be matlab-specific though...
Problem: if I hit debug-quit before the socket can get closed by server_socket.close, it stays dangling/open. The only workaround that seems to work is stopping and restarting matlab, which I guess kills the background socket. How can I more generally close the ServerSocket, if it is left open by this function?
function server_socket = my_server( opts )
% Run the server, my_server.m
import java.net.ServerSocket
import java.io.*
server_socket = ServerSocket(6001);
server_socket.setSoTimeout(5000);
io_socket = server_socket.accept();
fprintf(1, 'Client connected\n');
% DO STUFF 1
BreakPointHere = 1;
% DO STUFF 2
% clean up
server_socket.close; % badness if 'dbquit' before this
io_socket.close;
end
I've tried googling for this but am not quite sure what keywords to use and there is information-overload with regard to java sockets...
EDIT: One solution is to make this a script instead of a function (i.e. delete the first line and last 'end'). That way the server_socket persists after debug kill. But I'd like to understand better where the sockets are handled, and don't want all the other variables to persist necessarily.