Alessandro Alessandro - 10 months ago 44
Android Question

TCP Server gets ???????? instead of the real message

The server is written in c# and works correctly with a client I made in c#, now i'm trying to make an android client but the server doesn't get the real message, it gets just a lot of question marks.

Here is the server

TcpListener listen = new TcpListener(IPAddress.Any, 1200);
TcpClient client;
client = listen.AcceptTcpClient();
NetworkStream stream = client.GetStream();
byte[] buffer = new byte[client.ReceiveBufferSize];
int data = stream.Read(buffer, 0, client.ReceiveBufferSize);
string message = Encoding.Unicode.GetString(buffer, 0, data);

this is the Android client

EditText et = (EditText) findViewById(;
String str = et.getText().toString()
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),

For example if I send the message "Hello", the server prints "???????????" and it happens the same for any message I send, even just 1 letter

I also tried with different methods like this one but the result is the same:

DataOutputStream out = new DataOutputStream(socket.getOutputStream());



string message = Encoding.Unicode.GetString(buffer, 0, data);


string message = Encoding.UTF8.GetString(buffer, 0, data);

Try also UTF16. I think java uses this encoding now.