gpt4 book ai didi

java - 使用 writeUnshared 和 readUnshared 与套接字通信...读取我之前发送的对象,而不是我现在发送的对象

转载 作者:行者123 更新时间:2023-12-01 11:00:54 24 4
gpt4 key购买 nike

我有一个服务器、一个 RMI 服务器和一个客户端...

我正在使用 writeUnshared 和 readUnshared 与套接字通信,但是当我读取时,我读取的是之前发送的对象(从客户端到服务器),而不是我现在发送的对象...

我知道我正在从客户端发送我想要发送的对象...但是在服务器端我正在读取我之前发送的对象...

     try {
while (true) {

postCard = null;
postCard = (ClientRequest) reciver.readUnshared();

alterRequest = postCard.getRequestID() + ("_" + myUserID);
postCard.setRequestID(alterRequest);
System.out.println(postCard.getRequestID());

System.out.println("[Server] Li a mensagem do cliente na boa.");
//mudar depois para um switch
if (postCard.getRequest()[0].equals("log")) {

postCard.setStage(1);
myMail = remoteConection.verificaLogIn(postCard);

if (myMail.getResponse()[0].equals("userrec")) {
myUserID = (int) myMail.getResponse()[1];
}

myMail.setStage(4);

} else if (postCard.getRequest()[0].equals("new")) {
System.out.println("Fui chamado!");
if(postCard.getResponse()!=null){
System.out.println("Não és null por que caralho!&");
}

postCard.setStage(1);

myMail = remoteConection.novoUtilizador(postCard);

if (myMail.getResponse()[0].equals("infosave")) {
System.out.println("myUserID:" + (int) myMail.getResponse()[1]);
myUserID = (int) myMail.getResponse()[1];
}
else if (myMail.getResponse()[0].equals("erro")){
System.out.println("ERRO!\n"); //Temos que tratar o erro
}
else if(myMail.getResponse()[0].equals("user_already_exists")){
System.out.println("User: "+ (String)myMail.getResponse()[1]+" already exists!");
}

myMail.setStage(4);

} else if (postCard.getRequest()[0].equals("new_project")) {

postCard.setStage(1);

myMail = remoteConection.novoProjecto(postCard);

if (myMail.getResponse()[0].equals("infosave")) {
System.out.println("myProjectID:" + (int) myMail.getResponse()[1]);
myProjectID = (int) myMail.getResponse()[1];
}

myMail.setStage(4);

} else if (postCard.getRequest()[0].equals("seesal")) {

System.out.println("Esteve aqui, como era suposto\n");

postCard.getRequest()[1] = myUserID;
postCard.setStage(1);

myMail = remoteConection.getUserSaldo(postCard);

myMail.setStage(4);
}

sender.writeUnshared(myMail);
}
} catch (Exception e) {
System.out.print("[Server]");
e.printStackTrace();
}
}

我做错了什么?

最佳答案

read/writeUnshared() 仅避免共享正在写入的实际对象。所有依赖(可访问、成员)对象保持共享。

在每次写入之前尝试reset()

关于java - 使用 writeUnshared 和 readUnshared 与套接字通信...读取我之前发送的对象,而不是我现在发送的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33333424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com