Question

so i have a program thats connect to a main server then I'm asking from that main server to open new server(lets call it small server) and then i connect to him so far so good :)

Then I'm sending to the small server string,int and an object using this line of code:

ObjectOutputStream  toServer = new ObjectOutputStream(socket.getOutputStream());

Again so far so good :)

The problem starts when i want to read back from the small server into my client using this line of code:

ObjectInputStream fromServer = new ObjectInputStream(socket.getInputStream());

my problem is that my client doesn't get the data from the small server(bool and int)..

I'm getting this error:

java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.readBoolean(ObjectInputStream.java:2737)
    at java.io.ObjectInputStream.readBoolean(ObjectInputStream.java:884)
    at AddEmployee.AddEmployee(AddEmployee.java:168)
    at AddEmployee.access$5(AddEmployee.java:136)
    at AddEmployee$1.run(AddEmployee.java:110)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:715)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:676)
    at java.awt.EventQueue$2.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:685)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

yea so pretty ha ha :)

here is my client code:

*Global.toserver and Global.fromserver are my connectors to the main server!!!

try
        {
            int port;
            Global.toServer.writeInt(btnAddEmp.getTag());
            Global.toServer.flush();
            try{
             port=Global.fromServer.readInt();
             try {
                    Thread.sleep(100);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
             Socket socket= new Socket("localhost",port );
             ObjectOutputStream toServer = new ObjectOutputStream(socket.getOutputStream());
            toServer.flush();
            ObjectInputStream fromServer = new ObjectInputStream(socket.getInputStream());
            toServer.writeInt(2);
            toServer.flush();
            toServer.writeUTF(PasswordGenereator());
            toServer.flush();
            toServer.writeInt(Global.IDcompany);
            toServer.flush();
            toServer.writeObject(newEmp);
            toServer.flush();
            System.out.println(fromServer.available());//idk if its part of the problem but its give me a 0
                if(fromServer.readBoolean())
                {
                    //toServer.flush();
                    int num=fromServer.readInt();
                    System.out.println(num);
                }

            socket.close();
        } 
        catch (UnknownHostException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
            }catch(IOException e3)
            {
                e3.printStackTrace();
            }

and now the small server code:

if(whatToDo==2)
             {
                 try {
                    String pass=inputFromClient.readUTF();
                    int idcomp=inputFromClient.readInt();
                    Employee newEmp=(Employee)inputFromClient.readObject();
                    System.out.println(String.format("INSERT INTO `Employees`(`empNumber`, `Fname`, `Lname`, `BirthDate`, `Address`, `Email`, `Password`, `IDCompany`) VALUES ('%d','%s','%s','%s','%s','%s','%s','%d')",newEmp.getEmpNumber(),newEmp.getFname(),newEmp.getLname(),newEmp.getBirthDate(),newEmp.getAddress(),newEmp.getEmail().getEmailAddress(),pass,idcomp));
                      statement=con.prepareStatement(String.format("INSERT INTO `Employees`(`empNumber`, `Fname`, `Lname`, `BirthDate`, `Address`, `Email`, `Password`, `IDCompany`) VALUES ('%d','%s','%s','%s','%s','%s','%s','%d')",newEmp.getEmpNumber(),newEmp.getFname(),newEmp.getLname(),newEmp.getBirthDate(),newEmp.getAddress(),newEmp.getEmail().getEmailAddress(),pass,idcomp));
                      int result1=statement.executeUpdate();
                      System.out.println(result1);
                      if(result1==1)
                      {
                          System.out.println("poooo");
                            outputToClient.writeBoolean(true);
                          statement=con.prepareStatement(String.format("SELECT IDemp FROM Employees WHERE empNumber=%d",newEmp.getEmpNumber()));
                            result=statement.executeQuery();
                            if(result.next())
                            {
                                System.out.println(result.getInt(1));
                                int send=result.getInt(1);
                                System.out.println(send);
                                outputToClient.writeInt(send);
                            }
                      }
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

what should I do?? thank to helpers :)

ok so i manage to solved it after 4 hours by this small line after every sending in the small server side:

outputToClient.flush();
Was it helpful?

Solution

This means there's nothing to read. This is what the error says.

You should make sure you have data written out before trying to read.

You have many conditions before actually writing out something. Check to what
these conditions evaluate, maybe you don't even get to the point of writing.

"ObjectInputStream not working" is a pretty strong statement,
actually your program using ObjectInputStream is not working.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top