尝试从服务器读取数据时,Java客户端,Python服务器,appfreeze冻结

时间:2019-04-26 12:17:50

标签: java python tcp client

我正在尝试从python服务器接收数据,但是当我调用应处理接收数据的函数时,应用程序将冻结。我不明白问题是什么,我无法连接到python服务器,但是它的输入流不起作用。当我尝试创建python客户端时,它可以正常工作。

我得到的唯一错误是  4153-4206 / com.example.droneapp E / ZrHungImpl:sendAppFreezeEvent失败!

import android.content.Context;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.InetSocketAddress;
import java.net.Socket;

public class Network extends Thread implements Serializable{

private static Socket mSocket;
private String raspIP = "192.168.12.1";
public boolean connected = false;
static BufferedReader in;
private int port = 3400;
private Context mContext;

public Network(Context context){
    this.mContext = context;
}

public void run() {
    mSocket = new Socket();
    try {
        mSocket.connect(new InetSocketAddress(raspIP, port));
        in = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
        if (mSocket.isConnected()){
            connected = true;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void receieveData(){
     try {
         in.readLine();
     }catch (IOException e){}
  }



host = '192.168.12.1'
port = 3400

# Creating server socket to send information over wifi

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
print("Successfully binded",host, "with port",port)
print("Waiting for client to connect.")

message = "HELLO"
connected = False

print("Barometer data: ", Barometer())
print("GPS data:", GPS())

while True:
  # Allowing client to reconnect
  if(not connected):
      try:
         s.bind((host, port))
       s.listen(1)
       conn, addr = s.accept()
       print("Client connected")
       connected = True
    except socket.error as msg:
       print("Error: " + str(msg))
       pass
  else:
    try:
       # Get barometer data
       barometer = str(Barometer())
       # Get GPS data
       gps = str(GPS())

       conn.sendall(message.encode('utf-8'))

       #conn.sendall(barometer.encode('utf-8'))
       #conn.sendall(gps.encode('utf-8'))
    except:
       print("Client not connected")
       s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
       connected = False
       conn.close()
       pass

1 个答案:

答案 0 :(得分:0)

您有几个问题。您正在尝试在客户端断开连接时发送数据。首先,您需要在客户端连接后将数据发送到客户端,然后关闭连接。

在Java方面,您需要通过关闭输入流和套接字来释放它们。

这是您的代码的固定版本,只需配置ip地址:

import socket

host = '127.0.0.1'
port = 3400

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
print("Successfully binded",host, "with port",port)
print("Waiting for client to connect.")

message = "HELLO"
connected = False

try:
    s.bind((host, port))
    s.listen(10)
    while True:
        conn, addr = s.accept()
        print("Client connected")
        conn.sendall(message.encode('utf-8'))
        conn.close()
except socket.error as msg:
    print("Error: " + str(msg))
    pass

在Java方面:

package com.examples;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Socket;

public class Main {

    private static Socket mSocket;
    private static String raspIP = "127.0.0.1";
    public static boolean connected = false;
    static BufferedReader in;
    private static int port = 3400;

    public static void main(String[] args) {
        mSocket = new Socket();
        try {
            mSocket.connect(new InetSocketAddress(raspIP, port));
            in = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
            if (mSocket.isConnected()){
                connected = true;
                receieveData();

                in.close();
                mSocket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void receieveData(){
        try {
            System.out.println(in.readLine());
        }catch (IOException e){
            System.out.println(e.getStackTrace());
        }
    }
}