1

我正在构建一个通过线程启动连接的应用程序,但是当我初始化连接时,应用程序没有连接并停止(它继续工作但它什么都不做)。1 周前它确实可以正常工作,但是当我更新 Android Studio 时,它开始导致我出现这个问题。

这是 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();
}

}

在进行一些调试时,我发现在实例化套接字时它显然会阻塞。

4

0 回答 0