-2

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...

enter image description here

John K
  • 371
  • 5
  • 26
  • The exception you mention is not a result of data not being available. It is a result of the connection being reset by the peer for another reason entirely. – user207421 Jul 27 '17 at 10:10
  • @EJP Which can be that reason? because that heppen only if i put a delay before sending data in the Server if i don't put the `Thread.Sleep(2000)` in my TCPServer made in VB that didn't happen... – John K Jul 27 '17 at 10:12
  • The reasons for resetting an estabilshed connection are given in a large number of duplicates here.Typically, an application protocol error. – user207421 Jul 27 '17 at 10:40
  • @EJP so is there anyway to fix that problem? – John K Jul 27 '17 at 12:34

1 Answers1

0

You are getting error message "ECONNRESET" which means either server is rejecting the connection or the firewalls in between client or server are blocking the connection. In either case you have to fist check server logs.If server is refusing with some reason ,you will get that in server logs. If server is not resetting the connection which means at server there is no entry of ur connection, In that case u should try from open network and if it works the try tuning the firewall for allowing required ports or IP.

You can take help from the link Getting "SocketException : Connection reset by peer" in Android

  • Yea but actually i'm getting this error only if i put a delay when i send the data from the server ( i set that delay for emulate somethink like a network traffic ) while if i don't put that delay that work properly without giving that error... – John K Jul 27 '17 at 10:17
  • Ok but can you debug from server "logs" how it behaves differently in both situation.Depending upon the error we are facing on server we can fix the issue Recommending to make server logs more verbose. – Gaurav Srivastava Jul 27 '17 at 10:23
  • The OP is in the middle of `java.net.PlainSocketImpl.read`. He is doing a read. *Ergo* there was already a connection. ECONNRESET does not result from a rejected connection. It results from resetting an *established* connection. – user207421 Jul 27 '17 at 10:39
  • @EJP , So what is the reset reason? one can get that info from server logs only ie why it is resetting. Hence requesting to debug in server logs.Let's help I.Mytyuk :) – Gaurav Srivastava Jul 27 '17 at 10:56
  • Actually i can't give the Server logs because it's made in VB so there is no logcat here – John K Jul 27 '17 at 12:33