gpt4 book ai didi

java - 对象输入流/对象输出流 |接收和发送大量对象的客户端(Java)

转载 作者:行者123 更新时间:2023-12-01 13:53:49 25 4
gpt4 key购买 nike

我的多线程服务器/客户端项目遇到问题。

服务器端很好,但问题出在客户端。

每次需要时,我都可以发送和接收对象,我需要将 ObjectInputStreamObjectOutputStream 声明为我的类的属性,然后在构造函数中实例化它们

但问题是代码在 ObjectInputStream 实例化上被阻塞。

这是我的代码:

客户:

 public class agency_auth_gui extends javax.swing.JFrame {

Socket s_service;
ObjectOutputStream out;
ObjectInputStream in;

public agency_auth_gui() {
try {
initComponents();
System.out.println("#Connexion en cours avec le Serveur Bancaire.");
s_service = new Socket("127.0.0.1", 6789);

out = new ObjectOutputStream(new BufferedOutputStream(s_service.getOutputStream()));
in= new ObjectInputStream(new BufferedInputStream(s_service.getInputStream()));
//Authentification du Client GAB
out.writeObject((String) "type:agence");
out.flush();

} catch (UnknownHostException ex) {
Logger.getLogger(agency_auth_gui.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(agency_auth_gui.class.getName()).log(Level.SEVERE, null, ex);
}
}
...
Some automaticaly generated swing code
...
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try {
// Envoi d'une arraylist exec contenant l'authentification

ArrayList exec = new ArrayList();
exec.add("auth_agent");
exec.add(jTextField1.getText());
exec.add(jTextField2.getText());
out.writeObject((ArrayList) exec);
out.flush();

// Reception de la reponse du serveur pour la fonction authentification


Agent agent = (Agent) in.readObject();
if(agent.getId()==0)
{
System.out.println("null");
}else
{
System.out.println(agent.getId());
}

} catch (IOException ex) {
Logger.getLogger(agency_auth_gui.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(agency_auth_gui.class.getName()).log(Level.SEVERE, null, ex);
}
}


public static void main(String args[]) {
try {

for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}

} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(agency_auth_gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(agency_auth_gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(agency_auth_gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(agency_auth_gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}

//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new agency_auth_gui().setVisible(true);
}
});
}
// Variables declaration - do not modify
..

最佳答案

根据JavaDoc for ObjectInputStream :

This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.

编辑

看来您没有从另一端的 InputStream 上接收到任何内容。

关于java - 对象输入流/对象输出流 |接收和发送大量对象的客户端(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19757460/

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