[Help] Server UI crashes

Discussion in 'Java' started by Randolph Carter, Jul 21, 2003.

  1. 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
     
    Randolph Carter, Jul 21, 2003
    #1
    1. Advertising

  2. Randolph Carter

    Andy Flowers Guest

    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:...
    > 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
    >
     
    Andy Flowers, Jul 21, 2003
    #2
    1. Advertising

  3. Randolph Carter

    Andy Flowers Guest

    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:...
    > On Mon, 21 Jul 2003 18:43:08 +0100, "Andy Flowers"
    > <> 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.
    >
     
    Andy Flowers, Jul 21, 2003
    #3
  4. Randolph Carter

    Jon A. Cruz Guest

    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.
     
    Jon A. Cruz, Jul 22, 2003
    #4
  5. On Mon, 21 Jul 2003 20:15:03 -0700, "Jon A. Cruz" <>
    wrote:

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


    Thank you a lot both of you!
     
    Randolph Carter, Jul 22, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Boban Dragojlovic

    server crashes when I access a certain control

    Boban Dragojlovic, Jul 28, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    385
    William F. Robertson, Jr.
    Jul 28, 2003
  2. klops
    Replies:
    0
    Views:
    372
    klops
    May 14, 2004
  3. Olaf Kittelmann
    Replies:
    1
    Views:
    328
    Phlip
    Sep 12, 2003
  4. Mufasa
    Replies:
    0
    Views:
    837
    Mufasa
    Jun 10, 2007
  5. Dave

    Server crashes - Help please

    Dave, Oct 8, 2003, in forum: ASP General
    Replies:
    3
    Views:
    129
    Ray at
    Oct 8, 2003
Loading...

Share This Page