how to send a byte array in socket java

http‮//:s‬www.lautturi.com
how to send a byte array in socket java

To send a byte array over a socket in Java, you can use the OutputStream and write methods of the Socket class.

Here is an example of how you can do this:

byte[] byteArray = {1, 2, 3, 4, 5};

Socket socket = new Socket("localhost", 1234);

OutputStream outputStream = socket.getOutputStream();
outputStream.write(byteArray);

outputStream.close();
socket.close();

In this example, we create a Socket object and connect it to the specified host and port. We then get the OutputStream of the socket and write the byte array to it using the write method. Finally, we close the output stream and the socket to release the resources.

On the server side, you can use the InputStream and read methods of the Socket class to receive the byte array.

Created Time:2017-11-01 20:43:00  Author:lautturi