Clojure中的套接字编程 [英] Socket programming in Clojure
问题描述
我是Clojure菜鸟,请多多包涵。我正在尝试编写一个简单的程序,以通过套接字发送命令并获得响应。我的代码是:
I'm a Clojure noob, so bear with me. I'm trying to write a simple program to send a command through a socket and get a response. My code is:
(def IPaddress "10.71.18.81")
(def port 1500)
(def socket (new Socket IPaddress port))
(print (clojure.string/join ["\nConnected to HSM: " (. socket isConnected)]))
(def in (DataInputStream. (BufferedInputStream. (. socket getInputStream))))
(def out (DataOutputStream. (BufferedOutputStream. (. socket getOutputStream))))
(def command "Some string")
(. out WriteUTF command)
(def response (. in readUTF))
(print (clojure.string/join ["Output from HSM: " response]))
错误为线程主中的异常java.lang.IllegalArgumentException:没有匹配的方法:writeUTF。我在理解Java互操作和访问对象方法等方面遇到困难。预先感谢。
The error is "Exception in thread "main" java.lang.IllegalArgumentException: No matching method: writeUTF". I'm having trouble understanding Java interop, and accessing object methods, etc. Thanks in advance.
编辑:如果有人感兴趣,我的最终工作代码包括在这里:
If anyone is interested, my final working code is included here:
(def IPaddress "10.71.18.81")
(def port 1500)
(def socket (Socket. IPaddress port))
(println "Connected:" (.isConnected socket))
(def in (DataInputStream. (BufferedInputStream. (.getInputStream socket))))
(def out (DataOutputStream. (BufferedOutputStream. (.getOutputStream socket))))
(def command "Some string")
(println "Input:" command)
(.writeUTF out command)
(.flush out)
(def response (.readUTF in))
(println "Output: " response)
推荐答案
Java是区分大小写的,因此应该为(。out writeUTF命令)
。请注意,互操作的首选语法是(。writeUTF out命令)
,它等效于您的语句
Java is case sensite so it should be (. out writeUTF command)
. Note that the prefer syntax for interop is (.writeUTF out command)
which is equivalent to your statement
这篇关于Clojure中的套接字编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!