-
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 08:21 AM.
-
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
-
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 ?
-
Show how you have the server running in it's own thread. I only see the threads created to handle each client
-
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!
-
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()
-
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
-
By Dan Dubinsky in forum java.announcements
Replies: 0
Last Post: 08-23-2002, 05:48 PM
-
By Dan Dubinsky in forum java.announcements
Replies: 0
Last Post: 06-27-2002, 09:02 PM
-
Replies: 9
Last Post: 06-28-2001, 11:36 AM
-
By @ragorn in forum ASP.NET
Replies: 1
Last Post: 05-25-2001, 09:06 PM
-
By Sok Y. Kim in forum VB Classic
Replies: 4
Last Post: 10-29-2000, 11:54 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks