Android和Linux服务器套接字通信错误

时间:2018-06-03 15:32:17

标签: android sockets

我在Linux上编写了一个python脚本作为套接字服务器,Android作为套接字通信的套接字客户端。 linux的ip是192.168.80.100,套接字端口是12345.Android和Linux服务器套接字通信错误首先启动套接字服务器,然后启动客户端,用客户端发送消息,服务器可以很好地接收它,但要处理消息一段时间,等到返回客户端消息后,将出现python:

BrokenPipeError: [Errno 32] Broken pipe

这是Android套接字客户端代码:

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

}

可能是Android客户端在收到消息之前关闭了套接字,但如何解决它,时间非常紧迫。谢天谢地!

0 个答案:

没有答案