ObjectInputStream/ObjectOutputStream |接收和发送大量对象的客户端(Java)
我的多线程服务器/客户端项目有问题 服务器端很好,但问题出在客户端 每当我需要在类上声明ObjectInputStream/ObjectOutputStream |接收和发送大量对象的客户端(Java),java,swing,sockets,objectinputstream,objectoutputstream,Java,Swing,Sockets,Objectinputstream,Objectoutputstream,我的多线程服务器/客户端项目有问题 服务器端很好,但问题出在客户端 每当我需要在类上声明ObjectInputStream和ObjectOutputStream作为属性,然后在构造函数上实例化它们时,我都可以发送和接收对象 但问题是代码阻塞了ObjectInputStream实例化 这是我的密码: 客户: public class agency_auth_gui extends javax.swing.JFrame { Socket s_service; ObjectOutp
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
..
public class agency\u auth\u gui扩展了javax.swing.JFrame{
插座s_服务;
对象输出流输出;
目标输入流;
公共机构{
试一试{
初始化组件();
System.out.println(“#Connexion en cours avec le Serveur Bancaire.”);
s_服务=新插座(“127.0.0.1”,6789);
out=newObjectOutputStream(new BufferedOutputStream(s_service.getOutputStream());
in=new ObjectInputStream(new BufferedInputStream(s_service.getInputStream());
//客户端GAB的身份验证
writeObject((字符串)“类型:代理”);
out.flush();
}捕获(未知后异常除外){
getLogger.getLogger(agency\u auth\u gui.class.getName()).log(Level.SEVERE,null,ex);
}捕获(IOEX异常){
getLogger.getLogger(agency\u auth\u gui.class.getName()).log(Level.SEVERE,null,ex);
}
}
...
一些自动生成的swing代码
...
私有void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
试一试{
//环境保护署署长环境认证
ArrayList exec=新的ArrayList();
执行添加(“授权代理”);
add(jTextField1.getText());
add(jTextField2.getText());
out.writeObject((ArrayList)exec);
out.flush();
//收到确认服务的回复
Agent=.readObject()中的(Agent);
if(agent.getId()==0)
{
System.out.println(“空”);
}否则
{
System.out.println(agent.getId());
}
}捕获(IOEX异常){
getLogger.getLogger(agency\u auth\u gui.class.getName()).log(Level.SEVERE,null,ex);
}捕获(ClassNotFoundException ex){
getLogger.getLogger(agency\u auth\u gui.class.getName()).log(Level.SEVERE,null,ex);
}
}
公共静态void main(字符串参数[]){
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(agency_auth_gui.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(agency_auth_gui.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(agency_auth_gui.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(agency_auth_gui.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
/*创建并显示表单*/
invokeLater(new Runnable()){
公开募捐{
新代理的身份验证gui().setVisible(true);
}
});
}
//变量声明-不修改
..
根据以下内容:
此构造函数将阻塞,直到相应的ObjectOutputStream写入并刷新标头
编辑
因此,您在
输入流上似乎没有从另一端接收到任何东西。显然,当连接被接受时,您的服务器没有构造ObjectOutputStream。不要推迟此步骤:它将在构造ObjectInputStream时阻塞客户端。请参阅Javadoc。上一个se的相关性是什么ntence?你是对的,这没有意义。我误读了这个示例,认为流是通过管道传输的,这显然是不必要的。明白了,但我怎么能做我wana所做的事情?我应该如何让我的客户端读取并发送他想要的尽可能多的对象?当你的服务器接受客户端时,它还必须发送一个对象作为回报,否则它将ust block。不,不需要。但当它接受套接字时,它确实需要构造一个ObjectOutputStream。明白,但我该如何做我想做的事情?我该如何让我的客户端读取并发送他想要的尽可能多的对象?你必须修复服务器。我不知道这为什么如此神秘。