0

Sorry for the somewhat sloppy programming but it should be understandable.
Ok i'm supposed to write a function decide(), its purpose is to first broadcast a User object and then listening for any acknowledgements. If any was received it acts like a client and the one who sent the response will act as a server and vive versa if no answer was received.

Function decide() is given below
    public boolean decide() throws ClassNotFoundException{
        try{
            serverSocket=new DatagramSocket(UdpPort);
        }catch(Exception e){
             JOptionPane.showMessageDialog(null,"Init error","Server socket error",JOptionPane.ERROR_MESSAGE);
        }

       // while(true){
             byte buf[]=new byte[20000];

        Enumeration<NetworkInterface> interfaces = null;
        try {
            interfaces = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
        NetworkInterface networkInterface=null;
        InetAddress ip=null;
        
        while (interfaces.hasMoreElements()) {
             networkInterface = interfaces.nextElement();

  

             if(!networkInterface.isVirtual()){
                try {
                    if (networkInterface.isLoopback()) {
                        continue; // Don't want to broadcast to the loopback interface
                    }
                } catch (SocketException ex) {
                    Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
                }
          }

            for (InterfaceAddress interfaceAddress :networkInterface.getInterfaceAddresses()) {
                   broadcast = interfaceAddress.getBroadcast();
                   ip=interfaceAddress.getAddress();
                   if (broadcast == null)
                       continue;
                   else
                       break;
                       // Use the address
             }
            if(broadcast!=null)
                break;
       }

        try {
           ByteArrayOutputStream ib=new ByteArrayOutputStream();
            ObjectOutputStream os = new ObjectOutputStream(ib);
            
            if(!networkInterface.isUp()){
                JOptionPane.showMessageDialog(null,"Network problem","No proper running network interfaces were found",JOptionPane.ERROR_MESSAGE);
                System.exit(1);
            }
            String mac=new String(networkInterface.getHardwareAddress());
            user=new User("Pain",mac,ip,"online",socket);
            Ulist.add(user);
            os.writeObject(user);
            os.flush();
            
            os.close();        
            DatagramPacket receive=null;
            byte[] buffer=new byte[20000];

            DatagramPacket pack=new DatagramPacket(ib.toByteArray(),ib.size(),broadcast,UdpPort);
            serverSocket.send(pack);
            ib.close();
            ByteArrayInputStream baos=new ByteArrayInputStream(buffer);
******//error on the next line
            ObjectInputStream is=new ObjectInputStream(baos);
            for(int j=0;j<2;j++){
                receive=new DatagramPacket(buf,buf.length);
                if(receive!=null){
                    User u=(User)is.readObject();
                    Ulist.add(u);
                    break;
                }
                try {
                    serverSocket.wait(30000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            if(receive.equals(null)){
                ishost=true;
            }else
                ishost=false;
        baos.close();
        is.close();
        }catch (IOException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }

        System.out.println("broadcast= "+broadcast+" InetAddress= "+ip);
        return ishost;

        //}
    }

The errors on the line i pointed to in the code are the following
java.io.StreamCorruptedException: invalid stream header: 00000000
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at smartlanmessenger.MainWindow.decide(MainWindow.java:205)
at smartlanmessenger.MainWindow.<init>(MainWindow.java:43)
at smartlanmessenger.MainWindow$1.run(MainWindow.java:251)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

i really dont know what StreamCorruptedException is indicating
Things i've tried:
1.Take out the objectoutputstream, the error was the same;
2.I took out ObjectInputStream without touching the byteArrayinputStream and the code compiled succesfully.
Any help would be most welcome

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by verruckt24
0

You are creating a ByteArrayInputStream from the byte array 'buffer', which has just been initialized. byte being a primitive type will initialize to it's default value which would be 0 (??). Then you are using this ByteArrayInputStream - baos - to create an ObjectInputStream. The constructor documentation says:

Creates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.
If a security manager is installed, this constructor will check for the "enableSubclassImplementation" SerializablePermission when invoked directly or indirectly by the constructor of a subclass which overrides the ObjectInputStream.readFields or ObjectInputStream.readUnshared methods.

I don't have a clue what the second parts means, but the first part means that the stream should contain a valid serialization header, which as we have seen you aren't obviously setting. Now if these headers are set implicitly then we may be on the wrong track but if they are not then may be you need to do something to the stream before you use it to create the ObjectInputStream.

These are just swags, which hopefully go into the right direction, I may as well be completely wrong.

Edited by verruckt24: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.