The problem is GetBufferSize method, I send a buffer size of 40 and it returns some weird number 54124.
Why is this dose the java int byte code different from C# int byte code?
JAVA CLIENT
mBufferOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), UTF8)), true);
private void sendMessage(final String message) {
if (mBufferOut != null && message != null) {
try {
Log.d("_TAG", "Message length: " + Integer.toString(message.length()));
Log.d("_TAG", "Sending: " + message);
mBufferOut.print(message.length());
mBufferOut.flush();
mBufferOut.print(message);
mBufferOut.flush();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
The code above sends two messages: the first one is the buffer size and the second is the data itself.
C# SERVER
private void Read() {
int size = GetBufferSize();
byte[] myReadBuffer = new byte[size];
int numberOfBytesRead = 0;
string str = "";
do
{
numberOfBytesRead = networkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
str = Encoding.UTF8.GetString(myReadBuffer, 0, numberOfBytesRead);
} while (networkStream.DataAvailable);
}
private int GetBufferSize()
{
byte[] myReadBuffer = new byte[4];
int numberOfBytesRead = 0;
do
{
numberOfBytesRead = networkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
} while (networkStream.DataAvailable);
if (numberOfBytesRead > 0)
{
return BitConverter.ToInt32(myReadBuffer, 0);
}
else
{
return 0;
}
}
Any ideas?