Java 新的DataOutputStream(输出); dataOut.writeInt(整数长度); for(int e:int s)dataOut.writeInt(e); dataOut.flush(); } 私有静态int[]readInts(InputStream in)引发IOException{ DataInputStream dataIn=新的DataInputStream(in); int[]ints=new int[dataIn.readInt()]; 对于(inti=0;i

Java 新的DataOutputStream(输出); dataOut.writeInt(整数长度); for(int e:int s)dataOut.writeInt(e); dataOut.flush(); } 私有静态int[]readInts(InputStream in)引发IOException{ DataInputStream dataIn=新的DataInputStream(in); int[]ints=new int[dataIn.readInt()]; 对于(inti=0;i,java,arrays,sockets,Java,Arrays,Sockets,您只需使用方法writeInts(OutputStream,int[])和readInts(InputStream)并使用套接字流调用它们 出于演示目的,我没有实现C程序,因为没有标准的套接字实现(虽然有库,但它不是标准的,不是C而是C++) 玩得开心 您可以在两端使用ASCII和parse之类的文本编码。对于C程序,我应该在C文件的顶部添加它们吗#包括typedef int int32_t;typedef unsigned int uint32\u t;typedef size_t uint3

您只需使用方法
writeInts(OutputStream,int[])
readInts(InputStream)
并使用套接字流调用它们

出于演示目的,我没有实现C程序,因为没有标准的套接字实现(虽然有库,但它不是标准的,不是C而是C++)


玩得开心

您可以在两端使用ASCII和parse之类的文本编码。对于C程序,我应该在C文件的顶部添加它们吗#包括typedef int int32_t;typedef unsigned int uint32\u t;typedef size_t uint32_t;谢谢你的详细解释。帮助我更多地了解C和java@乔,没问题。是的,您需要在源代码中包含
stdint.h
,但不需要类型定义(这就是标题的作用)。根据您为
int32\t
编译的系统,它本身就是
int
。大多数情况都是这样。但有几次不是这样,
int32\t
被定义为等同于
int
的东西。它增加了代码的可移植性。我有一个问题!我实现了上面的方法,但在我的原始代码中这一部分如何:_toCProgram=newprintwriter(_clientSocket.getOutputStream())_fromCProgram=新扫描仪(_clientSocket.getInputStream());如何让您的
DataOutputStream&DataInputStream
通过声明的套接字发送/接收?谢谢,希望很快收到你的来信@DoeJoe,我的代码中的
ByteArrayOutputStream
ByteArrayInputStream
仅用于演示目的。我对你的代码做了一些调整,现在你可以从你的套接字读写了。无需使用
扫描仪
打印机
。您可以在此处找到新代码:
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;
    }
}
package com.acme;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;

public class SendAndReceive {

    public static void main(String[] args) throws IOException {
        int[] ints = new int[] {Integer.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE};

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        writeInts(out, ints);
        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
        int[] results = readInts(in);

        if (!Arrays.equals(ints, results)) System.out.println("Damn!");
        else System.out.println("Aaall's well!");
    }

    private static void writeInts(OutputStream out, int[] ints) throws IOException {
        DataOutputStream dataOut = new DataOutputStream(out);
        dataOut.writeInt(ints.length);
        for (int e : ints) dataOut.writeInt(e);
        dataOut.flush();
    }

    private static int[] readInts(InputStream in) throws IOException {
        DataInputStream dataIn = new DataInputStream(in);
        int[] ints = new int[dataIn.readInt()];
        for (int i = 0; i < ints.length; ++i) ints[i] = dataIn.readInt();
        return ints;
    }
}