I'm trying to set the client that will open the connection and will listen for new data available
But actually if the data is not available instantly or like with a delay of some ms my app give the following error:
> 07-27 08:50:09.732 27021-27153/com.example.sguidetti.selfmanegment
> E/TCP Client: C: Connecting... 07-27 08:50:09.802
> 27021-27153/com.example.sguidetti.selfmanegment E/SERVER: S: ONLINE
> 07-27 08:50:09.803 27021-27153/com.example.sguidetti.selfmanegment
> E/RESPONSE FROM SERVER: S: Received Message: '3' 07-27 08:50:11.424
> 27021-27021/com.example.sguidetti.selfmanegment V/Monotype:
> SetAppTypeFace- try to flip, app = com.example.sguidetti.selfmanegment
> 07-27 08:50:11.424 27021-27021/com.example.sguidetti.selfmanegment
> V/Monotype: Typeface getFontPathFlipFont - systemFont =
> default#default 07-27 08:50:11.698
> 27021-27180/com.example.sguidetti.selfmanegment E/TCP Client: C:
> Connecting... 07-27 08:50:14.127
> 27021-27180/com.example.sguidetti.selfmanegment E/TCP: S: Error
> java.net.SocketException: recvfrom failed: ECONNRESET (Connection
> reset by peer)
> at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:592)
> at libcore.io.IoBridge.recvfrom(IoBridge.java:556)
> at java.net.PlainSocketImpl.read(PlainSocketImpl.java:485)
> at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
> at
> java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
> at java.io.InputStreamReader.read(InputStreamReader.java:231)
> at java.io.BufferedReader.fillBuf(BufferedReader.java:145)
> at java.io.BufferedReader.readLine(BufferedReader.java:397)
> at com.example.sguidetti.selfmanegment.Client.run(Client.java:95)
> at
> com.example.sguidetti.selfmanegment.help$ConnectTask.doInBackground(help.java:127)
> at
> com.example.sguidetti.selfmanegment.help$ConnectTask.doInBackground(help.java:112)
> at android.os.AsyncTask$2.call(AsyncTask.java:288)
> at java.util.concurrent.FutureTask.run(FutureTask.java:237)
> at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
> at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
> at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
> at java.lang.Thread.run(Thread.java:818)
> Caused by: android.system.ErrnoException: recvfrom failed: ECONNRESET
> (Connection reset by peer)
> at libcore.io.Posix.recvfromBytes(Native Method)
> at libcore.io.Posix.recvfrom(Posix.java:161)
> at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:250)
> at libcore.io.IoBridge.recvfrom(IoBridge.java:553)
> at java.net.PlainSocketImpl.read(PlainSocketImpl.java:485)
> at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
> at
> java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
> at java.io.InputStreamReader.read(InputStreamReader.java:231)
> at java.io.BufferedReader.fillBuf(BufferedReader.java:145)
> at java.io.BufferedReader.readLine(BufferedReader.java:397)
> at com.example.sguidetti.selfmanegment.Client.run(Client.java:95)
> at
> com.example.sguidetti.selfmanegment.help$ConnectTask.doInBackground(help.java:127)
> at
> com.example.sguidetti.selfmanegment.help$ConnectTask.doInBackground(help.java:112)
> at android.os.AsyncTask$2.call(AsyncTask.java:288)
> at java.util.concurrent.FutureTask.run(FutureTask.java:237)
> at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
> at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
> at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
> at java.lang.Thread.run(Thread.java:818) 07-27 08:50:14.130
> 27021-27217/com.example.sguidetti.selfmanegment E/TCP Client: C:
> Connecting...
For my TCP Client i've used the following GUIDE by just adding in the TCPClient.class the following string mRun = false;
under the mMessageListener.messageReceived(mServerMessage);
For testing i've just added a Thread.Sleep(2000)
in my TCP Server made in VB before it will make the data available whyle the 1st data at the start is getting fine because is without any delay.
Here is my activity where i use the TCP Client for the 2nd time and where it give the error if there is some delay:
Start client function that repeat it x times
public void startClient(){
for(int i=0; i< Integer.valueOf(MainActivity.SelfNumber); i++) {
new ConnectTask().execute("");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
client.sendMessage("C" + Integer.valueOf(i+1));
}
}
And here is my AsyncTask where i decode the text sent by the Server and put it in a RecyclerView
public class ConnectTask extends AsyncTask<String, String, Client> {
@Override
protected Client doInBackground(String... message) {
client = new Client(new Client.OnMessageReceived() {
@Override
public void messageReceived(String message) {
publishProgress(message);
}
});
client.run();
return null;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
StringTokenizer stringTokenizer = new StringTokenizer(values[0], "#");
nCassa = stringTokenizer.nextToken();
status = stringTokenizer.nextToken();
receipt = stringTokenizer.nextToken();
eur = stringTokenizer.nextToken();
adapter = new SelfAdapter("CASSA SELF N° " + nCassa, "EUR: " + eur, "SC: " + receipt, help.img);
selfList.add(help.adapter);
adapterView.notifyDataSetChanged();
}
}
EDIT Actually there is no delay at the 1st Client-Server Connection so as you can see in the screenshot the server got the IP string from the client and the client got number 3, but when i run another activity where i open another time the connection and the response from the server has a delay you can see in the screen what is happening, and the Server got also CS1 string from the Client when it had to be on delay...