我的多線程服務器/客戶端項目出現問題。ObjectInputStream/ObjectOutputStream |客戶端接收和發送大量對象(Java)
服務器端很好,但問題出在客戶端。
我可以發送和接收的對象我想我需要聲明一個ObjectInputStream
和ObjectOutputStream
作爲我的類屬性,然後實例化它們的構造
每次但問題是,代碼上阻塞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
..
最後一句話的相關性是什麼? – EJP
你說得對,它沒有意義。我誤解了這個例子,並認爲這些溪流是通過管道輸送的,他們顯然不需要這樣做。 – NilsH
瞭解,但我怎麼做我wana做什麼? 我應該如何讓我的客戶讀取併發送儘可能多的對象? – Coldfire