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.
LVL 1
rcm9445Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Please post stack trace
0
gnoonCommented:
You shan't re-open the socket to receive data from directory server on the client, just send and receive data in next line on a socket. You'll need to call outputstream.flush() to flush the output stream and force any buffered output bytes to be written out after you've called outputstream.write().

Try to use this format on client:

try
{

  // opens socket to directory server
  Socket clientSocket = new Socket("canopus.cis.ksu.edu", 4567);

  // gets output stream
  PrintStream outToDirectory = new PrintStream(clientSocket.getOutputStream());
  // gets input stream
  ObjectInputStream inFromServer = new ObjectInputStream(clientSocket.getInputStream());

  // sends a command
  outToDirectory.println(chatroomName);
  // flushes output stream
  outToDirectory.flush();

  // reads data
  Object[] o = (Object[])inFromServer.readObject();

  // closes streams
  outToDirectory.close();
  inFormServer.close();

  // closes socket
  clientSocket.close();

}
catch(IOException e)
{
  e.printStackTrace();
}

PS. Maybe it's more effective to send a chat server info to client than send a list of server info.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DScoteseCommented:
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.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

rcm9445Author Commented:
*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.
0
rcm9445Author Commented:
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
0
gnoonCommented:
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
0
gnoonCommented:
Thanks :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.