Solved

java.io.EOFException

Posted on 2004-10-26
553 Views
Last Modified: 2012-05-05
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.
0
Question by:rcm9445
    7 Comments
     
    LVL 86

    Expert Comment

    by:CEHJ
    Please post stack trace
    0
     
    LVL 16

    Accepted Solution

    by:
    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
     

    Expert Comment

    by: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.
    0
     
    LVL 1

    Author Comment

    by:rcm9445
    *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
     
    LVL 1

    Author Comment

    by:rcm9445
    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
     
    LVL 16

    Expert Comment

    by:gnoon
    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
     
    LVL 16

    Expert Comment

    by:gnoon
    Thanks :-)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: JavaScript Coding - Massive 12-Part Bundle

    Regardless of your programming skill level, you'll go from basics to advanced concepts in a vast array of JavaScript subjects including Sammy.js, Agility.js, Ember.js, Node.js, jQuery, AJAX, Extjs, AngularJS, Knockout.js, and JSON.

    Suggested Solutions

    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    This video teaches viewers about errors in exception handling.

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now