I am trying to write a TCP client in Clojure for a networking assignment.
While everyone else is using C or C#, I elected to use Clojure and Java
sockets, but am naturally running into problems and can't figure out for
the life of me what I am doing wrong.
The goal is to send a message to the instructor's server and process the
response from the server. The request message must meet the following
requirements:
- Request must be encoded as bytes, where the header - the first two
bytes - represents a short integer conveying the length of the request
(excluding the header itself)
- Message must be sent in Big Endian byte order
The following code shows my TCP client:
*(defn- receive*
* "Given a socket create a DataInputStream and process the server*
* response"*
* [reader socket]*
* (let [bytes-in (make-array Byte/TYPE util/max-message-size)*
* info {:type :receive :socket socket :count
util/max-message-size}]*
* (log info)*
* (.read reader bytes-in 0 util/max-message-size)*
* bytes-in))*
*(defn- transmit*
* "Given a socket create a DataOutputStream and process the request to*
* the server"*
* [writer socket message]*
* (let [bytes-out (req/make-request (into {:socket socket} message))*
* begin-info {:type :begin-transmit :socket socket :message
bytes-out}*
* end-info {:type :end-transmit :writer writer}]*
* (log begin-info)*
* (.write writer bytes-out 0 (count bytes-out))*
* (log end-info)*
* (.flush writer)))*
*(defn send-request*
* "Given a create a Socket and process the request"*
* [msg]*
* (with-open [socket (java.net.Socket. (:host-ip msg) (:host-port msg))*
* writer (DataOutputStream. (.getOutputStream socket))*
* reader (DataInputStream. (.getInputStream socket))]*
* (log {:type :client})*
* (transmit writer socket msg)*
* (receive reader socket)))*
And here is the output this produces by calling send-request with some test
data:
Initializing the TCP client ...
Transmitting request
-------------------------------
Message Size : 124 bytes
Host IP : /192.168.xxx.xxx
Host Port : 2605
0 121 124 82 69 81 124 0 0 1 69 -38 63 103 2 124 108 10 75 27 12 99 82 52
109 107 58 111 16 95 45 13 114 127 3 69 124 71 108 101 97 115 111 110 78
105 99 107 111 108 105 99 104 68 124 49 57 45 49 53 57 54 124 48 124 47 49
57 50 46 49 54 56 46 49 46 49 50 51 124 53 52 51 54 50 124 50 49 124 47 49
57 50 46 49 54 56 46 49 48 49 46 50 49 48 124 50 54 48 53 124 72 101 108
108 111 32 116 104 101 114 101 124 49
Bytes written: 124 bytes
Receiving response
--------------------------------
Maximum Size : 146 bytes
Host IP : /192.168.xxx.xxx
Host Port : 2605
.....
No response from the server. It looks like my byte array is correct (as you
can see the byte at index 0 is 0, followed by 121 representing the length
of the request -- though the "bytes written" does appear to be 1 more than
it should). Also, my understanding is that DataOutputStream sends data in
Network order by default -- is this correct?
My next step is to try writing an echo server to see if I can at least
communicate to myself. In the mean time, if anyone has any insight, it
would be much appreciated!
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.