0

I am building an application that starts a Connection via a Thread but when i initialize the Connection the Application doesn't connect and stops (it continues to work but it does nothing). 1 week ago it did work without any troubles, but when i updated Android Studio it started causing me this problem.

This is the abstract class of the Connection:

public abstract class Connection implements Runnable{

private Socket socket;
private PrintWriter writer;
private BufferedReader reader;
private Semaphore semaphore;
private ArrayList<String> messageQueue;
private Thread connection;
//private String state; // CONNECTED,RUNNING,STOPPED,RESTARTED,CLOSED

Connection(InetAddress address, int port) {
    try {
        socket = new Socket(address,port);
        onConnectionEstablished(socket);
        writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
        reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        semaphore = new Semaphore(1);
        messageQueue = new ArrayList<>();
    } catch (Exception e) {
        close();
    }
}

@Override
public void run() {
    connection = Thread.currentThread();
    while(!connection.isInterrupted()){
        this.read();
    }
}

// Checks if connection is established
boolean isConnectionEstablished(){
    return socket != null && !socket.isClosed();
}

public abstract Socket onConnectionEstablished(Socket socket);

// Restarts the connection
public abstract void onConnectionClose();

// Launched when message is received
public abstract String onMessageReceived(String message);

// Launched when message is posted
public abstract String onMessagePosted(String message);

// Launched when message is sent
public abstract String onMessageSent(String message);

// Post message
public void postMessage(String message){
    messageQueue.add(message);
    sendMessage();
}

// Send Message
private void sendMessage() {
    final String message = messageQueue.get(0);
    onMessagePosted(message);
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                semaphore.acquire();
                System.out.println(Colors.color("[*] Sending: \'"+message+"\' [*]","blue"));
                writer.print(message);
                writer.flush();
                messageQueue.remove(0);
                semaphore.release();
            } catch (Exception e) {
                close();
            }
        }
    }).start();
    onMessageSent(message);
}

// Read messages
private void read(){
    try {
        String message = reader.readLine();
        if(message != null && message.length() > 0)
            onMessageReceived(message);
        else
            throw new Exception();
    } catch (Exception e) {
        close();
    }
}

// Close the connection
private void close(){
    try{
        connection.interrupt();
        this.writer.close();
        this.reader.close();
        this.socket.close();
    }catch(Exception e){
        System.out.println(Colors.color("[X] Error while closing [X]","red"));
    }
    onConnectionClose();
}

// Restart the connection
void restart(){
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                System.out.println(Colors.color("[X] Error while restarting [X]","red"));
            }
            new Thread(new ConnectionInitializer()).start();
        }
    }).start();
}

}

While doing some debugging i found out that it apparently blocks when the socket is instantiated.

DRE
  • 263
  • 8
  • 35
  • `Socket` uses blocking I/O, and that includes the connect phase on construction. – user207421 Apr 07 '19 at 00:37
  • Yes i know, but i don't understand why the socket blocks and takes a lot of time to connect the address and the port are correct. – DRE Apr 08 '19 at 04:51

0 Answers0