Welcome! Please see the About page for a little more info on how this works.

+1 vote
in IO by

I want to use some libs in IO for text file and binary file,not using ugly synax like .write,.read.
then, how about libs for network socket??

1 Answer

+2 votes
by
edited by

For files, resources and other streams have a look at clojure.java.io namespace.

Note that java interop is idiomatic Clojure — it's a valid tool, not a last resort, so you should not look at is as ugly. Here is a network socket example:

$ clj
Clojure 1.10.1
user=> (def socket (java.net.Socket. "127.0.0.1" 5555))
#'user/socket
user=> (require '[clojure.java.io :as io])
nil
user=> (def out (io/writer (.getOutputStream socket)))
#'user/out
user=> (def in (io/reader (.getInputStream socket)))
#'user/in
user=> (.write out "(+ 1 2 3)\n")
nil
user=> (.flush out)
nil
user=> (.readLine in)
"user=> 6"

You can test it by starting socket repl on port 5555 in a separate terminal:

$ clj -J-Dclojure.server.repl='{:port 5555 :accept clojure.core.server/repl}'
...