Android从python服务器中的套接字接收消息

2024-04-25 16:34:29 发布

您现在位置:Python中文网/ 问答频道 /正文

在使用此代码发送消息之后,我尝试从python服务器获取消息

我在Android上编写了代码,因此每次单击按钮时,该代码都会被激活并从新客户端连接,Python会将消息发送回所有登录过的参与者,并发送到新客户端

如何从服务器接收消息

我的代码:

class Send extends AsyncTask<String, Void, Void> {
    public Socket socket; // Create socket
    public PrintWriter printWriter; // Create print writer

    protected Void doInBackground(String... strings) {
        String command = strings[0]; // Set the command

        try {
            socket = new Socket("10.0.0.2", 13131); // Set socket connection
            printWriter = new PrintWriter(socket.getOutputStream()); // Set the print writer with socket properties

            printWriter.write(command); // Send the command to server
            printWriter.flush(); // Clear the line
            

            socket.close();
            
        }
        catch (UnknownHostException e) {e.printStackTrace();} // Error exception
        catch (IOException e) {e.printStackTrace();} // Error exception

        return null;
    }
}

我试过了

        InputStream input = socket.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        String line = reader.readLine();

        System.out.println(line);

我得到一个错误异常:

I/System.out: [socket] e:java.lang.ClassNotFoundException: com.mediatek.cta.CtaUtils

我如何接收这些信息?

我的python服务器:

    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Set the socket
    server.bind((socket.gethostname(), 13131)) # Set socket properties

    server.listen()
    (client, (ipNum, portNum)) = server.accept() # Accept to new client

    print("Phone connected")

    while True:
        server.listen()
        (client, (ipNum, portNum)) = server.accept() # Accept to new clients (Accept to new command from the phone)

        Clients.append(client)

        message = str(client.recv(32).decode()) # Set the command as string

        if(message != ""): # Checks if a command has been sent
            print("Client: " + message) # Print the command

            Command(message.lower()) # Process the command

            for Client in Clients:
                Client.send(str(BackMessage).encode())
                Clients.remove(Client)

            print("Server: " + BackMessage) # Print the BackMessage


        else:
            for Client in Clients:
                Client.send(str(BackMessage).encode())
                Clients.remove(Client)

            time.sleep(0.05) # Sleep for 0.05 seconds

Tags: theto代码client消息newstringserver
1条回答
网友
1楼 · 发布于 2024-04-25 16:34:29

CTA=China Type Approval,这是联发科为测试目的在Android中添加的东西

您的错误发生在DriverManager.getConnection()中,它可能使用Android的libcore中的okhttpapache httpSocket类来执行请求

联发科修补了这些库,以添加对HTTP请求的控制。它尝试动态加载在/system/framework/mediatek-cta.jar中定义的一些方法,但可能在android设备的文件系统中不存在或无法访问

我看到了5种解决方案:

  1. 通过OTA或adb重新安装添加合适的联发科cta.jar(如果您使用的是自定义/根ROM)
  2. 使用具有相同应用程序源代码的另一个设备(非基于Mediatek的设备不会有此问题)
  3. 通过官方OTA更新升级操作系统,希望设备制造商解决了这个问题
  4. 通过以下修改自行重建和自定义操作系统
  • 确保将medatek cta添加到产品包产品启动罐
  • 删除libcore、okhttp和apachehttp中的钩子
  1. 联系操作系统维护人员的支持人员

如果它不能以1修复,那么第二个解决方案似乎很简单。因为它的操作系统和硬件相关的问题

参考:https://stackoverflow.com/a/54985015/9416473

相关问题 更多 >