Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Program is working when using "Step into" in debugging mode, but not when running. (Sockets)

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Program is working when using "Step into" in debugging mode, but not when running. (Sockets)

    Hello!

    I have three classes: Server, Client and Cap. I'm trying to send Cap from Server to Client and back through sockets. Server implements runnable and in the run-method there is a infinite while-loop that look like this:

    try {
    out.writeObject(new Cap());
    out.flush();
     
    Cap cap = (Cap) in.readObject();
     
    boolean bol = false;
     
    if(cap.getValue()==2) {
     
     bol = true;
     }
     
    out.writeBoolean(bol);
    out.flush();
    } catch (IOException ex) {
                    JOptionPane.showMessageDialog(null, "Something is wrong");
                    break;
    } catch (ClassNotFoundException ex) {JOptionPane.showMessageDialog(null, "Something is wrong");}
     
    }

    In the run-method but before the while-loop I did this:

    out = new ObjectOutputStream(socket.getOutputStream());
    in = new ObjectInputStream(socket.getInputStream());


    In the Client-class I also have a while loop like this:

    try {
     
         Cap cap = (Cap) in.readObject();
     
         textBox.setText(cap.toString());
     
         while(!JButtonHasBeendPressed) {
     
          }
     
          out.writeObject(cap);
          out.flush();
          bol = in.readBoolean();
     
          if(bol) {
     
           //Do something
          }
          else {
           //Do something else
           }
           JButtonHasBeendPressed = false;
     
       } catch (IOException ex) {
           //Do something
       } catch (ClassNotFoundException ex) {
          //Do something
       }
     }

    Before the while-loop I did this:

    out = new ObjectOutputStream(socket.getOutputStream());
    in = new ObjectInputStream(socket.getInputStream());
    When I'm debugging the Client-program it works just exactly as I want - the program gets inside "if(bol)". When I run the program, Server can send the object to Client, but after that something goes wrong. As you can see in the code I have a JButton in the Client-class, that is setting JButtonHasBeendPressed = true, but nothing happens when I'm pressing it. I get no error messages, literally nothing happens.

    Hank

    --- Update ---

    When I'm debugging both the server and the client, a IOException occurs at out.writeBoolean(bol); in the Server-class


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Program is working when using "Step into" in debugging mode, but not when running. (Sockets)

    Please don't post multiple threads on the same topic. This illustrates how confusing it is, because in the other thread, this problem has been solved. Return to the other thread, remove [SOLVED] if appropriate, and keep working it there with any new info.

    This thread is closed.

    --- Update ---

    Reopened in response to OP request.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program is working when using "Step into" in debugging mode, but not when running. (Sockets)

    Can you post a SSCCE for testing?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Program is working when using "Step into" in debugging mode, but not when running. (Sockets)

    Yes, soon. The problem seems to be with the while-loop that is waiting for the JButton to be pressed.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program is working when using "Step into" in debugging mode, but not when running. (Sockets)

    while-loop that is waiting for the JButton to be pressed
    That sounds like a poor design. Having loops wait is not the way to handle events. That is what event listeners are for.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Program is working when using "Step into" in debugging mode, but not when running. (Sockets)

    Well, I have a listener for the JButton. In the actionPerformed-method I have a boolean that becomes true when I'm pressing the button. That will break the below loop.

     while(!JButtonHasBeendPressed) {
     
          }

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program is working when using "Step into" in debugging mode, but not when running. (Sockets)

    Yes, I understand. See post#5

    The code needs to be rewritten to have the listener start the code to be done when the button is pressed.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    iHank (February 18th, 2014)

  9. #8
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Program is working when using "Step into" in debugging mode, but not when running. (Sockets)

    I managed to get it work. Thanks!

Similar Threads

  1. Understanding layers of system logic. Clarity for what "SDK" & "JDK" mean
    By MilkWetGhost in forum Java Theory & Questions
    Replies: 1
    Last Post: October 3rd, 2013, 12:25 PM
  2. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  3. Replies: 26
    Last Post: February 10th, 2013, 12:59 PM
  4. "Program execution skips the "if" statement"!
    By antony1925 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2012, 07:15 AM
  5. The program working fine but the "NO" button doesn't work
    By ahcenpg in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 1st, 2011, 07:32 PM