Search code examples
javaandroidutf-8persian

Receive byte array from data with persian character


My code is a messenger that sends messages from an android device to another android device over WiFi.

Here's my code, in this class I send the message:

public class ClientThread extends Thread {

    String address;
    String message;
    int portNum;
    InetAddress serverAddr;
    Socket socket;
    PrintWriter out;
    boolean broadcast;

    public ClientThread(String addr, int port, String msg, boolean bcast) throws IOException {
        address = addr;
        portNum = port;
        message = msg;
        broadcast = bcast;
    }

    public void run() {
        try {

            ChatService.portsocket.setBroadcast(broadcast);
            // Charset.forName("UTF-8").encode(message);
            // Log.d("client",new String(message.getBytes()));
            // byte[] b = EncodingUtils.getBytes(message,
            // Charset.forName("UTF-8").name());
            DatagramPacket packet1 = new DatagramPacket(message.getBytes(), message.length(),
                InetAddress.getByName(address), 5555);
            ChatService.portsocket.send(packet1);

        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

and in this class I receive the message :

public class ChatService extends Service {

    public static String serverIP;
    public static int serverPort = 5555;
    private ServerSocket serverSocket;
    private serverThread sThread;
    public static final String BROADCAST_ACTION = "UpdateEvent";
    public static DatagramSocket portsocket;
    Intent intent;
    int i = 0;

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    private class serverThread extends Thread {

        public void run() {
            try {
                serverIP = getLocalIpAddress();
                Log.v("addr", serverIP);
                if (serverIP != null) {
                    serverSocket = new ServerSocket(serverPort);
                    Log.v("socket", "created");
                    while (true) {
                        /*
                         * Socket client = serverSocket.accept();
                         * Log.v("client", "received");
                         */
                        try {
                            byte [] buf = new byte [1024];
                            DatagramPacket packet = new DatagramPacket(buf, buf.length);
                            ChatService.portsocket.receive(packet);
                            byte [] result = new byte [packet.getLength()];
                            System.arraycopy(packet.getData(), 0, result, 0, packet.getLength());

                            String msg = new String(result);

                            Log.d("armin", msg);
                            updateGui(msg, packet.getAddress());

                        }
                        catch (Exception e) {
                        }
                    }
                }
            }
            catch (Exception e) {
            }
        }
    }
}

when I type for example آرمین and send it,the message the app receives is an incoherent word for example آر anyone has idea to fix it ?


Solution

  • If you have a

    String message = ...;
    

    this is converted to a byte[]

    byte[] bytes = message.getBytes();
    

    then the DatagramPacket must be constructed using

    new DatagramPacket(bytes, bytes.length(), ... );
    

    Your call uses

    new DatagramPacket( message.getBytes, message.length(),..,
    

    but this uses the String length but Farsi requires more than one byte per character.

    The string آرمین has 5 characters, but the UTF-8 encoding requires 10 bytes. You need to send 10 bytes.