0

I have this code to send an array of bytes through a socket:

byte[] code_b = code.getBytes("US-ASCII");
byte[] id_b = id.getBytes("US-ASCII");
byte[] username_b = username.getBytes("US-ASCII");

ByteBuffer chapAsk = ByteBuffer.allocate(lengthmsg);

chapAsk.put(code_b);
chapAsk.put(id_b);
chapAsk.putShort((short)(lengthmsg));
chapAsk.put(username_b);
chapAsk.array(); //several bytes together - concat

sal.println(chapAsk); //with this I send the array through the socket

With this I send the array but now I want to receive the array on the Socket Server and save the bytes in a new array. How can I do this??

  • Would this http://stackoverflow.com/questions/10475898/receive-byte-using-bytearrayinputstream-from-a-socket be what you need? – StephaneM Feb 28 '14 at 12:57
  • possible duplicate of [How to get data from TCP socket into a ByteBuffer](http://stackoverflow.com/questions/12478845/how-to-get-data-from-tcp-socket-into-a-bytebuffer) – hammerfest Feb 28 '14 at 12:57
  • I don't think [array()](http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html#array()) does what you think it does. And what type is _sal_? If it is a `PrintStream` something is not right. – McDowell Feb 28 '14 at 12:58
  • It is supposed that array() will be code_b+id_b+lenghtmsg+username_b together, I mean the bytes! And that is what I want to send over the TCP socket then READ THEM as bytes on the Server. And sal is: PrintWriter sal = new PrintWriter (sClnt.getOutputStream() ,true); – user3364832 Feb 28 '14 at 13:57

0 Answers0