1

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();
3
  • It would really help if you'd show a short but complete program which demonstrates the problem (and does nothing else). Also you should absolutely fix your JDBC code to avoid building SQL like that. Use parameterized SQL for various reasons - see bobby-tables.com Commented Mar 10, 2014 at 17:35
  • i can't give short version of my code cuz my program is to big and it takes allot of variables and two servers :) Commented Mar 10, 2014 at 17:39
  • So do you believe every single statement in the code you've posted is necessary in order to reproduce the problem? I very much doubt it. Yes, you need to show both the server and the client - but I bet you can show two complete programs in about the same amount of code that you've used to show incomplete ones. Please read tinyurl.com/so-hints Commented Mar 10, 2014 at 17:42

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

11 Comments

if you look at the code of the server you can see there that I'm sending outputToClient.writeBoolean(true); and outputToClient.writeInt(send);
@DorCohen You have many conditions. Why don't you write a small program writing out without any conditions and see if that eliminates the problem.
all my conditions are working i check that in the old good way of println and the server goes all the way to the end but the client is stack at the readboolen part :(
How do you make sure the client is trying to read only after the server has written the boolean value? I don't see any code trying to make sure this holds true. Actually looking at what you do on the server side (accessing a DB), I am pretty sure the client is faster and tries reading too quickly before the server has sent anything.
its suppose do to so...when something need the be read from the server it stops until it gets it but in the server side is going throw it like is really sending it...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.