安卓与linux服务器进行socket通信错误:BrokenPipeError 10C

我在linux上写了个python脚本,作为socket 服务器,安卓作为socket客户端,进行socket通信。linux的ip为192.168.80.100,socket端口为12345。
先启动socket服务器,再启动客户端,用客户端发送消息,服务器可以很好收到,但要对消息进行一段时间的处理,等到再发返回给客户端消息时,python就会出现:

 BrokenPipeError: [Errno 32] Broken pipe

这是安卓socket客户端代码:

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import  java.net.Socket;

public class MainActivity extends AppCompatActivity {
    private TextView result = null;
    private static final String HOST = "192.168.80.100";
    private static final int PORT = 12345;
    private Socket socket = null;
    private BufferedReader bufferedReader = null;
    private PrintWriter printWriter = null;
    private String string = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText eturl= findViewById(R.id.eturl);
        Button btn= findViewById(R.id.btn);

        initSocket();
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String message;
                message = eturl.getText().toString();
                if (socket.isConnected()){
                    if (!socket.isOutputShutdown()){
                        Toast.makeText(MainActivity.this,"正在发送,请稍等……",Toast.LENGTH_LONG).show();
                        sendMsg(message);
                    }
                }
            }
        });
    }

    protected void sendMsg(final String message){
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(socket.isConnected()){
                    if(!socket.isOutputShutdown()){
                        printWriter.println(message);
                    }
                }
            }
        }).start();
    }

    protected void initSocket(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    socket=new Socket(HOST,PORT);
                    bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    printWriter=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
                    if(socket!=null){
                        if (socket.isConnected()) {
                            if (!socket.isInputShutdown()) {
                                if ((string = bufferedReader.readLine()) != null) {
                                    Log.i("TAG","++"+string);
                                    string+="";
                                    handler.sendEmptyMessage(1);

                                } else {    //TODO
                                }
                            }
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public Handler handler;

    {
        handler = new Handler() {
            public void handleMessage(Message msg) {
                if (msg.what == 1) {
                    Log.i("TAG", "--" + msg);
                    result.setText(new StringBuilder().append(result.getText().toString()).append(string).toString());
                }
            }
        };
    }

}

可能是安卓客户端在收到消息前已经关闭了socket,但要怎么解决呢,时间很急,求大神请教!不胜感激

6个回答

建议参考一下:
socket编程—— 服务器遇到Broken Pipe崩溃
https://www.cnblogs.com/jingzhishen/p/3453727.html

网络连接断开了,一个是安卓客户端可能发现一段时间没有数据发送,就把连接断开了。如果服务端需要随时发送数据给客户端,需要两者之间建立心跳连接

但是安卓电源管理机制等,可能在手机休眠等的状况还是会进行连接关闭和app结束

qq_40939785
Eumenides-Z 具体到我这个要怎么改呢?
2 年多之前 回复

建议参考一下:
Python脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

socket编程—— 服务器遇到Broken Pipe崩溃
https://www.cnblogs.com/jingzhishen/p/3453727.html

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问