UDP Packet received okay in Java but corrupted in Python
<p>I am trying to record audio from an Android tablet and send it to a python server. At the start of the byte packet, I include some relevant information about the state of the Android app (A byte array called "actives" -- but considering it's receiving fine by a Java server, this should not be relevant). The android code is as follows:</p>

<pre><code>                   int read = recorder.read(buffer, 0, buffer.length);

                    for (int a = 0; a &lt; actives.length; a++) {
                        outBuffer[a+1] = (byte)actives[a];
                        logger = logger + Byte.toString(actives[a]) + ",";
                    }
                    int furthest=0;
                    for(int a =0; a &lt; buffer.length; a++){
                        outBuffer[actives.length+1+a]=buffer[a];
                        if(buffer[a]!=0)furthest=a;
                    }
                    packet = new DatagramPacket(outBuffer, read,
                            serverAddress, PORT);
                    Log.d("writing", logger+Byte.toString(outBuffer[7])+".length"+Integer.toString(1+furthest+actives.length+1));
                    Log.d("streamer","Packet length "+outBuffer.length);
                    try {
                        socket.send(packet);
                    }catch (IOException e){
                        Log.e("streamer", "Exception: " + e);
                    }
                    Log.d("streamer","packetSent");
</code></pre>

<p>I receive a clean signal on the other end using a Java server.
Image of received java output: !(<a href="http://i.imgur.com/31UWzya.png" rel="nofollow">http://i.imgur.com/31UWzya.png</a>)
This is my Java server:</p>

<pre><code>DatagramSocket serverSocket = new DatagramSocket(3001);
int byteSize=970;
byte[] receiveData = new byte[byteSize];
DatagramPacket receivePacket = new DatagramPacket(receiveData,
        receiveData.length);
while(true){        // recieve data until timeout
                        try {
                            serverSocket.receive(receivePacket);
                            String rcvd = "rcvd from " + receivePacket.getAddress();
                           System.out.println("receiver"+"Received a packet!" +rcvd);

                            break;
                        }
                        catch (Exception e) {
                            // timeout exception.
                            System.out.println("Timeout reached without packet!!! " + e);
            timeoutReached=true;
                            break;

                        }
                 }
if(timeoutReached)continue;
    currTime = System.currentTimeMillis();

    data = receivePacket.getData();
</code></pre>

<p>Here is my Python server's output:
!(<a href="http://i.imgur.com/RYkcCCE.png" rel="nofollow">http://i.imgur.com/RYkcCCE.png</a>)
And here is the code:</p>

<pre><code>import socket
ip="192.ip.address"
port=3001;
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM);
sock.bind(('',port));
while(True):
    data,addr=sock.recvfrom(970);
    print("address",addr);
    print("received a data!");
    print(data);
</code></pre>

<p>In the last line of the python script, I have tried to change "print(data)" to "print(data.decode())", in which case I get this error: </p>

<pre><code>UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)
</code></pre>

<p><strong>I am not running these servers at the same time</strong>
My guess is that it has to do something with Java using unsigned ints and python not doing that. Is there a way in Python that I can convert this data, because data.decode() is not working? Alternatively I should be able to convert the data in Java somehow? None of the answers on stackoverflow that I have tried have worked. </p>

<p>Decoding is the right approach. In your android app explicitly mention the character encoding. <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/charset/StandardCharsets.html#UTF_8" rel="nofollow">UTF-8</a> is the standard Charset that is used.</p>

<p>Your log is pretty clear. You are trying to decode the data packet as ASCII (which is the default encoding of the <a href="https://docs.python.org/2/library/codecs.html#codecs.decode" rel="nofollow">decode() function</a>) but I'm guessing its ISO_8859_1 or UTF-8 (more likely).</p>

<p>Next try <code>data.decode('utf8', 'ignore')</code> in your android app. Note: <code>'ignore'</code> is an optional argument and to be used only in case of debugging as it will ignore malformed(corrupted) data and try to convert individual characters. If you want to use decode() in production use '<code>strict'</code> or no second argument (<code>'strict'</code> is the default).</p>

<p>In place of <code>'utf8'</code> try other options from other <a href="https://docs.python.org/2/library/codecs.html#module-codecs" rel="nofollow">Python Encodings</a>.</p>

java android python udp datagram