GUI problem when I open a ServerSocket


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: GUI problem when I open a ServerSocket

  1. #1
    Join Date
    Sep 2005
    Posts
    16

    GUI problem when I open a ServerSocket

    Hi guys!
    I have an interesting problem here

    I made a server interface for my client-server application, that should offer support for more than one client. So, the code looks like this when I push the start button:

    Code:
    while(true){
                    sock = server.accept();
                    ClientThread t = new ClientThread(sock,i);
                    t.start();
                    i++;
                }
    where sock is declared as Socket and server is declared as ServerSocket.
    So, when a client is connecting to the server, the server runs a new thread for the client [ClientThread]

    My problem appears at this line
    Code:
    sock = server.accept();
    where my GUI is not responding anymore. I said it might be from the while(true) expression and I put the waiting statement in a new thread for the server. And now, the result is the same.. my GUI begins to act unresponsive.

    The funny thing is that the background code works.. the clients are connecting.. and the server is working well.. with the GUI exeption :|

    Maybe someone had the same problem.. and could help me out..
    Thanks!
    Last edited by bedeabza; 11-28-2005 at 09:21 AM.

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    If you are running this while-loop in the event-thread then the gui will stall updating while this code is running (it can't process events such as repaint) when it is executing this loop. I think that your problem should be solved if you move this code into its own thread and then use the GUI components to manipulate this thread (such as killing it, etc).

    Hope this helps.
    ~evlich

  3. #3
    Join Date
    Sep 2005
    Posts
    16
    But that's what I've done.. and that's what I'm saying that it's very weard because I moved the sloop in the ServerThread.java class but even then, at the same line the interface stalls.
    And I was wondering.. how can that be possible ???

    Thanks ! any other oppinion ?

  4. #4
    Join Date
    Oct 2005
    Posts
    107
    Show how you have the server running in it's own thread. I only see the threads created to handle each client

  5. #5
    Join Date
    Sep 2005
    Posts
    16
    this is the class for the ServerThread

    Code:
    class Server extends Thread{
        int Port=6363, i=0;
        ServerSocket server;
        Socket sock;
        
         public Server(){
            super("Nodens");
            try{
                server = new ServerSocket(Port);
                while(true){
                    System.out.println("Waiting for client...");
                    sock = server.accept();
                    ClientThread t = new ClientThread(sock,i);
                    t.start();
                    i++;
                    System.out.println("Numar de clienti: "+ i);
                }
            }catch(IOException e){
                System.err.println("IO Error: \n"+e);
            }finally{
                try{
                    server.close();
                }catch(Exception e){}
            }
        }
        
        public void trace(String it){
        	System.out.println(it);
        }
    }
    and this is how i start it when the button is pushed

    Code:
        private void startNodens(){
            Control.setText("Stop Nodens");
            LED.setBackground(new java.awt.Color(0, 204, 51));
            ComStat.setText("Alive...");
            setStatus("Alive... waiting for clients");
            log("Server alive...");
            Server s = new Server();
            s.start();
        }
    if i comment the line below, the interface will go normaly.. else, it stalls :|
    Code:
    sock = server.accept();

    Thanks!

  6. #6
    Join Date
    Oct 2005
    Posts
    107
    your extending thread but putting the main server acception loop in the constructor??????

    put that in the run() method of ServerThread, so when you call start on ServerThread it will create a new thread to execute the main loop code.

    as it is now, I'm surprised your not stuck in an infinte loop at
    Server s = new Server()

  7. #7
    Join Date
    Sep 2005
    Posts
    16
    Thanks very much, Joe Beam.. you were right
    The fact is that it's my first application that uses threads, and I didn't knew how to use them right. Now the GUI is working well.

    Thanks again !

Similar Threads

  1. JADE Open Framework 1.1 Released with Eclipse Integration
    By Dan Dubinsky in forum java.announcements
    Replies: 0
    Last Post: 08-23-2002, 06:48 PM
  2. The JADE Open Framework, an open source RAD tool for the J2EE!
    By Dan Dubinsky in forum java.announcements
    Replies: 0
    Last Post: 06-27-2002, 10:02 PM
  3. Replies: 9
    Last Post: 06-28-2001, 12:36 PM
  4. Problem to open a new IE Window
    By @ragorn in forum ASP.NET
    Replies: 1
    Last Post: 05-25-2001, 10:06 PM
  5. Ado Connection
    By Sok Y. Kim in forum VB Classic
    Replies: 4
    Last Post: 10-30-2000, 12:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center