streamcorruptedexception无效类型代码53

uyhoqukh  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(276)

我希望有人能帮我解决这个问题。我找不到解决类似问题的方法。
我有服务器-客户端通信。服务器为客户端提供对象流,反之亦然。一切正常。只有当我使用对象输出流到服务器,然后想从服务器读取对象时,我得到这个异常。它类似于“如果曾经使用过objectoutputstream,就不能使用objectinputstream”,因为反之亦然。
p、 它不是因为arraylist,当只有poruka时也是一样。

Klient
private void streamOpening(){
        try {
            InetAddress ips = InetAddress.getLoopbackAddress();
            soketK = new Socket(ips,9000);
            brK = new BufferedReader(new InputStreamReader(soketK.getInputStream()));
            pwK = new PrintWriter(new BufferedWriter(new  OutputStreamWriter(soketK.getOutputStream())),true);
            oosK = new ObjectOutputStream(soketK.getOutputStream());
            oisK = new ObjectInputStream(soketK.getInputStream());

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Gresnka pri konekciji");
            System.exit(1);
        }
        System.out.println("Veza sa serverom uspostavljena !");
    }
@Override
            public void actionPerformed(ActionEvent actionEvent) {
                String kor = tfKorime.getText();
                String prim = tfPrimalac.getText();
                String sadr = taSadrzaj.getText();
                taSadrzaj.setText("");
                tfKorime.setText("");
                tfPrimalac.setText("");
                if(rbPosalji.isSelected()){
                    if(kor.length() != 0 &&  prim.length() != 0 && sadr.length() != 0 ){
                        pwK.println("slanje##");
                        try {
                            oosK.writeObject(new Poruka(kor,prim,sadr,new Date()));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        lblInfo.setText("Uspesno poslata poruka!");
                    }
                    else
                        lblInfo.setText("Morate popuniti sva polja!");
                }
                else {
                    if(kor.length() != 0) {
                            pwK.println("mojePoruke##" + kor);
                            try {

>         poruke = (ArrayList<Poruka>) oisK.readObject();

                                for (Poruka po : poruke) {
                                    taSadrzaj.append(po.toString());
                                }
                            } catch (IOException | ClassNotFoundException e) {
                                e.printStackTrace();
                            }
                            lblInfo.setText("Inbox korisnika " + kor);
                        }
                    else
                        lblInfo.setText("Unesite korisnicko ime !");
                }
            }
        });
Server
public ServerNit(Socket soketK, Sanduce inbox) {
        this.soketK = soketK;
        this.inbox = inbox;
        try {
            brK = new BufferedReader(new InputStreamReader(soketK.getInputStream()));
            pwK = new PrintWriter(new BufferedWriter(new OutputStreamWriter(soketK.getOutputStream())),true);
            oosK = new ObjectOutputStream(soketK.getOutputStream());
            oisK = new ObjectInputStream(soketK.getInputStream());
        } catch (IOException e) {
            System.out.println("Problem pri inicijalizaciji strimova !");
            System.exit(1);
        }
            System.out.println("Konekcija uspostavljena !");

    }

    @Override
    public void run() {
        String zahtev = "";
        Poruka primPoruka;
        while(!zahtev.equals("kraj")){
            try {
                System.out.println("Server nit " + Thread.currentThread().getId() + " => ceka zahtev");
                 zahtev = brK.readLine();
                System.out.println("Zahtev primljen... ");
                 String[] params = zahtev.split("##");
                 switch (params[0]){
                     case "slanje":
                         pwK.println("Server spreman za prijem poruke.");
                         primPoruka = (Poruka) oisK.readObject();
                         inbox.dodajPoruku(primPoruka);
                         break;
                     case "mojePoruke":
                            oosK.writeObject(inbox.porukeZa(params[1]));
                         break;
                     case "kraj":
                         zahtev = "kraj";
                         break;
                     default:
                         System.out.println("Server ne moze da odgovori na zahtev !");
                 }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

    }
Exception
java.io.StreamCorruptedException: invalid type code: 53
    at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1710)
    at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:493)
    at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:451)
    at klijent.Klient$1.actionPerformed(Klient.java:118)
. . .

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题