简体   繁体   中英

Android receive messages from socket in python server

I'm trying to get messages from python server after I send messages with this code

I made the code on Android so that every time I click the button, this code will be activated and connected from a new client and the Python will send the message back to all participants who have ever logged in to send to the new client as well

How can I also receive messages from the server?

My code:

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

I tried with

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

        System.out.println(line);

And I got an error exception:

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

How can I receive the messages?

My python server:

    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

CTA = China Type Approval , this is something that Mediatek adds in Android for testing purpose.

Your error is happening in DriverManager.getConnection() that probably uses okhttp , apache-http or Socket class in Android's libcore to do its requests.

Mediatek patched these libraries for adding a control of HTTP requests. It tries to load dynamically some methods defined in /system/framework/mediatek-cta.jar but it is probably absent or not accessible on your android device's file system.

I see 5 solutions:

  1. Add an appropriate mediatek-cta.jar via OTA or via adb remount if you are on a custom/rooted ROM
  2. Use another device with the same application's source code (a non- Mediatek based device would not have that issue).
  3. Upgrading your OS via an official OTA update and hope device manufacturer fixed the issue.
  4. Rebuild and customize the OS by yourself with the following modifications
  • Make sure medatek-cta is added to PRODUCT_PACKAGES and PRODUCT_BOOT_JARS
  • Remove the hooks in libcore, okhttp and apache-http.
  1. Contact your OS maintainer's support .

if its not able able fix by 1, Out of rest Second one seems to be Easy Solution. Since its OS and hardware related issue.

Reference: https://stackoverflow.com/a/54985015/9416473

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM