1

当我streamCorruptedException尝试从服务器获取对象时,我在客户端得到了一个。

起初,当客户端连接到服务器时它运行良好,但是当我尝试连接第二个客户端或第二次运行时,出现错误。我也尝试在服务器端重置流,但没有奏效。 编辑:

以下是我的服务器代码:

       Socket clientSocket=null;
            BufferedReader rdr=null;
            ObjectOutputStream objwriter=null;
            String name="";
            audioReceiver = new TAudioReceiver(ser);
            videoReceiver = new TVideoReceiver(ser);
            fileReceiver = new TFileReceiver(ser);
            //audioReceiver.start();
            //videoReceiver.start();
            //fileReceiver.start();
            InputStream cliIn=null;
            OutputStream cliOut = null;
            ClientDetails client=null;


            while(true)
            {
                objwriter=null;
                clientSocket = ser.MainSocket().accept();
                cliIn = clientSocket.getInputStream();
                cliOut = clientSocket.getOutputStream();
                client = new ClientDetails(clientSocket);
                rdr= new BufferedReader( new InputStreamReader(cliIn));
                name = rdr.readLine();
                System.out.println("Client connected '"+ name +"'");

                boolean a = ser.clientsMainSockets.AddClient(name, client);
                if(a){
                    ser.clientsNames = ser.clientsMainSockets.ClientNames();

                    wr = new BufferedWriter(new OutputStreamWriter(cliOut));
                    wr.write("OKM");
                    wr.write('\n');
                    wr.flush();
                    objwriter = new ObjectOutputStream(cliOut);
                    //Thread.sleep(1000);
                    objwriter.writeObject(ser.clientsNames);
                    objwriter.flush();
                    clientSocket =null;
                    cliIn = null;
                    cliOut = null;

                    client = ser.clientsMainSockets.Value(name);

                    TMessageHandler msghdl = new TMessageHandler(ser, client.InputStream() , client.OutputStream(), name);
                    clientHandler.add(msghdl);
                    msghdl.start();
                    System.out.println("Starting process for client");
                    objwriter.reset();
                    Thread.sleep(5000);
                }
                else{

                        wr = new BufferedWriter(new OutputStreamWriter(cliOut));
                        wr.write("ERROR");
                        wr.write('\n');
                        wr.flush();
                }


            }
    }
    catch(Exception ex)
    {
    }

以下是客户端代码:

 mainSock = new Socket(serverIp,serMainPort);
            serOut = mainSock.getOutputStream();
            serIn = mainSock.getInputStream();
            buffWr= new BufferedWriter(new OutputStreamWriter(serOut));
            //serOut = mainSock.getOutputStream();
            //serIn = mainSock.getInputStream();
            buffWr.write(myName);
            buffWr.write('\n');
            buffWr.flush();
            buffRdr = new BufferedReader(new InputStreamReader(serIn));
            String per="";
            per = buffRdr.readLine();
            System.out.println(per);
            if(per.equals("OKM"))
            {

                objin = new ObjectInputStream(serIn);
                ClientsName = (String[])objin.readObject();
                System.out.println("Client no.'"+ ClientsName.length );
                msgHandler = new TMainHandler(this, serOut, serIn);
                msgHandler.start();
            }
            else {
                throw new Exception("User Already exisits. Choose Another name.");
            }
        }
        catch(Exception ex)
        {
            throw new Exception(ex.getMessage());
        }
4

1 回答 1

1

你不应该混合文本和二进制流,因为这通常会导致混淆,除非你真的知道你在做什么。

我建议您在流的生命周期中包装一次,mainSock.getInputStream()并且使用它。即不要多次调用它。mainSocket.getOutputStream()

于 2013-01-23T18:29:36.200 回答