How to write array to OutputStream in Java?

Use java.io.DataOutputStream / DataInputStream pair, they know how to read ints. Send info as a packet of length + random numbers.

sender

Socket sock = new Socket("localhost", 8181);
DataOutputStream out = new DataOutputStream(sock.getOutputStream());
out.writeInt(len);
for(int i = 0; i < len; i++) {
      out.writeInt(randomGenerator.nextInt(35))
...

receiver

 DataInputStream in = new DataInputStream(sock.getInputStream());
 int len = in.readInt();
 for(int i = 0; i < len; i++) {
      int next = in.readInt();
 ...

Java arrays are actually Objects and moreover they implement the Serializable interface. So, you can serialize your array, get the bytes and send those through the socket. This should do it:

public static void sendMessage(Socket s, int[] myMessageArray)
   throws IOException {
  ByteArrayOutputStream bs = new ByteArrayOutputStream();
  ObjectOutputStream os = new ObjectOutputStream(bs);
  os.writeObject(myMessageArray);
  byte[] messageArrayBytes = bs.toByteArray();
  s.getOutputStream().write(messageArrayBytes);
  s.getOutputStream().flush();
}

What's really neat about this is that it works not only for int[] but for any Serializable object.

Edit: Thinking about it again, this is even simpler:

sender:

public static void sendMessage(Socket s, int[] myMessageArray)
   throws IOException {
  OutputStream os = s.getOutputStream();  
  ObjectOutputStream oos = new ObjectOutputStream(os);  
  oos.writeObject(myMessageArray); 
}

receiver:

public static int[] getMessage(Socket s)
   throws IOException {
  InputStream is = s.getInputStream();  
  ObjectInputStream ois = new ObjectInputStream(is);  
  int[] myMessageArray = (int[])ois.readObject(); 
  return myMessageArray;
}

I'm leaving my first answer also as(it also works and) it's useful for writing objects to UDP DatagramSockets and DatagramPackets where there is no stream.


I would advice to simply concatenate the int values in a string using some delimiter e.g. @@, then transmit the final concatenated string at once. On the receiving side, just split the string using same delimiter to get the int[] back e.g.:

    Random randomGenerator = new Random();
    StringBuilder numToSend = new StringBuilder("");
    numToSend.append(randomGenerator.nextInt(35));

    for (int idx = 2; idx <= 1000; ++idx){
        Thread.sleep(500);
        numToSend.append("@@").append(randomGenerator.nextInt(35));
    }
    String numsString = numToSend.toString();
    System.out.println(numsString);
    sendMessage(nodejs, numsString);

On the receiving side, get your string and split as:

   Socket remotejs = new Socket("remotehost", 8181);
   BufferedReader in = new BufferedReader(
                               new InputStreamReader(remotejs.getInputStream()));
   String receivedNumString = in.readLine();
   String[] numstrings = receivedNumString.split("@@");
   int[] nums = new int[numstrings.length];
   int indx = 0;
   for(String numStr: numstrings){
     nums[indx++] = Integer.parseInt(numStr);
   }