次の問題があります。AndroidフォンからUDPを使用して古いサーバーにバイトのリストを送信する必要があります。Javaではバイトが署名されているため、サーバーは常に多くのバイトを受信します。36バイトの代わりに彼は72バイトを持っています。したがって、バイトリストの解釈はサーバー側では機能しません。
サーバーでの正しい解釈は次のとおりです。23808288 F2 F2 8C F7 B8 FC 9B 88 98 91 82 84 89 80 90 E5 AA 82 C0 83 AB BE 87 BC D3 C0 95 80 80 16 0D 0A 354242055139836 2013-19-02 17:16:18 47,414139,3971879m273,2°0km/h
私の間違った結果は次のとおりです:32 33 38 30 38 32 38 38 46 32 46 32 38 43 46 37 42 38 46 43 39 42 38 38 39 38 39 31 38 32 38 31 38 30 38 30 39 30 45 35 44 32 38 32 43 30 38 33 41 42 42 42 38 37 41 43 44 33 43 30 39 35 38 30 38 30 37 44 30 44 30 41
このバイトリストをどのように送信する必要があるか誰かが考えていますか?署名されていない36バイトである必要がありますか?
私のAndroidアプリのコードは次のとおりです。
public class UdpClient extends IntentService {
public static final String REQUEST_STRING = "sharedText";
public UdpClient() {
super("UdpClient");
}
@Override
public void onHandleIntent(Intent intent) {
String requestString = intent.getStringExtra(REQUEST_STRING);
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/*".equals(type)) {
runUdpClient(requestString); // Handle text being sent
}
}
}
@SuppressLint("DefaultLocale")
public static String getSum(String in) {
int checksum = 0;
if (in.startsWith("$")) {
in = in.substring(1, in.length());
}
int end = in.indexOf('*');
if (end == -1)
end = in.length();
for (int i = 0; i < end; i++) {
checksum = checksum ^ in.charAt(i);
}
String hex = Integer.toHexString(checksum);
if (hex.length() == 1)
hex = "0" + hex;
return hex.toUpperCase();
}
private static final int UDP_SERVER_PORT = 11111;
@SuppressLint("DefaultLocale")
private void runUdpClient(String string) {
String sharedText = string.toUpperCase();
String checksum = null;
if (sharedText != null) {
// Update UI to reflect text being shared
checksum = getSum(sharedText);
}
String udpMsg = "23" + sharedText + checksum + "0D0A";
DatagramSocket ds = null;
try {
ds = new DatagramSocket();
InetAddress serverAddr = InetAddress.getByName("127.0.0.1");
DatagramPacket dp;
byte[] sendData = new byte[36];
sendData = udpMsg.getBytes("ISO-8859-1");
dp = new DatagramPacket(sendData, sendData.length, serverAddr,
UDP_SERVER_PORT);
ds.send(dp);
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ds != null) {
ds.close();
}
}
}
}