In server, I have send a byte array to client through Java socket
byte[] message = ... ;
DataOutputStream dout = new DataOutputStream(client.getOutputStream());
dout.write(message);
How can I receive this byte array from client?
In server, I have send a byte array to client through Java socket
byte[] message = ... ;
DataOutputStream dout = new DataOutputStream(client.getOutputStream());
dout.write(message);
How can I receive this byte array from client?
You need to either have the message be a fixed size, or you need to send the size or you need to use some separator characters.
This is the easiest case for a known size (100 bytes):
In this case
DataInputStream
makes sense as it offersreadFully()
. If you don't use it, you need to loop yourself until the expected number of bytes is read.There is a JDK socket tutorial here, which covers both the server and client end. That looks exactly like what you want.
(from that tutorial) This sets up to read from an echo server:
taking a stream of bytes and converts to strings via the reader and using a default encoding (not advisable, normally).
Error handling and closing sockets/streams omitted from the above, but check the tutorial.
Try this, it's working for me.
Sender:
Receiver:
First, do not use
DataOutputStream
unless it’s really necessary. Second:Of course this lacks any error checking but this should get you going. The JDK API Javadoc is your friend and can help you a lot.