Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

java.io.EOFException

Posted on 2004-10-26
7
Medium Priority
?
590 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
Comment
Question by:rcm9445
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12420010
Please post stack trace
0
 
LVL 16

Accepted Solution

by:
gnoon earned 1000 total points
ID: 12420016
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
ID: 12420096
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:rcm9445
ID: 12421624
*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
ID: 12423563
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
ID: 12429785
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
ID: 12460090
Thanks :-)
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses

636 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