数据可用时如何将数据传输到TCP客户端?

时间:2017-07-27 06:57:07

标签: java android tcp

我正在尝试设置将打开连接的客户端并将侦听可用的新数据

但实际上,如果数据无法立即获得或延迟某些 ms ,我的应用会出现以下错误:

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

对于我的TCP客户端,我使用了以下GUIDE,只需在mRun = false;

下添加TCPClient.class中的以下字符串mMessageListener.messageReceived(mServerMessage);

为了测试,我刚刚在我的TCP服务器中添加Thread.Sleep(2000),然后才能使数据可用,并且开始时的第一个数据正常,因为没有任何延迟。

这是我的活动,我第二次使用TCP客户端,如果有一些延迟,它会给出错误:

启动重复x次的客户端功能

    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));

    }
}

这是我的AsyncTask,我解码服务器发送的文本并将其放入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();


        }

    }

修改 实际上第一个客户端 - 服务器连接没有延迟,因此您可以在屏幕截图中看到服务器从客户端获得IP字符串,客户端获得了数字3,但是当我运行另一个活动时,我再次打开连接和来自服务器的响应有一个延迟,你可以在屏幕上看到发生了什么,并且当必须延迟时,服务器也从客户端获得CS1字符串......

enter image description here

1 个答案:

答案 0 :(得分:0)

您收到错误消息“ECONNRESET”,这意味着服务器拒绝连接或客户端或服务器之间的防火墙阻止连接。在任何一种情况下,你都必须先检查服务器日志。如果服务器因某种原因拒绝,你将在服务器日志中得到它。如果服务器没有重置连接,这意味着在服务器上没有ur连接的条目,在这种情况下你应该尝试从开放网络,如果它工作,尝试调整防火墙以允许所需的端口或IP。

您可以从链接中获取帮助 Getting "SocketException : Connection reset by peer" in Android