"unbind" a socket...?
Posted: Thu Jan 02, 2014 1:04 pm
Hi. I'm trying to learn beginning network programming. I wrote a little SBCL code to accept a connection and then close it:
It works, in that if I run (serve 5555) I can then use a telnet client to connect to localhost:5555 (which disconnects immediately). However, if afterwards I try to run the serve function again with the same port number, I get the error "<SB-BSD-SOCKETS:ADDRESS-IN-USE-ERROR>". If I change the port number, it will work once with that port, and then get the aforementioned error always after that.
I get the same problem trying to code something using usocket, which suggests a fundamental misunderstanding on my part. Is there a cleanup step I'm missing? (Like, "unbinding" the socket somehow?)
Code: Select all
(defun serve (port)
(let ((sock (make-instance 'inet-socket :type :stream :protocol :tcp)))
(progn
(socket-bind sock (make-inet-address "127.0.0.1") port)
(socket-listen sock 100)
(let ((connection (socket-accept sock)))
(socket-close connection)))))
I get the same problem trying to code something using usocket, which suggests a fundamental misunderstanding on my part. Is there a cleanup step I'm missing? (Like, "unbinding" the socket somehow?)