title: How to write array to OutputStream in Java?
body: <p>I want to send more than one random value though <code>Socket</code>. I think array is the best way to send them. But I don't know how to write an array to Socket <code>OutputStream</code>?</p>
  
  <p>My Java class:</p>
  
  <p>import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.Socket;
  import java.io.*;
  import java.util.Random;</p>
  
  <pre><code>public class NodeCommunicator {
  
      public static void main(String[] args) {
          try {
              Socket nodejs = new Socket("localhost", 8181);
  
              Random randomGenerator = new Random();
              for (int idx = 1; idx &lt;= 1000; ++idx){
                  Thread.sleep(500);
                  int randomInt = randomGenerator.nextInt(35);
                  sendMessage(nodejs, randomInt + " ");
                  System.out.println(randomInt);
              }
  
              while(true){
                  Thread.sleep(1000);
              }
          } catch (Exception e) {
              System.out.println("Connection terminated..Closing Java Client");
              System.out.println("Error :- "+e);
          }
      }
  
      public static void sendMessage(Socket s, String message) throws IOException {
          s.getOutputStream().write(message.getBytes("UTF-8"));
          s.getOutputStream().flush();
      }
  
  }
  </code></pre>