Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3444190
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:53:03+00:00 2026-05-18T08:53:03+00:00

All I want to do is start a thread to listen for communication on

  • 0

All I want to do is start a thread to listen for communication on a certain port.

I start this by having an ‘ok’ button on a jDialog. When ‘ok’ has been clicked, the jDialog should hide itself (HostClientDialog.setVisible(false);), which works when the thread start line isn’t in there.

try {

            HostClientDialog.setVisible(false);

            // start a thread that listens for incoming messages
            new gameCycle().start();

} catch (Exception e) { }

The new gameCycle().start(); line is calling the following code:

public class gameCycle extends Thread {

    //public gameCycle(){
      //  super();
    //}

    @Override
    public void run() {

        try {

            ServerSocket connection = new ServerSocket(4242);

            // Wait for connection
            Socket s = connection.accept();

            // Socket input
            BufferedReader in = new BufferedReader(new
                    InputStreamReader(s.getInputStream()));

            // for receiving moves
            while (true) {

                String message = "";

                message = in.readLine();

                if (message != null && !message.equals("")) {
                    // do something with message
                }

                sleep(100);

            } // end while

        } catch (Exception e) { }

    } // end run
} // end class

I understood the above code to be looping until a message is received, and then doing something with the message. But when you execute this code, the jDialog box is made again (instantly) and re-prompts the user to click ok. It won’t let the user get past the jDialog box, it will just continually re-prompt them.

I’m fairly novice with threads (I haven’t done much more than printing using multiple threads), so I feel like I might be me implementing it incorrectly. But I’ve looked around for examples, and they seem to be more or less what I have.

EDIT (11/29/2010 at 1:30 AM EST)

It seems that I don’t quite have the idea of TCP communication down as well as I thought. My goal with the thread and subsequent while loop was as follows:

  • In the background the program would
    be waiting for any messages sent its
    way
  • If it it received a message it would update something on the GUI, and then go back to waiting for more messages

All the while allowing the user to be continually using the GUI.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:53:04+00:00Added an answer on May 18, 2026 at 8:53 am

    I’d say the code you supplied, looks fine to me.

    I would implement the possibility to cancel the whole thread instead of looping with while(true).
    Use a boolean instead and make the boolean accessible for other threads. If you need help on that, come back at me.

    I think the problem lies with the code we do NOT see here.
    I’m partially interested in the code fragments surrounding the .start() call on your thread. What happens after that?

    cheers

    EDIT:

     public class Main {
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        final JDialog dia = new JDialog();
        JButton btn = new JButton("Foo");
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                dia.setVisible(false);
                Runnable t = new Runnable() {
                    public void run() {
                        System.out.println("foo");
                    }
                };
                t.run();
            }
        });
        dia.getContentPane().add(btn);
        dia.setSize(500, 400);
        dia.setVisible(true);
    }
    }
    

    This works perfectly for me – sorry but I can’t reproduce the error like you stated in the comments. Consider giving more code please 🙂

    EDIT #2:
    Don’t set any variables from within your thread directly. Instead make use of the observer-listener pattern. When there is a new message incoming, have the thread fire a propertyChangeEvent instead. http://download.oracle.com/javase/1.4.2/docs/api/java/beans/PropertyChangeListener.html

    Maybe you should try and implement something more trivial .. go for a quick and easy chat first. http://www.ashishmyles.com/tutorials/tcpchat/index.html

    When you have that down, think about your project ahead. Maybe reading on multithreading with swing might help? http://java.sun.com/developer/technicalArticles/Threads/swing/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first real attempt at using multithreading, I want to know how
I want to extend a common security check to nearly every view of my
I submit some some Future tasks using a CompletionService wrapped round a 2 thread
Is it possible (in .net) to have a thread wait for a semaphore to
I'm a complete newbie to multi-threading in C++, and decided to start with the
I have a question about some code I'm testing to start understanding posix threads.
I try to program a TCPServer with threads (ThreadingMixIn) in Python. The problem is
Assume, there is a task containing the following actions approximately: Task someTask = new
I currently have something working where I can drag a box horizontally on the
I am looking to read the contents of a file in Java. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.