我有一个包含三台设备的场景:Android 智能手机、Linux 笔记本电脑 (Linux mint) 和索尼 a7R 相机。
我的目标是使用笔记本电脑与相机对话索尼的http-SDK通过 WiFi(Cam 打开热点)。 SDK 附带 Android 示例应用程序的代码。它首先发送 SSDP 数据包,然后摄像机用 UDP 数据包进行应答,到目前为止一切顺利。在 Linux 上使用相同的 java 代码,数据包也被发送,但从未收到答案。然而,所需的 UDP 数据包出现在笔记本电脑上(使用wireshark 验证)。
问: 包裹在哪里?如何让java应用程序在linux上接收数据包?
这是代码(基于索尼的示例应用程序)。对于 Android 和普通 Java 来说,它几乎是相同的。
/*
* Copyright 2014 Sony Corporation
*/
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class JavaSsdpTest {
public static void main(String[] args) {
search();
}
private static final String TAG = "MainActivity";
private static final int SSDP_RECEIVE_TIMEOUT = 10000; // msec
private static final int PACKET_BUFFER_SIZE = 2048;
private static final int SSDP_PORT = 1900;
private static final int SSDP_MX = 1;
private static final String SSDP_ADDR = "239.255.255.250";
private static final String SSDP_ST = "urn:schemas-sony-com:service:ScalarWebAPI:1";
public static class Log {
static void i(final String tag, final String msg) {
System.out.println(tag + ": " + msg);
}
}
public static synchronized void search() {
final String ssdpRequest =
"M-SEARCH * HTTP/1.1\r\n" + String.format("HOST: %s:%d\r\n", SSDP_ADDR, SSDP_PORT)
+ String.format("MAN: \"ssdp:discover\"\r\n")
+ String.format("MX: %d\r\n", SSDP_MX)
+ String.format("ST: %s\r\n", SSDP_ST) + "\r\n";
final byte[] sendData = ssdpRequest.getBytes();
new Thread() {
@Override
public void run() {
// Send Datagram packets
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
InetSocketAddress iAddress = new InetSocketAddress(SSDP_ADDR, SSDP_PORT);
DatagramPacket packet = new DatagramPacket(sendData, sendData.length, iAddress);
socket.send(packet);
Log.i(TAG, "Sent packages");
} catch (Exception e) {
Log.i(TAG, "send: Something went wrong:");
System.exit(1);
}
// Receive reply packets
byte[] array = new byte[PACKET_BUFFER_SIZE];
try {
DatagramPacket receivePacket = new DatagramPacket(array, array.length);
socket.setSoTimeout(SSDP_RECEIVE_TIMEOUT);
socket.receive(receivePacket);
Log.i(TAG, "Received package: " + receivePacket.getData().length);
System.exit(0);
} catch (Exception e) {
Log.i(TAG, "recv: something went wrong");
System.exit(1);
}
}
}.start();
}
}