#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Posts
    149
    Rep Power
    17

    GUI takes forever to display


    Hi all,

    I've begun teaching myself the use of Sockets and Swing in Java, and have written a little 'demo' application to teach myself.

    Essentially, it opens a socket connection to Port 119 (NNTP) to a Newsserver and retrieves a list of groups available on it. What I've done, however, is split the incoming string from the server into an array of strings to split up the group name, the last post, the first post, etc., then I put the name of the group into a Vector object. Once it retrieves all the groups from the server, it creates a JList object using the Vector, adds it to a JScrollPane, which is then added to a JPanel, then a JFrame.

    The problem is that after all the groups are retrieved, it takes FOREVER for the GUI component of the application to load and display the list of groups. I'm running a K6-2 333 512MB SDR, and it takes sometimes up to an hour or more to finally display the GUI, but it *does* seem to display it. I left it overnight last night and I came back this morning to find my GUI staring me in the face (not properly, but still).

    So I'm curious as to why this might be happening. I personally suspect the Vector object, but I'm not 100% sure. So I thought I'd post my code to see if anyone else can help me identify what the bottleneck might be.

    NOTE: This code uses other objects 'EasySocket' and 'EasyStdin', which I wrote to help simplify Socket and Stdin reading. If anyone needs to see the code, let me know... but they seem to work fine and not the cause of the problem.

    Code:
    public class NetSwing01
    {
      public static void main(String args[])
      {
        EasySocket socket01;
        String splitString[];
        String currData; 
        String serverName;
        Vector vect01;
      
        try
        {
          EasyStdin stdin = new EasyStdin();
    
          System.out.print("Enter Newserver : ");
          serverName = stdin.readLine();
    
          System.out.print("Connect to '" + serverName + "'? (Y/n): ");
    
          if (stdin.readLine().equals("Y"))
          {
            socket01 = new EasySocket(serverName,119);
            vect01 = new Vector();
    
            System.out.println("Created Socket...");      
          
            socket01.writeData("MODE READER");
            if (socket01.readData() != null)
            {
    	  System.out.println("Sent MODE READER...");
    
              // (BEGIN) RETRIEVE A LIST OF GROUPS ON THE SERVER
              socket01.writeData("LIST");
      
              System.out.println("Sent LIST...");
    
              while ((currData = socket01.readData()) != null)
              {
                // Split the incoming data.
                splitString = currData.split(" ");
              
                // If the first string cannot be turned into an Integer object, then assume
                // it is text and add to the Vector object.
                try
    	    {
    	      new Integer(splitString[0]);
      
                  System.out.print("X");
    	    } catch (Exception e) {
                  vect01.addElement(splitString[0]);
                  System.out.println("  > " + splitString[0]);
      	    }
              }
              // (END) RETRIEVE A LIST OF GROUPS ON THE SERVER
            
              // (11-19-2003 : Once retrieving is done, it takes FOREVER to display the GUI, and doesn't display properly)
              System.out.println("...DONE");
    	}
          }
          else
          {
            vect01 = new Vector();
            vect01.addElement("Hello");
    	vect01.addElement("World");
          }
    
    
            System.out.println("Creating GUI..."); 
            
            // (BEGIN) CREATE A SWING LIST TO DISPLAY RESULTS
            JList list01 = new JList(vect01);
            list01.setVisibleRowCount(10);
            // (END) CREATE A SWING LIST TO DISPLAY RESULTS
            
            // (BEGIN) CREATE A SWING SCROLLING PANEL FOR LIST
            JScrollPane scrollPane01 = new JScrollPane(list01);
            // (END) CREATE A SWING SCROLLING PANEL FOR LIST
            
            // (BEGIN) CREATE A SWING PANEL FOR THE SCROLLING PANEL
            JPanel panel01 = new JPanel(new FlowLayout());
            panel01.add(scrollPane01);
            // (END) CREATE A SWING PANEL FOR THE SCROLLING PANEL
            
            // (BEGIN) CREATE A SWING FRAME FOR THE PANEL
            JFrame frame01 = new JFrame();
            frame01.setContentPane(panel01);
            frame01.setSize(500,200);
            frame01.setTitle("Newsgroups");
            frame01.setLocation(200,200);
            frame01.pack();
            // (END) CREATE A SWING FRAME FOR THE PANEL
            
            
            // (BEGIN) DISPLAY THE WINDOW
            frame01.setVisible(true);
            // (END) DISPLAY THE WINDOW
    
        } catch (Exception e) {
          System.err.println("Error: " + e);
        }
      }
    }
    Thank You,

    - coredumped.
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    121
    Post those classes so I can compile and run this please.
    ~Nemi

    Before posting did you try:
    [ Javadocs | Google ]
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Posts
    149
    Rep Power
    17

    Full Code


    No prob... here's the whole thing. It's all in one file: NetSwing01.java.

    I just got back from letting it run for a few hours and my JFrame was there, newsgroups listed, everything as it should... but it took that long to get to that point. See if you have better luck.

    Code:
    import java.lang.*;
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    class EasyStdin
    {
      private BufferedReader inp01;
      
      public EasyStdin()
      {
        try
        {
          this.inp01 = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
          System.err.println("ERROR: Failed to created Buffered Reader for EasyStdin Object");
        }
      }
      
      
      public String readLine()
      {
        String tempString;
        
        try
        {
          tempString = this.inp01.readLine();
        } catch (Exception e) {
          tempString = null;
        }
        
        return tempString;
      }
    }
    
    
    class EasySocket
    {
      private Socket socket01;         // Private instance of a Socket object.
      private PrintWriter socketOut;   // Private instance of a PrintWriter object for outputting to the socket.
      private BufferedReader socketIn; // Private instance of a BufferedReader object for reading from the socket.
      
    
    
      public EasySocket(String serverName, int portNumber)
      {
        try
        {
          // Create a new Socket object.
          this.socket01 = new Socket(serverName,portNumber);
          
          // Create the PrintWriter object.
          this.socketOut = new PrintWriter(this.socket01.getOutputStream(), true);
          
          // Create the BufferedReader object.
          this.socketIn = new BufferedReader(new InputStreamReader(this.socket01.getInputStream()));
          
        } catch (Exception e) {
          System.err.println("ERROR: " + e);
        }
      }
      
      
      public String readData()
      {
        String tempStr;
    
        try
        {
          tempStr = this.socketIn.readLine();
        } catch (Exception e) {
          tempStr = null;
        }
    
        return tempStr;
      }
      
      
      public void writeData(String outStr)
      {
        this.socketOut.println(outStr);
      }
      
    }
    
    
    public class NetSwing01
    {
      public static void main(String args[])
      {
        EasySocket socket01;
        String splitString[];
        String currData; 
        String serverName;
        Vector vect01;
        int counter01, counter02;
      
        try
        {
          EasyStdin stdin = new EasyStdin();
    
          System.out.print("Enter Newserver : ");
          serverName = stdin.readLine();
    
          System.out.print("Connect to '" + serverName + "'? (Y/n): ");
    
          if (stdin.readLine().equals("Y"))
          {
            socket01 = new EasySocket(serverName,119);
            vect01 = new Vector();
    
            System.out.println("Created Socket...");      
          
            socket01.writeData("MODE READER");
            if (socket01.readData() != null)
            {
    	  System.out.println("Sent MODE READER...");
    
              // (BEGIN) RETRIEVE A LIST OF GROUPS ON THE SERVER
              socket01.writeData("LIST");
      
              System.out.println("Sent LIST...");
    
              counter01 = 0;
    
              while ((currData = socket01.readData()) != null)
              {
                // Split the incoming data.
                splitString = currData.split(" ");
              
                // If the first string cannot be turned into an Integer object, then assume
                // it is text and add to the Vector object.
                try
    	    {
    	      new Integer(splitString[0]);
      
                  System.out.print("X");
    	    } catch (Exception e) {
      	      counter01++;
                  vect01.addElement(splitString[0]);
                  System.out.println("  " + counter01 + ": " + splitString[0] + " VectorSize: " + vect01.size());
      	    }
              }
              // (END) RETRIEVE A LIST OF GROUPS ON THE SERVER
            
              // (11-19-2003 : Once retrieving is done, it takes FOREVER to display the GUI, and doesn't display properly)
              System.out.println("...DONE");
    	}
          }
          else
          {
            vect01 = new Vector();
            vect01.addElement("Hello");
    	vect01.addElement("World");
          }
    
    
            System.out.println("Creating GUI..."); 
            
            // (BEGIN) CREATE A SWING LIST TO DISPLAY RESULTS
            JList list01 = new JList(vect01);
            list01.setVisibleRowCount(10);
            // (END) CREATE A SWING LIST TO DISPLAY RESULTS
            
            // (BEGIN) CREATE A SWING SCROLLING PANEL FOR LIST
            JScrollPane scrollPane01 = new JScrollPane(list01);
            // (END) CREATE A SWING SCROLLING PANEL FOR LIST
            
            // (BEGIN) CREATE A SWING PANEL FOR THE SCROLLING PANEL
            JPanel panel01 = new JPanel(new FlowLayout());
            panel01.add(scrollPane01);
            // (END) CREATE A SWING PANEL FOR THE SCROLLING PANEL
            
            // (BEGIN) CREATE A SWING FRAME FOR THE PANEL
            JFrame frame01 = new JFrame();
            frame01.setContentPane(panel01);
            frame01.setSize(500,200);
            frame01.setTitle("Newsgroups");
            frame01.setLocation(200,200);
            frame01.pack();
            // (END) CREATE A SWING FRAME FOR THE PANEL
            
            
            // (BEGIN) DISPLAY THE WINDOW
            frame01.setVisible(true);
            // (END) DISPLAY THE WINDOW
    
        } catch (Exception e) {
          System.err.println("Error: " + e);
        }
      }
    }
    - coredumped.
  6. #4
  7. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    121
    EDIT:I found a news server


    Well, I tried your program and it appears to be working correctly, but the news servers I am trying are taking a long time to return the information. I don't think it has anything to do with your program. Some servers I tried were really slow and some returned data faster.

    I do not the protocol for news servers. It may be that you are getting all your listings but the server does not close the connection for some reason. I had one server return a short list quickly, but then sat there for a long time. Maybe you should catch the ioexception thrown by the bufferedreader and see what the message is when you get up in the morning. It may shed some light on the situation.
    Last edited by Nemi; November 19th, 2003 at 10:30 PM.
    ~Nemi

    Before posting did you try:
    [ Javadocs | Google ]
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Posts
    149
    Rep Power
    17

    Newsserver


    The News Server I'm connecting to is one provided by my ISP. No specific name address other than 'shawnews' (Shaw Cable Broadband network). But as for more specifics, I haven't a clue.

    - coredumped.
  10. #6
  11. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    121
    I edited my post while you were posting, so you may not have seen my reply. This post is just so you get an email notification. Check my last post.
    ~Nemi

    Before posting did you try:
    [ Javadocs | Google ]
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Posts
    149
    Rep Power
    17
    Well, I took your advice and displayed the Exception to the screen, but didn't really get anything special... but I did also display the value of 'currData' retrieved in the while loop... let it run and found the following line this morning:

    445 connection dropped

    THEN it displays '...DONE' and THEN displays the GUI. So do I maybe have to explicitly close the connection before proceeding?

    - coredumped.
  14. #8
  15. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    121
    Ok, you need to find out the exact protocol for usenet. I was playing around with some public newsgroups (list at the bottom) and they all return a dot (.) as the last entry when getting a list of newsgroups. This is likely the signal that your list is complete. You then need to issue a "quit" command to sign off the server.

    free newsgroups
    ~Nemi

    Before posting did you try:
    [ Javadocs | Google ]

IMN logo majestic logo threadwatch logo seochat tools logo