'Socket connection not workin in flutter release apk

I am new to working with sockets, and I am working on this project where a connection between my android flutter app and a java server is needed, to do this I am trying socket programming.

The server code is fairly simple, I create a new thread for every client connected and I give them a bunch of URLs, later on, this should be replaced by a query result. here is the java code:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CrawlitServer {

    // The port number on which the server will listen for incoming connections.
    public static final int PORT = 6666;

    //main method
    public static void main(String[] args) {
        System.out.println("The server started .. ");

        // Create a new server socket
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(PORT);

        }   catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }

        // Listen for incoming connections and create a new thread for each one
        while (true) {
            try {
                new CrawlitServerThread(serverSocket.accept()).start();
            }
            catch (Exception e) {
                System.out.println("Error: " + e.getMessage());
            }
        }
    }

    public static class CrawlitServerThread extends Thread {
        private final Socket socket;

        public CrawlitServerThread(Socket socket) {
            this.socket = socket;
        }

        public void run() {

            List<String> list = new ArrayList<>();
            //assign a value to list
            list.add("http://www.google.com");
            list.add("http://www.yahoo.com");
            list.add("http://www.bing.com");
            list.add("http://www.facebook.com");
            list.add("http://www.twitter.com");
            list.add("http://www.linkedin.com");
            list.add("http://www.youtube.com");
            list.add("http://www.wikipedia.com");
            list.add("http://www.amazon.com");
            list.add("http://www.ebay.com");
            list.add("http://stackoverflow.com");
            list.add("http://github.com");
            list.add("http://quora.com");
            list.add("http://reddit.com");
            list.add("http://wikipedia.org");
            try {
                // Get the input stream from the socket
                DataInputStream inputStream = new DataInputStream(socket.getInputStream());
                Scanner scanner = new Scanner(inputStream);
                DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
                PrintWriter writer = new PrintWriter(outputStream, true);

                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    System.out.println("Received Message from client: " + line);
                    writer.println(list + "\n");
                }

            }
            catch (Exception e) {
                System.out.println("Error: " + e.getMessage());
            }
        }
    }

}


Now I run this server and connect to it using sockets in Flutter, I give it the IP address I get from the ipconfig command, and here is the dart code:

import 'dart:async';
import 'dart:io';

//Utilities that manage connections with server sockets.

//ServerUtil Class
class ServerUtil {
  static const port = 6666;
  static const host = MY_IP_GOES_HERE;
  static late Socket socket;
  static bool connected = false;
  //a list of urls returned by the server
  static List<String> urls = [];

  //Constructor
  ServerUtil() {
    //Initialize the socket.
    Socket.connect(host, port).then((Socket sock) {
      socket = sock;
      connected = true;
      socket.listen(dataHandler,
          onError: errorHandler, onDone: doneHandler, cancelOnError: false);
      //send a message to the server.
    }).catchError((e) {
      print("Unable to connect: $e");
    });
  }

  //Query method that sends a message to the server. The server will return a list of urls.
  //The urls will be added to the urls list.
  //The urls list will be returned.
  static Future<List<String>> query(String userQuery) async {
    urls.clear();
    //check if socket is connected.
    if (connected) {
      //send the query to the server.
      socket.writeln(userQuery);
      await Future.delayed(const Duration(milliseconds: 200));
      print(urls);
      return urls;
    }
    //if socket is not connected, wait for 5 seconds and try again.
    await Future.delayed(const Duration(milliseconds: 50));
    return query(userQuery);
  }

  //Handles data from the server.
  void dataHandler(data) {
    //String of received data.
    String dataString = String.fromCharCodes(data).trim();
    //remove first and last character from the string.
    dataString = dataString.substring(1, dataString.length - 1);
    //remove all the whitespace characters from the string.
    dataString = dataString.replaceAll(RegExp(r'\s+'), '');
    urls = dataString.split(',');
  }

  //Handles errors from the server.
  void errorHandler(error, StackTrace trace) {
    print(error);
  }

//Handles when the connection is done.
  void doneHandler() {
    socket.destroy();
  }
}

This works perfectly fine while using a debug apk running it on my real Note 9 device. The problem however is that when I build a release apk and try it out, nothing happens. The way I set it up is that I wait for the query method in an async and then I send the result to a new screen and push that screen into the navigator.

But in the release apk nothing happens, the new screen doesn't load.

So this leads me to my first question:

Is there a way to debug a release apk? see what exceptions it throws or print some stuff to console?

I have the server running on my Laptop, and the app runs on my phone which is on the same WIFI network.

My second question is:

Do I need to enable some sort of option with my router or my laptop to allow my phone to connect? it does connect in debug mode without any modifications

I tried some random things, like using 'localhost' instead of my IP, as I would normally connect say with a java client for example, but it didn't work.

My last question is:

Does the release apk or like android OS prevent connections to local hosts, maybe because it thinks it is not secure? but then it still connects in debug mode.

Thank you for your time.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source