7 Replies Latest reply on May 21, 2007 2:37 PM by 843790

    how to... join DatagramPacket and Serializable

    843790
      Hi!

      Here's what I want to achieve:
      I want to send a packet much more bigger than 256bytes. My serializable object has about 4000bytes. And I want send him to Client. How to do that? Everything is working while an object has not more than 256bytes. If it's more, there's exception like:

      java.io.EOFException
           at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2279)
           at java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(ObjectInputStream.java:3017)
      ...

      Is there any better method to send packets of any size?
        • 1. Re: how to... join DatagramPacket and Serializable
          EJP
          If your network path includes a router you may have trouble getting a 4k datagram through it. I would use TCP for this.

          The EOFException can only mean that you didn't put all the serialized data into the DatagramPacket. Coding error. Did you flush & close the ObjectOutputStream?
          • 2. Re: how to... join DatagramPacket and Serializable
            843790
            1. How to use TCP then?

            2. Yes, I closed ObjectOutputStream. I guess the Exception occured cause just first 256bytes has been send via one packet (the whole object has about 4000bytes so...)

            Message was edited by:
            blelump
            • 3. Re: how to... join DatagramPacket and Serializable
              EJP
              1. java.net.Socket, java.net.ServerSocket, new ObjectOutputStream(socket.getOutputStream()), etc. Much easier. Why would you try to use a streaming protocol over a datagram protocol?

              2. You must have incorrectly constructed the DatagramPacket to be sent, i.e. with a size of 256 instead of the size of the ByteArrayOutputStream.
              • 4. Re: how to... join DatagramPacket and Serializable
                843790
                /////////////////// Server
                ServerSocket serverSocket;
                          try {
                               serverSocket = new ServerSocket(4444);     
                               ObjectOutputStream out;
                                    
                                    Socket clientSocket = serverSocket.accept();
                                    ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
                                  Object temp=in.readObject();
                                  javax.swing.JButton button = (javax.swing.JButton)temp;                 
                                  
                                    clientSocket = serverSocket.accept();
                                    out = new ObjectOutputStream(clientSocket.getOutputStream());
                                    JLabel label = new JLabel("lab");
                                    out.flush(); 
                                    out.writeObject(label); 
                                    out.flush();
                                    
                                    in.close();
                                    out.close();
                                    clientSocket.close();                         
                          }
                          catch (IOException e1) {
                               e1.printStackTrace();               
                          }
                          catch(ClassNotFoundException e) {
                               e.printStackTrace();
                          }
                
                ///////////////// Client
                Socket socket = new Socket("localhost", 4444);
                          JButton butt = new JButton("button");
                          
                          ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
                          
                          out.flush();
                out.writeObject(butt);
                out.flush();

                ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                Object temp=in.readObject();

                in.close();
                out.close();
                socket.close();


                and well Server gets the JButton object (and sends JLabel object aswell) but client doesn't want get JLabel object. there's exception:

                Exception in thread "main" java.net.SocketException: Broken pipe
                at java.net.SocketOutputStream.socketWrite0(Native Method)
                at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
                at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
                at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1838)
                at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1747)
                at java.io.ObjectOutputStream.writeNonProxyDesc(ObjectOutputStream.java:1249)
                at java.io.ObjectOutputStream.writeClassDesc(ObjectOutputStream.java:1203)
                at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1387)
                at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
                at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1538)
                at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:329)

                what I'm doing wrong?
                • 5. Re: how to... join DatagramPacket and Serializable
                  EJP
                  You have two accepts. Is the client connecting twice, once to send and once to receive? Doesn't seem likely - use the same accepted socket for both.
                  • 6. Re: how to... join DatagramPacket and Serializable
                    843790
                    what do you mean? Don't I use same socket for both?
                    There's just ObjectOutputStream for the output and ObjectInputStream for the input of the socket. Shouldn't it be like that? And how should it be then?
                    • 7. Re: how to... join DatagramPacket and Serializable
                      843790
                      I finally fixed it out. Thank you for your time :)