gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:54:08 26 4
gpt4 key购买 nike

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

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

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

最佳答案

您担心 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 - 将字节数组从java程序发送到c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22393042/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com