Link to home
Start Free TrialLog in
Avatar of rcm9445
rcm9445Flag for United States of America

asked on

java.io.EOFException

This is an extension to my other question...but...I changed everything around, so the other one...doesn't really apply now.

I have a DirectoryServer class that contacts the ChatServer(command line argument is whatever chatroom name it is starting) to get the hostname and a random portnumber and then it stores the info for later use.  A ChatClient then send the DirectoryServer the chatroom it wants to join, the Directory Server then looks up to find out what hostname and portnumber to use for that chatroom and then sends it back to the ChatClient, which then connects.
 
Right now everything works up till its time for the ChatClient to receive information from the DirectoryServer, so it can connect to the ChatServer itself.  I had to close the  ChatClients socket to the DirectoryServer after it sent information because if I didn't it wouldn't send the info to the DirectoryServer in the 1st place...it would just hang there waiting. I then open up the same one?(not sure..technically might not be same)

I have posted just the pertaining code.

connectDirectory = new ServerSocket(4567);
            //Wait for connection
            while (true)
            {
                  try
                  {
                        connectionSocket = connectDirectory.accept();
                        System.out.println("Right before Datastream being made");
                        BufferedReader inFromServer =      new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
                        //Read line      from one of the clients.
                        System.out.println("Waiting to readLine in from one of the clients");
                        String my_info = inFromServer.readLine();
                        System.out.println("Info is " + my_info);
                        StringTokenizer st = new StringTokenizer(my_info);
                        System.out.println("Info is " + my_info);
                        while (st.hasMoreTokens())
                        { //Coming from the ChatServer
                              if ( "+".equals(st.nextToken()) )
                              {
                                    ++count;
                                    chatroom_name = st.nextToken();
                                    url = st.nextToken();
                                    portNumber = st.nextToken();
                                    all_information[count] = chatroom_name + " " + url + " " + portNumber;
                                    System.out.println("The tokens : " + chatroom_name + url + portNumber);
                                    continue_prog = false;
                                    inFromServer.close();
                              } //Coming from ChatClient
                              else
                              {
                                    outToClient = new ObjectOutputStream(connectionSocket.getOutputStream());                  
                                    System.out.println("Made the outToClient");
                                    outToClient.writeObject(information);
                                    System.out.println("sent the info to client");
                                    continue_prog = true;
                                    outToClient.close();
                              }
                        }
                  }
                  catch (IOException ioe)
                  {System.out.println(ioe);}
            }
      }
}
######################################
try
            {
                  try
                  {
                        clientSocket = new Socket("canopus.cis.ksu.edu", 4567);
                        DataOutputStream outToDirectory = new DataOutputStream(clientSocket.getOutputStream());
                        System.out.println("OUttoDirectory made");
                        outToDirectory.writeBytes(chatroomName);
                        System.out.println("chatroom name sent is : " + chatroomName);
                        outToDirectory.close();
                        clientSocket.close();
                        //Reading in info from DirectoryServer
                        System.out.println("Trying to make inFromServer");
                        clientSocket = new  Socket("canopus.cis.ksu.edu", 4567);
                        System.out.println("Failed on getting inputstream");
                        inFromServer =      new ObjectInputStream(clientSocket.getInputStream());
                        try
                        { info = (Object[])inFromServer.readObject();}
                        catch (Exception e) {System.out.println(e);}
                        for (int i=0; i<20; ++i)
                        {
                              Object the_object = info[i];
                              System.out.println("The object array at i = " + info[i]);
                              if (the_object != null)
                              {
                                    String the_string = the_object.toString();
                                    StringTokenizer st = new StringTokenizer(the_string);
                                    while (st.hasMoreTokens())
                                    {
                                          if ( chatroomName.equals(st.nextToken()) )
                                          {  
                                                url = st.nextToken();
                                                portNumber = st.nextToken();
                                                the_port=Integer.parseInt(portNumber);
                                                System.out.println("The tokens : " + url + portNumber);

                                          }
                                    }
                                    clientSocket.close();
                                    inFromServer.close();
                              }
                        }
                  }
                  catch (UnknownHostException e)
                  {System.err.println(e);}
                  }
                  catch (IOException e) {System.err.println(e);}

If someone could help...it would be appreciated.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Please post stack trace
ASKER CERTIFIED SOLUTION
Avatar of gnoon
gnoon
Flag of Thailand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DScotese
DScotese

You probably need to flush() or close() the outToDirectory OutputStream.  The call to clientSocket.close() does this, but you want to keep the socket open and use it some more, don't you?  If you just flush the OutputStream, I think you can keep using it too.  The reason your data isn't sent when writeBytes() to it is that it's tremendously wasteful to send every little piece of data in a new network packet.

Dave.
Avatar of rcm9445

ASKER

*DScotese*
Actually...my client is done with clientSocket once it successfully sends the chatroom name to DirectoryServer and then gets the information back from the DirectoryServer

*Gnoon*
I implemented what you suggested, but now, the DirectoryServer reads in the info from the Server and then doesn't read in anymore information.
The ChatClient makes the printstream, but stops and won't make the inputstream.  (Neither of the programs fail...they just don't do anything further.)  

I was having this probably earlier with the inputstream being made right after the output stream, if I make the inputstream after the outtoDirectory is flushed, then the DirectoryServer reads in all the information correctly and sends the information to the ChatClient, but it doesn't go back to
connectionSocket = connectDirectory.accept();
so its no longer waiting for requests from clients.
The information the ChatClient receives from the DirectoryServer is null, so it ends without getting a connection to the ChatServer.

*CEHJ*
There was no stack trace, it just gave the one error, when the ChatClient was trying to either trying to send or receive(don't remember which now) from the DirectoryServer and then it would continue on with the program...I think it was one of my try catches that caught it.
Avatar of rcm9445

ASKER

Ok, I fixed it now...but..do you have any idea why I have to declare the inFromServer after I've flushed the outToDirectory in the client??

I'm going to give you the points Gnoon, but I want to keep it open a little longer incase I have problems implementing some other functionality...that way I don't have to restate everything again.  I suppose if I have another issue, whoever solves it can get the points from my other open question.

thanks
No ploblem, rcm9445.

>the DirectoryServer reads in all the information correctly and sends the information to the ChatClient, but it doesn't go back to ...

I think there's an error occurs in that loop. You can use e.printStackTrace() to debug. It'll gives you more informations about the error than using System.err.println(e). Furthermore, try to catches the Exception instead of specified exception while debugging, you'll see various types of error.

If you have any more questions, please feel free to ask.

G noon
Thanks :-)