简体   繁体   English

软件导致连接中止:即使从最近的列表中清除了该应用程序,套接字写入错误(它在与服务不同的线程中运行)

[英]Software caused connection abort: socket write error on clearing the app from recent list even (it running in a different thread from a service)

I've made an app in which I have Start button and when I press it, it starts the Client Thread Service in which the client Socket connects to my PC through Server Socket and it runs well till the app is cleared from recent list. 我制作了一个具有“开始”按钮的应用程序,当我按下该按钮时,它会启动“客户端线程服务”,其中客户端套接字通过服务器套接字连接到我的PC,并且运行良好,直到从最近的列表中清除了该应用程序为止。 The same doesn't happen when I press Start button and execute finish() to close the app. 当我按下开始按钮并执行finish()关闭应用程序时,不会发生同样的事情。 The Service runs in both cases but when I clear the app from recent list, the Service keeps running but I receive an error in my Server App "Software caused connection abort: socket write error". 该服务在两种情况下均运行,但是当我从最近的列表中清除该应用程序时,该服务保持运行,但是我的服务器应用程序中出现错误“软件导致连接中止:套接字写入错误”。 And when I use finish() to close the app, it also gets cleared from recent list but it the service keeps running service successfully without the socket getting any error. 当我使用finish()关闭应用程序时,它也会从最近的列表中清除,但是它可以使服务成功运行,而套接字不会出现任何错误。

What should I do?? 我该怎么办??

And Sorry for my English, it is not my native language. 对不起,我的英语不是我的母语。

MainActivity.java MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent serviceIntent = new Intent(getBaseContext(), ClientService.class);
}

//This is called when I click the start button
public void ServiceToggle(View v) {
    if (!isServiceRunning(ClientService.class)) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                startService(serviceIntent);
            }
        }).start();
    } else {
        new Thread(new Runnable() {
            @Override
            public void run() {
                stopService(serviceIntent);
            }
        }).start();
    }
}

ClientService.java ClientService.java

public class ClientService extends Service {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onCreate();
        handler = new Handler();
        new Thread(new Client()).start();

        return START_STICKY;
    }

    class Client implements Runnable {
        String message;

        @Override
        public void run() {
            try {
                socket = new Socket(IP, PORT);

                objectInputStream = new ObjectInputStream(socket.getInputStream());

                while (true) {
                    message = (String) objectInputStream.readObject();

                    UpdateOnUI(message);
                }
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void UpdateOnUI(final String string) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

The error is from the Client Side(Android) not from the Server Side(Windows) and is different from other questions asked in Stack overflow. 该错误是来自客户端(Android)而非服务器端(Windows),并且与堆栈溢出中提出的其他问题不同。 This error is thrown when the App is closed from the Task, the Service keeps running and the socket is implemented inside the Service the too the comes when closing the activity and not Service. 当从任务关闭应用程序,服务继续运行并且套接字在服务内部实现时,抛出此错误,关闭活动而不是服务时也是如此。

It works now. 现在可以使用了。 Just needed to make the service work in Foreground. 只需使服务在前景中工作即可。 Making it Foreground didn't work before because of a silly mistake, I was doing it in another thread but forgot to execute it (didn't call .start()). 由于一个愚蠢的错误,使前台无法正常工作,我在另一个线程中进行了该操作,但忘记了执行它(未调用.start())。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 软件导致连接中止:套接字写入错误 - Software caused connection abort: socket write error jvisualvm:软件导致连接中止:套接字写入错误 - jvisualvm: Software caused connection abort: socket write error 软件导致连接异常终止:写入DataOutputStream时套接字写入错误 - Software caused connection abort: socket write error while writing to DataOutputStream “软件导致连接中止:套接字写入错误”的官方原因 - Official reasons for "Software caused connection abort: socket write error" 在FTP中存储ObjectOutputStream导致java.net.SocketException:软件导致连接中止:套接字写入错误 - Store ObjectOutputStream in FTP causes java.net.SocketException: Software caused connection abort: socket write error java.net.SocketException:软件导致连接中止:重新提交请求时套接字写入错误 - java.net.SocketException: Software caused connection abort: socket write error when resubmitting the request javax.servlet.jsp.JspException:无法插入页面…软件导致连接中止:套接字写入错误 - javax.servlet.jsp.JspException: Can't insert page … Software caused connection abort: socket write error 通过串行端口java.net.SocketException的通信:软件导致连接中止:套接字写入错误 - Communication through serial port, java.net.SocketException: Software caused connection abort: socket write error Maven Web项目在eclipse(tomcat 7)中运行错误:错误:无法调用Tomcat管理器:软件导致连接中止:套接字写入错误 - The maven web project runs error in eclipse(tomcat 7):error:Cannot invoke Tomcat manager: Software caused connection abort: socket write error Java错误 - 软件导致连接中止:recv失败 - Java error - Software caused connection abort: recv failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM