0

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?

5
  • 1
    try getBytes(StandardCharsets.UTF_16LE)? stackoverflow.com/questions/33175545/… Commented Jan 12, 2017 at 17:20
  • "this is ascii": saying it doesn't make it so; It's UTF-16 because that's what a string is. Commented Jan 12, 2017 at 17:42
  • @flakes If you where next to me I would kiss you... Commented Jan 12, 2017 at 18:31
  • @roni lol glad it worked out :-) Commented Jan 12, 2017 at 18:33
  • Possible duplicate of Java unicode byte parsing Commented Jan 12, 2017 at 18:37

1 Answer 1

1

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));
Sign up to request clarification or add additional context in comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.