title: Java: Sending/Receiving int array via socket to/from a program coded in C
body: <p>I would like to send an entire integer array from Java to another program which is coded in C, and vice versa for receiving.</p>
  
  <p>I have read from <a href="https://stackoverflow.com/questions/16117681/sending-int-via-socket-from-java-to-c-strange-results">here</a> that i should use short array in Java while using normal int array in the C program.</p>
  
  <p>Being new to Java, I'm still not very sure how to do this correctly. Below is my code:</p>
  
  <pre><code>package tcpcomm;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.net.Socket;
  import java.net.UnknownHostException;
  import java.util.Scanner;
  import java.io.ByteArrayInputStream;
  import java.io.ByteArrayOutputStream;
  import java.io.DataInputStream;
  import java.io.DataOutputStream;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  import datatypes.Message;
  
  public class PCClient {
  
      public static final String C_ADDRESS = "192.168.1.1";
      public static final int C_PORT = 8888;
  
      private static PCClient _instance;
      private Socket _clientSocket;
          private ByteArrayOutputStream _toCProgram;
          private ByteArrayInputStream _fromCProgram;
  
      private PCClient() {
  
      }
  
      public static PCClient getInstance() {
          if (_instance == null) {
              _instance = new PCClient();
          }
          return _instance;
      }
  
      public static void main (String[] args) throws UnknownHostException, IOException {
          int msg[] = {Message.READ_SENSOR_VALUES};
          ByteArrayOutputStream out = new ByteArrayOutputStream();
  
      PCClient pcClient = PCClient.getInstance();
      pcClient.setUpConnection(C_ADDRESS, C_PORT);
      System.out.println("C program successfully connected");
      while (true) {
          pcClient.sendMessage(out, msg);
          ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
          int[] msgReceived = pcClient.readMessage(in);
        } 
      }
  
      public void setUpConnection (String IPAddress, int portNumber) throws UnknownHostException, IOException{
          _clientSocket = new Socket(C_ADDRESS, C_PORT);
          _toCProgram = new PrintWriter(_clientSocket.getOutputStream());
          _fromCProgram = new Scanner(_clientSocket.getInputStream());
      }
  
      public void closeConnection() throws IOException {
          if (!_clientSocket.isClosed()) {
              _clientSocket.close();
          }
      }
  
      public void sendMessage(OutputStream out, int[] msg) throws IOException {
          int count = 0;
          DataOutputStream dataOut = new DataOutputStream(out);
          dataOut.writeInt(msg.length);
          System.out.println("Message sent: ");
          for (int e : msg) {
              dataOut.writeInt(e);
              System.out.print(e + " ");
              if(count % 2 == 1)
                  System.out.print("\n");
              count++;
          }
          dataOut.flush();
      }
  
      public int[] readMessage(InputStream in) throws IOException {
          int count = 0;
          DataInputStream dataIn = new DataInputStream(in);
          int[] msg = new int[dataIn.readInt()];
          System.out.println("Message received: ");
          for (int i = 0; i &lt; msg.length; ++i) {
              msg[i] = dataIn.readInt();
              System.out.print(msg[i] + " ");
              if(count % 2 == 1)
                  System.out.print("\n");
              count++;
          }
          return msg;
      }
  }
  </code></pre>
  
  <p>Any guidance on how to correct the code appreciated!!</p>
  
  <p>Do i need to change the stream from bytearray to intarray? Sounds like a lot of conversion.. (Based on answers I can find here)</p>