Java:通过socket向/从C

时间:2016-02-20 18:47:24

标签: java arrays sockets

我想将一个完整的整数数组从Java发送到另一个用C编码的程序,反之亦然接收。

我从here读到我应该在Java中使用短数组,而在C程序中使用普通的int数组。

刚接触Java,我仍然不太确定如何正确地执行此操作。以下是我的代码:

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 < msg.length; ++i) {
            msg[i] = dataIn.readInt();
            System.out.print(msg[i] + " ");
            if(count % 2 == 1)
                System.out.print("\n");
            count++;
        }
        return msg;
    }
}

有关如何更正代码的任何指导意见!!

我是否需要将流从bytearray更改为inarray?听起来像很多转换..(基于我可以在这里找到的答案)

0 个答案:

没有答案