roni
roni

Reputation: 41

java client and C# server working with unicode

I'm trying to build a tcp client and a server that will work with unicode. the server is in C# and looks like that:

    static void Main(string[] args)
    {
        // tcp setup
        TcpListener serverSocket = new TcpListener(8888);
        TcpClient clientSocket = default(TcpClient);
        serverSocket.Start();

        // waiting for client to connect
        clientSocket = serverSocket.AcceptTcpClient();

        // client comunication
        string resAscii = Recv(clientSocket);
        Send(clientSocket, "got from you: " + resAscii);
        string resUnicode = Recv(clientSocket);
        Send(clientSocket, "קיבלתי ממך: " + resUnicode);
        Console.ReadKey();
    }

    public static void Send(TcpClient client, string msg)
    {
        Byte[] sendtBytes = Encoding.Unicode.GetBytes(msg + "$");
        client.GetStream().Write(sendtBytes, 0, sendtBytes.Length);
        client.GetStream().Flush();
    }
    public static string Recv(TcpClient client)
    {
        byte[] recvBytes = new byte[65537];
        client.GetStream().Read(recvBytes, 0, (int)client.ReceiveBufferSize);
        string dataFromClient = System.Text.Encoding.Unicode.GetString(recvBytes);
        dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf('$'));
        return dataFromClient;
    }

and the client is in java (android):

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Socket s = new Socket("192.168.0.102", 8888);
                Send(s, "this is ascii");
                String asciiString = Recv(s);
                Send(s, "זה יוניקוד");
                String unicodeString = Recv(s);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}


private String Recv(Socket s) throws IOException {
    byte[] b = new byte[10080];
    int read = s.getInputStream().read(b, 0, 1000);
    String ret = new String(b, "UTF16");
    ret = ret.substring(0, ret.indexOf('$'));
    return ret;
}

private void Send(Socket s, String msg) throws IOException {
    s.getOutputStream().write((msg + "$").getBytes("UTF16"));
}

When I'm sending from the client to the server I get the message. But when the server sending to the client I get only Chinese letters: client (android) error how can I fix it?

Upvotes: 0

Views: 279

Answers (1)

roni
roni

Reputation: 41

As @flakes said that was the answer. change this line in the Recv function:

    String ret = new String(b, "UTF16");

with this line:

        String ret = new String(b, StandardCharsets.UTF_16LE);

and

    s.getOutputStream().write((msg + "$").getBytes("UTF16"));

with this line in the Send file:

        s.getOutputStream().write((msg + "$").getBytes(StandardCharsets.UTF_16LE));

Upvotes: 1

Related Questions