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

时间:2014-03-13 23:18:50

标签: java c++ sockets network-programming

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

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

如果我有用C ++编写的网络仿真器的主机名和监听UDP端口,我该如何用Java发送字节数组呢?

1 个答案:

答案 0 :(得分:2)

您对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);
    }

}