Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > [Help] Server UI crashes

Reply
Thread Tools

[Help] Server UI crashes

 
 
Randolph Carter
Guest
Posts: n/a
 
      07-21-2003
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

 
Reply With Quote
 
 
 
 
Andy Flowers
Guest
Posts: n/a
 
      07-21-2003
It sits in a loop, that's why it blocks.

while(actived)
{
....
}

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


"Randolph Carter" <rcarter[remove_this]@email[remove_this].it> wrote in
message news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
 
 
 
Andy Flowers
Guest
Posts: n/a
 
      07-21-2003
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;
}
}

"Randolph Carter" <rcarter[remove_this]@email[remove_this].it> wrote in
message news:(E-Mail Removed)...
> On Mon, 21 Jul 2003 18:43:08 +0100, "Andy Flowers"
> <(E-Mail Removed)> wrote:
>
> >It sits in a loop, that's why it blocks.
> >
> >while(actived)
> >{
> >...
> >}
> >
> >Is actived ever set to false ? Also why is the loop there ?
> >

> activated is set to true, the loop is there to continue listening for
> clients. I've tried without the loop , but it blocks, when the first
> client connects to it, the button isn't pressed but the server doesn't
> accepts any connection. I think the loop must be there.
>



 
Reply With Quote
 
Jon A. Cruz
Guest
Posts: n/a
 
      07-22-2003
Andy Flowers wrote:
>
> 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.

 
Reply With Quote
 
Randolph Carter
Guest
Posts: n/a
 
      07-22-2003
On Mon, 21 Jul 2003 20:15:03 -0700, "Jon A. Cruz" <(E-Mail Removed)>
wrote:

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


Thank you a lot both of you!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
James Gosling the Creator of EMACS and JAVA - leaves ORACLE - Butthen reports started coming in of odd failures. Systems would crashstrangely. We'd get crashes in applications. All applications. Crashes in thekernel. small Pox C Programming 2 07-22-2010 10:07 PM
sun application server 8 crashes after its start immediately klops Java 0 05-14-2004 12:59 PM
Windows 2003 Server crashes with stop errror. Jack MCSE 4 12-11-2003 08:15 AM
debugger/profiler lotus dll crashes server - how to tackle this as a non expert Olaf Kittelmann C++ 1 09-12-2003 02:09 PM
server crashes when I access a certain control Boban Dragojlovic ASP .Net 1 07-28-2003 04:48 PM



Advertisments