Note that there are some explanatory texts on larger screens.

plurals
  1. POAsync NetworkStream in Android
    primarykey
    data
    text
    <p>I got a client in android working with my custom async network, it works fine recieve fine. But, at some point, the server is sending 2 messages almost at the same time (2 diferent threads in server) But in client, when that happens, it only recieve the first message, already tried to use runnable, to stay recieving while parsing the message, but no succed. If someone can help me, how can I solve this problem.</p> <p>My recieve code:</p> <pre><code> interface RecieveCallback { void callback(int recievedBytes, InputStream stream); } public class Recieve implements Runnable { private RecieveCallback callback; private InputStream stream; private byte[] buffer; public Recieve(InputStream inStream, byte[] _buffer, RecieveCallback _callback) { callback = _callback; stream = inStream; buffer = _buffer; } public void run() { try { while (true) { final int recievedBytes = stream.read(buffer); (new Runnable() { public void run() { callback.callback(recievedBytes, stream); } }).run(); } } catch (IOException e) { Log.e("Recieve", e.getMessage()); } } } public Connection() { this(host, port); } public Connection(String ip, int _port) { inMessage = new NetworkMessage(); try { serverAddres = InetAddress.getByName(ip); socket = new Socket(serverAddres, _port); socketOut = socket.getOutputStream(); } catch (IOException e) { Log.e("Connect", e.getMessage()); } } public void Connect() { try { BeginRecieve(socket.getInputStream(), inMessage.getBuffer(), new RecieveCallback() { public void callback(int recievedBytes, InputStream stream) { OnRecieve(recievedBytes, stream); } }); } catch (IOException e) { Log.e("BeginRecieve", e.getMessage()); } } public boolean EndRead(int recievedBytes, InputStream stream) { int size = (int)inMessage.GetUInt32() + 4; while (recievedBytes &lt; size) { try { recievedBytes += stream.read(inMessage.getBuffer(), recievedBytes, (size - recievedBytes)); } catch (IOException e) { e.printStackTrace(); return false; } } inMessage.setLength(size); return true; } public void OnRecieve(int recievedBytes, InputStream stream) { inMessage.setPosition(0); if (!EndRead(recievedBytes, stream)) { return; } NetworkMessage tempMessage = inMessage; if (!ParseMessage(tempMessage)) { //Tratar erro } } public void BeginRecieve(InputStream inStream, byte[] _buffer, RecieveCallback _callback) { if (beginRecieve != null &amp;&amp; beginRecieve.isAlive()) { beginRecieve.interrupt(); } beginRecieve = new Thread(new Recieve(inStream, _buffer, _callback)); beginRecieve.start(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload