通过objectinputstream接收多个对象,但仅在发送objectoutputstream之后刷新它们

时间:2018-12-31 03:45:43

标签: java sockets tcp server client

所以我的问题是我有一个带有Runnable的客户端,该客户端从套接字在后台读取对象。在服务器中,我多次通过writeUnshared发送对象,例如通知更新等对象,但是当我通过writeUnshared向服务器发送请求时,客户端才收到它们。

\ ClientThread.java \

public class ThreadClientInFromServer implements Runnable {
Socket socket;
ClientData clientData;

public ThreadClientInFromServer(Socket socket, ClientData clientData) {
    this.socket = socket;
    this.clientData = clientData;
}

@Override
public void run() {
    ObjectInputStream in;
    ObjectOutputStream out;

    out = clientData.getOut();
    in = clientData.getIn();
    while (!socket.isClosed()) {
        try {

            Object object = in.readObject();

                clientData.updateData(object);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

SendRequestClient.java仅当我发送此请求时,它才会刷新并发出更新,通知等

public void sendRequest(Request request) {

    try {
        out.writeObject(request);
        out.flush();
        out.reset();

    } catch (IOException e) {
        System.out.println("[ERROR] ON SEND REQUEST!");
        return;
    }
}

例如,在服务器(KEEPALIVETCP.java)上,他没有收到。

public class KeepAliveTCP implements Runnable {
ServerModel serverModel;

public KeepAliveTCP(ServerModel serverModel) {
    this.serverModel = serverModel;
}

@Override
public void run() {
    Request request = new Request(null, Constants.ACK);
    while (!serverModel.getSocket().isClosed()) {
        try {
            for (SocketModel clients : serverModel.getModelClientes()) {
                if (clients.getNome() != null) {
                    clients.getOut().writeUnshared(request);
                    clients.getOut().flush();
                }
            }
            sleep(5000);
            watchWhoFails();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

预先感谢!

编辑:因此,我试图找到一种解决方案,并提出让我的客户阻止菜单线程(该线程用于用户在应用程序上浏览)时使用该线程(ThreadClientInFromServer)的问题。因此,如果你们知道一些解决方案,我真的不知道如何解决这个问题!

谢谢。

0 个答案:

没有答案