To read and write bytes via Socket

2013-08-28 Thread Yoshinori Kohyama
Hello all.

Please help me to read and write bytes via Socket.

For example, assumed that I want to send 5 bytes [0x00, 0x01, 0x7f, 0x80, 
0xff] to a TCP server.

I did:
(require '[clojure.java.io :refer :all])
(with-open [s (java.net.Socket. *ip.of.test.server* *port_of_test_server*
)]
  (let [^java.io.BufferedWriter wtr (writer s)
^chars obuf (char-array (map char [0 1 127 128 255]))]
(.write wtr obuf 0 5)
(.flush wtr

Then, my test server received bytes: [0x00, 0x01, 0x7f, 0xc2, 0x80, 0xc3, 
0xbf].

* I think I should use a char array so that read() requires 'char[]'.
* What is the valid char value to send a byte 0x80?
* How can I make the char value from 0x80 int?
* How are things about read.
* Does character encoding environment affect? (I use -Dfile.encoding=UTF-8)

Thank you in advance.

Yoshinori Kohyama

-- 
-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: To read and write bytes via Socket

2013-08-28 Thread Armando Blancas
With UTF-8 characters are encoded in a variable number of bytes according 
to various ranges, as you can see here in section Description:
http://en.wikipedia.org/wiki/UTF-8
 
Since you're sending your bytes as chars but read back as bytes, your last 
two bytes create chars that later produce two bytes each, and thus you see 
seven bytes at the other end. You may want to get an output stream from the 
socket and write single bytes directly.

On Wednesday, August 28, 2013 7:13:31 PM UTC-7, Yoshinori Kohyama wrote:

 Hello all.

 Please help me to read and write bytes via Socket.

 For example, assumed that I want to send 5 bytes [0x00, 0x01, 0x7f, 0x80, 
 0xff] to a TCP server.

 I did:
 (require '[clojure.java.io :refer :all])
 (with-open [s (java.net.Socket. *ip.of.test.server* *port_of_test_server
 *)]
   (let [^java.io.BufferedWriter wtr (writer s)
 ^chars obuf (char-array (map char [0 1 127 128 255]))]
 (.write wtr obuf 0 5)
 (.flush wtr

 Then, my test server received bytes: [0x00, 0x01, 0x7f, 0xc2, 0x80, 0xc3, 
 0xbf].

 * I think I should use a char array so that read() requires 'char[]'.
 * What is the valid char value to send a byte 0x80?
 * How can I make the char value from 0x80 int?
 * How are things about read.
 * Does character encoding environment affect? (I use 
 -Dfile.encoding=UTF-8)

 Thank you in advance.

 Yoshinori Kohyama


-- 
-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: To read and write bytes via Socket

2013-08-28 Thread Yoshinori Kohyama
Armando, thank you for your reply.

Writing single value with
  void write(int c)
sends same 2 bytes for values larger than 127 as using
  void write(char[] cbuf, int off, int len)
of BufferedReader 
http://docs.oracle.com/javase/jp/6/api/java/io/BufferedWriter.html

Code:
  (require '[clojure.java.io :refer :all])
  (with-open [s (java.net.Socket. *ip.of.test.server* *port_of_test_server
*)]
(let [^java.io.BufferedWriter wtr (writer s)]
  (doseq [i [0 1 127 128 255]]
(.write wtr i))
  (.flush wtr)))
sends same bytes as code in my previous post.

I can't find other appropriate methods of BuffererdReader, Reader and 
OutputStreamReader
  http://docs.oracle.com/javase/jp/6/api/java/io/Writer.html
  http://docs.oracle.com/javase/jp/6/api/java/io/OutputStreamWriter.html
than
  void write(int c)
  void write(char[] cbuf)
  void write(char[] cbuf, int off, int len)
.

Does anyone have any information?
Thanks in advance.

Yoshinori Kohyama

-- 
-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: To read and write bytes via Socket

2013-08-28 Thread Yoshinori Kohyama
Resolved.

I overlooked that OutputStream has
  void write(byte[] b, int off, int len)
.

With write(byte[] ...) of OutputStream instead of write(char[] ...) of 
BufferedWritersends I can send the bytes that I want to send.

Code:
  (require '[clojure.java.io :refer :all])
  (with-open [s (java.net.Socket. *ip.of.test.server* *port_of_test_server
*)]
(let [^java.io.OutputStream os (.getOutputStream s)
  ^bytes obuf (byte-array (map #(byte (if ( % 128) % (- % 256)))
   [0 1 127 128 255]))]
  (.write os obuf 0 5)
  (.flush os)))

sent [0x00, 0x01, 0x7f, 0x80, 0xff] to my test server.

Thank you, Armando and all.

Regards,
Yoshinori Kohyama


-- 
-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.