I've got a minimal TCP server running in the SML/NJ
REPL, and I'm wondering how to gracefully close the listener socket on a keyboard interrupt. A stripped-down version of the server is
fun sendHello sock =
let val res = "HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello world!\r\n\r\n"
val slc = Word8VectorSlice.full (Byte.stringToBytes res)
in
Socket.sendVec (sock, slc);
Socket.close sock
end
fun acceptLoop serv =
let val (s, _) = Socket.accept serv
in print "Accepted a connection...\n";
sendHello s;
acceptLoop serv
end
fun serve port =
let val s = INetSock.TCP.socket()
in Socket.Ctl.setREUSEADDR (s, true);
Socket.bind(s, INetSock.any port);
Socket.listen(s, 5);
print "Entering accept loop...\n";
acceptLoop s
end
The problem is that if I start this server listening on a port, cancel with a keyboard interrupt, then try to restart on the same port, I get an error.
Standard ML of New Jersey v110.76 [built: Thu Feb 19 00:37:13 2015]
- use "test.sml" ;;
[opening test.sml]
[autoloading]
[library $SMLNJ-BASIS/basis.cm is stable]
[autoloading done]
val sendHello = fn : ('a,Socket.active Socket.stream) Socket.sock -> unit
val acceptLoop = fn : ('a,Socket.passive Socket.stream) Socket.sock -> 'b
val serve = fn : int -> 'a
val it = () : unit
- serve 8181 ;;
stdIn:2.1-2.11 Warning: type vars not generalized because of
value restriction are instantiated to dummy types (X1,X2,...)
Entering accept loop...
Accepted a connection...
C-c C-c
Interrupt
- serve 8181 ;;
stdIn:1.2-1.12 Warning: type vars not generalized because of
value restriction are instantiated to dummy types (X1,X2,...)
uncaught exception SysErr [SysErr: Address already in use [<UNKNOWN>]]
raised at: <bind.c>
-
So I'd like to be able to close out the listening socket when some error occurs. I see Interrupt
in the REPL when I issue a keyboard interrupt, so I assumed that Interrupt
is the constructor of the exception I'm expected to catch. However, adding the appropriate handle
line to either acceptLoop
or serve
doesn't seem to do what I want.
fun acceptLoop serv =
let val (s, _) = Socket.accept serv
in print "Accepted a connection...\n";
sendHello s;
acceptLoop serv
end
handle Interrupt => Socket.close serv
fun serve port =
let val s = INetSock.TCP.socket()
in Socket.Ctl.setREUSEADDR (s, true);
Socket.bind(s, INetSock.any port);
Socket.listen(s, 5);
print "Entering accept loop...\n";
acceptLoop s
handle Interrupt => Socket.close s
end
(then in REPL)
- use "test.sml" ;;
[opening test.sml]
val sendHello = fn : ('a,Socket.active Socket.stream) Socket.sock -> unit
val acceptLoop = fn : ('a,Socket.passive Socket.stream) Socket.sock -> 'b
val serve = fn : int -> 'a
val it = () : unit
- serve 8182 ;;
stdIn:3.1-3.11 Warning: type vars not generalized because of
value restriction are instantiated to dummy types (X1,X2,...)
Entering accept loop...
Accepted a connection...
C-c C-c
Interrupt
- serve 8182 ;;
stdIn:1.2-1.12 Warning: type vars not generalized because of
value restriction are instantiated to dummy types (X1,X2,...)
uncaught exception SysErr [SysErr: Address already in use [<UNKNOWN>]]
raised at: <bind.c>
-
Doing the same with a variable (handle x => (Socket.close s; raise x)
) or wildcard (handle _ => Socket.close s
) exception match has the same effect as above.