0

I have the following problem: I have to send a list of bytes from my android phone to an old server with udp. Because in java the bytes are signed, the server receive always to many bytes. Instead of 36 bytes he has 72 bytes. So, the interpretation of the bytes list is not work on serverside.

A correct interpretation on the server is: 23 80 82 88 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,41413 9,3971 879m 273,2° 0km/h

My wrong result is: 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

Has someone an idea how I have to send this byte list? It should be 36 unsigned bytes?

My android app code is:

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();
        }
    }
}

}

1 Answers1

2

You need to convert your udpMsg hex string to a byte array using something like so:

import javax.xml.bind.DatatypeConverter;
sendData = DatatypeConverter.parseHexBinary(udpMsg);

edit: try this method instead, I guess Android doesn't have the XML classes.

public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
    data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                         + Character.digit(s.charAt(i+1), 16));
}
return data;
}
Community
  • 1
  • 1
Hiro2k
  • 5,254
  • 4
  • 23
  • 28
  • Hello, it works. Many thanks to Hiro2k. Now, the string on the server is correct, I see the string on the server log, but the server doesn't the right action after receive the string. he does nothing! wa could be the problem? client port? Do I have to implement to receive the answer from the udp server? He sends a "*" back if the string is fine. Thx – cheafnebur Feb 21 '13 at 10:06
  • I accepted the answer, thanks. :) Do you have an idea what the problem could be that the server doesn't any actions after receiving my string? In the log of the Server the string seems to be right. If I send the same data from an emulator the string is exactly the same on the server and it works fine. It's a windows server. What could be the reason that the server accept the string only from the emulator and not from my phone? – cheafnebur Feb 22 '13 at 14:29
  • That's very strange, but I don't have a clue unfortunately. I suggest you ask a new question just for that problem since I'm not an android dev and I suspect someone else has already run into something similar. – Hiro2k Feb 22 '13 at 18:16
  • Ok, I opened a new question. Another Question to you: How can I decode the data byte array from your code to "UTF-16LE". Perhaps that could be the reason why the windows server doesn't recognize the byte list correctly. Thnx – cheafnebur Feb 22 '13 at 22:12
  • Hello @Hiro2k, could you explain me ho to format the byte list to UTF-XX in your code fragment: – cheafnebur Feb 24 '13 at 10:52
  • It's quite simple, new String(bytes, "UTF-16LE"); – Hiro2k Feb 24 '13 at 15:29
  • Hello Hiro2k.... yes I know this, but I don't know how to implement this in your code fragement (hexStringToByteArray) because there isn't any getBytes. – cheafnebur Feb 24 '13 at 16:26
  • That's supposed to be done on the server side, not the client. The client should be sending the bytes array to the server directly, not the UTF string. – Hiro2k Feb 24 '13 at 16:40
  • I have no access to this server. The server doesn't recognize the string and my idea is now to format the byte list on clientside to UTF before sending and perhaps the server recognize then the byte list – cheafnebur Feb 24 '13 at 17:13