安卓 和python 服务端通信,安卓收不到服务端返回

用安卓和python的服务端通过socket进行通信, server端可以接收信息,但无法返回到安卓。

安卓

try{
                        //port number 8888
                        Socket s1=new Socket(ipAdress,8883);
                        OutputStream os=s1.getOutputStream();
                        DataOutputStream dos=new DataOutputStream(os);

                        dos.writeUTF(etxtUserName.getText().toString() + " "
                                + etxtPassword.getText().toString());// send usr_name and pwd to server
                        //wait
                        new Handler().postDelayed(new Runnable(){
                            public void run() {
                                //execute the task
                            }
                        }, 1000);
                        dos.flush();
                        s1.shutdownOutput();

                        //
                        InputStream is=s1.getInputStream();
                        DataInputStream dis=new DataInputStream(is);
                        String getStr=dis.readUTF();//YES or NO
                        //String getStr = "YES";

                        if(getStr.equals("YES")){
                            intent.setClass(LogInActivity.this,
                                    personalActivity.class);
                            LogInActivity.this.startActivity(intent);

python server

import socket               # 导入 socket 模块
from time import ctime
import time
port = 8883            # 设置端口
host = socket.gethostname() # 获取本地主机名
BUFSIZE = 1024
tcp_socket = socket.socket()         # 创建 socket 对象
tcp_socket.bind((host, port))        # 绑定端口
tcp_socket.listen(5)                 # 等待客户端连接
while True:
    tcpcli_socket, addr = tcp_socket.accept()     # 建立客户端连接。
    print ('连接地址:', addr)
    #c.send('we are top 1 group')
    data = tcpcli_socket.recv(BUFSIZE)
    print (data)
    tcpcli_socket.send("YES".encode('utf-8'))
    print ('done')
    tcpcli_socket.close()                # 关闭连接

报错信息

W/System.err: java.io.EOFException
        at java.io.DataInputStream.readFully(DataInputStream.java:200)
        at java.io.DataInputStream.readUTF(DataInputStream.java:606)
        at java.io.DataInputStream.readUTF(DataInputStream.java:561)
        at com.example.tianmingyang.carpool.LogInActivity$MyButtonOnClickListener.onClick(LogInActivity.java:78)
        at android.view.View.performClick(View.java:6294)

1个回答

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