Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java readObject()不';不能读取数组_Java_Serialization - Fatal编程技术网

Java readObject()不';不能读取数组

Java readObject()不';不能读取数组,java,serialization,Java,Serialization,我试图创建一个客户机-服务器程序,其中客户机向服务器发送一个包含两个对象的数组。第一个对象只包含一个字符串。第二个对象是用户选择的形状。用户可以选择4种不同的形状 长方形 方格 三角 罗姆布斯 所以总共有5门课。形状、矩形、正方形、三角形和Rombus。每个形状类都继承自一个名为“FormaObj”的超类。客户端将阵列发送到服务器后,服务器必须了解客户端发送的对象类型(矩形、正方形等),并计算该形状的面积和周长 比如说,我想向服务器发送一个矩形 ClientFormeThread2 co; Fo

我试图创建一个客户机-服务器程序,其中客户机向服务器发送一个包含两个对象的数组。第一个对象只包含一个字符串。第二个对象是用户选择的形状。用户可以选择4种不同的形状

  • 长方形
  • 方格
  • 三角
  • 罗姆布斯
  • 所以总共有5门课。形状、矩形、正方形、三角形和Rombus。每个形状类都继承自一个名为“FormaObj”的超类。客户端将阵列发送到服务器后,服务器必须了解客户端发送的对象类型(矩形、正方形等),并计算该形状的面积和周长

    比如说,我想向服务器发送一个矩形

    ClientFormeThread2 co;
    FormaObj f[];
    FormaObj forma;
    RettangoloObjThread r;
    QuadratoObjThread q;
    TriangoloObjThread t;
    RomboObjThread ro;
        
    
        public ClientFormeForm2() {
            initComponents();
            co = new ClientFormeThread2();
            f = new FormaObj[2];
        }
    
        private void btnRettangoloActionPerformed(java.awt.event.ActionEvent evt) {    
        try {
                String nome = "Rettangolo";
    
              //Creating the Obj form
               FormaObj forma = new FormaObj(nome);  
    
            
             int base = Integer.parseInt(JOptionPane.showInputDialog("Inserire il valore della base")); 
            //Asking the user the insert the value of the base and the height of the rectangle
    
             int altezza = Integer.parseInt(JOptionPane.showInputDialog("Inserire il valore dell'altezza"));
               
            //Creating the object rectangle
             RettangoloObjThread r = new RettangoloObjThread(base, altezza, nome);   
              
             
             f[0] = forma; //Putting the first object form inside the array
             f[1] = r;  //Putting the second object rectangle inside the array
             
             co.comunicaFormaRettangolo(f); //Sending the array to the server 
             r = co.getRettangolo(); 
           
           atxVisualizza.setText("Rettangolo" + "\n" + "Base: " + r.getLato1()+ "\n" + "Altezza: " + 
            r.getLato2() + "\n" + "Area: " + r.getArea() + "\n" + "Perimetro: " + r.getPerimetro());
    
           }catch(ClassNotFoundException ex) {
                Logger.getLogger(ClientFormeForm2.class.getName()).log(Level.SEVERE, null, ex);
            }     
      
    
    这就是公司内部的情况//将阵列发送到服务器

    public ClientFormeThread2(){
             try{
                clientSocket = new Socket("localhost", 8123);
                OutputStream o = clientSocket.getOutputStream();
                InputStream i = clientSocket.getInputStream();
                outOggetto = new ObjectOutputStream(o);
                inOggetto = new ObjectInputStream(i);
                System.out.println("Client Attivo");     
             }catch (IOException e) {
                System.out.println(e.getMessage());
         }
                         
         }
      
    public void comunicaFormaRettangolo(FormaObj[] forma) throws ClassNotFoundException {
            try {
                
                f = forma;
                outOggetto.writeObject(f);
                outOggetto.flush();
                r =(RettangoloObjThread)inOggetto.readObject();
                int Area = r.getArea();
                System.out.println(Area);
              
            } catch (IOException ex) {
                
            } 
     
    
    服务器接收到阵列后,会读取其中的内容

    public class ServerFormeThread2 extends Thread{
         Socket clientDaServire;
        int NClient;
        FormaObj f[];
        FormaObj forma;
        RettangoloObjThread r;
        QuadratoObjThread q;
        TriangoloObjThread t;
        RomboObjThread ro;
        ObjectInputStream inOggetto;
        ObjectOutputStream outOggetto;
        
    
        public ServerFormeThread2(Socket clientDaServire, int NClient) {
            this.clientDaServire = clientDaServire;
            this.NClient = NClient;
        }
        public void run(){
        try{
            OutputStream os = clientDaServire.getOutputStream();
         InputStream ois =  clientDaServire.getInputStream();
          outOggetto = new ObjectOutputStream(os);
          inOggetto = new ObjectInputStream(ois);
          
          f = (FormaObj[]) inOggetto.readObject(); //Reading the array
    
          String NomeForma = f[0].getMessaggio(); //here i basically get the name of the shape. 
          while(!NomeForma.equals("Disconnetti")){  
    
              if(NomeForma.equals("Rettangolo")){ //checks whether the value of "NomeForma" equals"Rectangle"
      
                  r = (RettangoloObjThread) f[1]; //it reads the rectangle that has been stored inside the array
                  r.calcolaAreaRettangolo(); //here it calculates the area
    
                  r.calcolaPerimetroRettangolo(); //and here the perimeter
    
                  outOggetto.writeObject(r);  //and here i only send the object rectangle back to the client because that's what i actually need 
    
                  outOggetto.flush();        
             
              }
              f = (FormaObj[]) inOggetto.readObject(); // i continue listening for other arrays containing 
              shapes that the client might send
              NomeForma = f[0].getMessaggio();
          }
          outOggetto.writeObject(f);
          outOggetto.flush();
          outOggetto.close();
      
            
        }catch(Exception e){
           e.printStackTrace();
        }
    
    
    问题

    第一次将对象发送到服务器时,一切正常。因此,客户端接收回对象矩形,并在TextArea上显示区域和周长。但是当我尝试发送包含另一个矩形的第二个数组时,服务器不会读取它。整个程序都冻结了,除了关闭服务器,我什么也做不了。没有出现错误。下面是“RettangoloObjThread”和“FormaObj”类的代码

    rettangolobjthread

    FormaObj

  • 如果您的应用程序被冻结,这意味着问题出现在线程死锁中,或者某个地方正在运行无限循环,或者某个被认为是由调试器决定的非常长的时间。 您的应用程序也可能只是抛出了一个异常,但由于未正确处理而未记录该异常
  • 如果在调试模式下运行应用程序并在所需行上设置断点,则可以自己跟踪问题
  • 要格式化代码,请使用Ctrl+Alt+L 这样会更方便
  • 注意输入流,因为它们只能使用一次

  • 无论如何,在调试模式下运行应用程序将帮助您解决问题并获得一些经验。

    我通过添加
    writeUnshared()
    而不是普通的
    write()

    来解决问题。发送多个对象的客户端代码在哪里?还是你每次都按一个按钮?您肯定在混合各种代码、UI、业务逻辑、发送和(显然)多线程特定代码。这使得单独测试很困难,甚至是不可能的…是的,我每次按下一个按钮。我讨厌ObjectStream有几个原因。首先,它们只适用于Java,如果我想用另一种语言重写服务器或客户机,我不能。其次,调试基于文本的协议要容易得多。第三,我在论坛上看到的大多数客户机/服务器问题都与ObjectStream的使用有关。引发了什么异常?因此我已经尝试过调试它,并发现它在最后一次readObject()时冻结。我用来“监听”另一个数组的那个
    import java.io.Serializable;
    
    public class RettangoloObjThread extends FormaObj implements Serializable {
    
       private int lato1;
       private int lato2;
       private int Area;
       private int Perimetro;
       
       
        public RettangoloObjThread(int lato1, int lato2, String nomeForma) {
            super(nomeForma);
            this.lato1 = lato1;
            this.lato2 = lato2;
            this.Area = 0;
            this.Perimetro = 0;
        }
    
        
    
        public int getLato1() {
            return lato1;
        }
    
        public void setLato1(int lato1) {
            this.lato1 = lato1;
        }
    
        public int getLato2() {
            return lato2;
        }
    
        public void setLato2(int lato2) {
            this.lato2 = lato2;
        }
    
        public int getArea() {
            return Area;
        }
    
        public void setArea(int Area) {
            this.Area = Area;
        }
    
        public int getPerimetro() {
            return Perimetro;
        }
    
        public void setPerimetro(int Perimetro) {
            this.Perimetro = Perimetro;
        }
        public void calcolaAreaRettangolo(){
        Area = lato1 * lato2;
    }
        public void calcolaPerimetroRettangolo(){
        Perimetro = (lato1 + lato2)*2;
    }
        
    }
    
        import java.io.Serializable;
    
    public class FormaObj implements Serializable{
        private String messaggio;
    
        public FormaObj(String messaggio) {
            this.messaggio = messaggio;
        }
    
        public String getMessaggio() {
            return messaggio;
        }
    
        public void setMessaggio(String messaggio) {
            this.messaggio = messaggio;
        }