1

I am trying to create a chat application between Android and a Windows 10 device. I have successfully sent text from Android using DataOutputStream and read it in Windows 10 using a data reader class.

My problem is Android is not able to recognize the text from Windows. It displays the result of the datainputstream.available() function but the application hangs in case I use the readString() or the readbyte() function.

Code in Android for receiving:

DataInputStream  dIn = new DataInputStream(clientSocket.getInputStream());
if(dIn.available()>0)
{
    int length = dIn.readInt();  // app hangs in here
    byte[] byteReceived = new byte[length];
    dIn.readFully(byteReceived, 0 , length); // sometimes app hangs here 
    String textReceived = new String(byteReceived);
    text.setText(Client Says: "+ textReceived + "\n");//
}

Data sent from Windows through datawriter:

DataWriter writer = new DataWriter(socket.OutputStream))
{
    writer.UnicodeEncoding=windows.Storage.Streams.UnicodeEncoding.Utf8;
    writer.ByteOrder = windows.Storage.Streams.ByteOrder.LittleEndian;
    uint size =writer.MeasureString(message); 
    writer.WriteUint32(size);
    writer.WriteString(message);
    try
    {
        await writer.StoreAsync();
    }
    catch (Exception exception)
    {
        switch (SocketError.GetStatus(exception.HResult))
        {
            case SocketErrorStatus.HostNotFound:
                // Handle HostNotFound Error
                throw;
            default:
                throw;
        }
    }

    await writer.FlushAsync();
    writer.DetachStream();
}

What is the issue here?

Daniel
  • 2,355
  • 9
  • 23
  • 30
  • Are the windows size write Int at first transaction? – Evgeniy Mishustin Jun 20 '16 at 11:24
  • What is a `DataWriter`, and why aren't you using a `DataOutputStream` to complement your `DataInputStream`, and where are you writing the length word that you are reading, and where is the call to `available()` that you describe? – user207421 Jun 20 '16 at 11:42
  • @EJP : Datawriter is the writer stream class in windows uwp. i am writing the work to a textbox – harishdvvvvv Jun 20 '16 at 11:51
  • @EvgeniyMishustin : yes i wrote in windows the writeInt before writing my string – harishdvvvvv Jun 20 '16 at 11:51
  • Great, so you've answered 50% of the questions I asked. How about the rest of it? If you wrote the `writeInt()` why doesn't it appear in the code you posted? And why are you wasting your time calling `available()` at all? – user207421 Jun 20 '16 at 11:56
  • @EJP hello.sorry for the unclear explanation of my answers. i updated my code in windows side. available() is to check whether i receive something in android – harishdvvvvv Jun 20 '16 at 12:20

1 Answers1

0

Your dIn.readFully expects bytes and not String. Moreover, it expects the exact number of bytes, as length variable. You need to create bytes from String on the windows size and send the length of byte array as Int in first transaction. Then you need to transfer this byte array unchanged in second transaction. Try it.

Evgeniy Mishustin
  • 3,343
  • 3
  • 42
  • 81