Creates a server socket on port. Upon accept, a new thread is
created which calls:
(fun input-stream output-stream)
Optional arguments support specifying a listen backlog and binding
to a specific endpoint.
;; A Simple server that dumps the input stream to the output stream.
;; In this example, the create-server function is called with two
;; arguments, a port number and an anonymous function. The input
;; and output stream arguments passed to the anonymous function are
;; bound to the standard input and output respectively.
(use 'clojure.contrib.server-socket)
(import '(java.io BufferedReader InputStreamReader PrintWriter))
user=> (create-server
8080
(fn [in out]
(binding
[*in* (BufferedReader. (InputStreamReader. in))
*out* (PrintWriter. out)]
(loop []
(println (read-line))
(recur)))))
{:server-socket #<ServerSocket ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=8080]>, :connections #<Ref@73f17a73: #{}>}
(defn create-server
"Creates a server socket on port. Upon accept, a new thread is
created which calls:
(fun input-stream output-stream)
Optional arguments support specifying a listen backlog and binding
to a specific endpoint."
([port fun backlog ^InetAddress bind-addr]
(create-server-aux fun (ServerSocket. port backlog bind-addr)))
([port fun backlog]
(create-server-aux fun (ServerSocket. port backlog)))
([port fun]
(create-server-aux fun (ServerSocket. port))))
Comments top
No comments for create-server. Log in to add a comment.