将字节数组从java程序发送到c++

Posted

技术标签:

【中文标题】将字节数组从java程序发送到c++【英文标题】:Send byte array from java program to c++ 【发布时间】:2014-04-19 00:55:22 【问题描述】:

我目前正在学习网络,需要一些帮助来找出通过 UDP 将字节数组从 Java 程序发送到 C++ 程序的最简单方法。以前,我用 Java 创建了一个非常简单的客户端和服务器程序,并且能够使用 Java 类 DatagramSocket 和 DatagramPacket 在两个 Java 客户端/服务器程序之间发送和接收数据包。

但是现在,我有一个需要通过的网络模拟器,它是用 C++ 编写的,所以我担心它无法识别 DatagramSocket 和 DatagramPacket 数据,因为它们是 Java 类。

如果我有用 C++ 编写的网络模拟器的主机名和侦听 UDP 端口,我将如何用 Java 向它发送字节数组?

【问题讨论】:

【参考方案1】:

您担心 Java 类不被识别是错误的,因为当您编写 UDP 数据包时,您使用的是不同的 API(UDP 是 network protocol)。您没有序列化 Java 对象。

下面是一个工作示例,Java 客户端将 UDP 数据包发送到 PureData(即非 Java)服务器(来自 codesounding):

package codesounding;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/*
  Every instruction type triggers an UDP message, each on different ports:
            Variable declaration:   7001
            Start block ():        7002
            End block ():          7003
            break:                  7004
            continue:               7005
            do:                     7006
            for:                    7007
            if:                     7008
            return:                 7009
            throw:                  7010
            while:                  7011
 */
public class UDPProcessor extends SilentProcessor 
    private static final String TARGET = "target"; //target host name
    private final DatagramPacket packetVar;
    private final DatagramPacket packetStart;
    private final DatagramPacket packetEnd;
    private final DatagramPacket packetBreak;
    private final DatagramPacket packetContinue;
    private final DatagramPacket packetDo;
    private final DatagramPacket packetFor;
    private final DatagramPacket packetIf;
    private final DatagramPacket packetReturn;
    private final DatagramPacket packetThrow;
    private final DatagramPacket packetWhile;
    final DatagramSocket socket; 
    final byte[] signal = "1;\n".getBytes(); //this message will trigger both PureData and MaxMSP UDP receivers; see the example patches

    public UDPProcessor() 
        try 
            String target = "127.0.0.1";
            if (System.getProperty(TARGET) != null  && !System.getProperty(TARGET).equals("")) 
                target = System.getProperty(TARGET);
            
            System.out.println("Sending to "+target+"...");
            socket = new DatagramSocket();
            InetAddress address = InetAddress.getByName(target);

            packetVar = new DatagramPacket(signal, signal.length, address, 7001);
            packetStart = new DatagramPacket(signal, signal.length, address, 7002);
            packetEnd = new DatagramPacket(signal, signal.length, address, 7003);
            packetBreak = new DatagramPacket(signal, signal.length, address, 7004);
            packetContinue = new DatagramPacket(signal, signal.length, address, 7005);
            packetDo = new DatagramPacket(signal, signal.length, address, 7006);
            packetFor = new DatagramPacket(signal, signal.length, address, 7007);
            packetIf = new DatagramPacket(signal, signal.length, address, 7008);
            packetReturn = new DatagramPacket(signal, signal.length, address, 7009);
            packetThrow = new DatagramPacket(signal, signal.length, address, 7010);
            packetWhile = new DatagramPacket(signal, signal.length, address, 7011);

         catch (Exception e) 
            throw new RuntimeException(e);
        
    

    private synchronized void send(DatagramPacket packet) 
        try 
            socket.send(packet);
         catch (IOException e) 
            e.printStackTrace();
            //skipping 
        
    

    public void getVarDeclaration() 
        send(packetVar);
    

    public void getStartBlock() 
        send(packetStart);
    

    public void getEndBlock() 
        send(packetEnd);
    

    public void getIfStatement() 
        send(packetIf);
    

    public void getForStatement() 
        send(packetFor);
    

    public void getDoStatement() 
        send(packetDo);
    

    public void getWhileStatement() 
        send(packetWhile);
    

    public void getReturnStatement() 
        send(packetReturn);
    

    public void getBreakStatement() 
        send(packetBreak);
    

    public void getContinueStatement() 
        send(packetContinue);
    

    public void getThrowStatement() 
        send(packetThrow);
    


【讨论】:

以上是关于将字节数组从java程序发送到c++的主要内容,如果未能解决你的问题,请参考以下文章

如何将从 C++ 发送的 cv::MAT 字节数组转换为 Java 中的位图?

将字节数组从 c++ 传递到 c# 程序集都有哪些不同的方法?

将字节数组从 NodeJS 发送到 C 库

将字节数组从 c++ 传递到 c#(单声道)

linux到windows C++字节数组

如何将带有图像的字节数组从 AS3 发送到 PHP?