[Help] Server UI crashes

R

Randolph Carter

Hi everyone, I need help.
I'm doing a client/server application.
The server class extends JFrame and it implements the graphical UI. It
has also a ServerSocket object and when it receives connection
requests from a client, it starts a thread.
The problem is: this class has a button that activates the server
doing this:

ServerSocket s;

private void activate()
{
try{
int port = 1234;
s=new ServerSocket(port);
while(actived)
{
Socket sock = s.accept();
ServerThread sThread=new ServerThread(sock);
sThread.start();
}
}
catch(Exception err) { }
}

but when I push this button and the server starts listening, the UI is
blocked, the button "activate" is still pushed, the server works and
he accepts client connections, but you can't do anything with the
server!
Thank you for your help, and sorry for my English!
Bye
 
A

Andy Flowers

It sits in a loop, that's why it blocks.

while(actived)
{
....
}

Is actived ever set to false ? Also why is the loop there ?
 
A

Andy Flowers

Given you've only provided a snippet can you confirm my understanding

You have 2 applications, the server and the client.
The server needs to listen for connections from clients
The server starts listening when you press an activate button
The activate button on the server app calls the activate method
The activate method has a loop and thus it NEVER returns thus blocking the
server UI

The loop needs to be there, but you need to start a listening thread and
have the loop in there.

i.e.

(excuse syntax errors as I'm typing this as I think of it)

class SocketListener implements Runnable
{
private boolean activated = false;

public void setActivated( boolean activated )
{
this.activated = activated;
}

public void run()
{
try
{
int port = 1234;
ServerSocket s=new ServerSocket(port);
while(activated)
{
Socket sock = s.accept();
ServerThread sThread=new ServerThread(sock);
sThread.start();
}
}
catch(Exception err) { }
}
}

button handler class

SocketListener s = null;

private void activate()
{
if( s == null )
{
// create it
s = new SocketListener();
s.setActivated(true);
new Thread(s).start();
}
else
{
// kill it
s.setActivated(false);
s = null;
}
}
 
J

Jon A. Cruz

Andy said:
public void run()
{
try
{
int port = 1234;
ServerSocket s=new ServerSocket(port);
while(activated)
{
Socket sock = s.accept();
ServerThread sThread=new ServerThread(sock);
sThread.start();
}
}
catch(Exception err) { }

Two problems with that last line:

1) Never have an empty catch clause

2) Don't deal with the base Exception class. Try to be specific.



But the rest of the code (what Andy changed) is what's needed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top