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

  • Home
  • SEARCH
  • 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 602243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:48:05+00:00 2026-05-13T16:48:05+00:00

I have the following code in which I spawn a thread listen which is

  • 0

I have the following code in which I spawn a thread listen which is supposed to constantly listen to any incoming TCP messages, after this thread is run I want the main thread to be used for sending messages but as soon as I initiate listen.run() it seems that main thread does not run any further. I want it to continue to run the while loop but it never reaches it.

package tcpclient;

import java.io.*;
import java.net.*;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class client {
//instance vars
static Socket cSocket =null;
static PrintWriter out = null;
static BufferedReader in = null;

//server info
static String serverName = null;
static int serverPort = 0;
static String userName=null;

//listening vars
static Thread listen;
static  String incoming=null;

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {
    try {
        System.out.println("\n\n\nTCP Chat Client\n\nEnter server name:");
        Scanner scan = new Scanner(System.in);

        //get server info from user
        serverName = scan.nextLine();

        System.out.println("\nEnter port number:");
        serverPort = Integer.parseInt(scan.nextLine());


        System.out.println("\nEnter your username:");
        userName = scan.nextLine();

        //make connection to server
        cSocket = new Socket(serverName, serverPort);
        out = new PrintWriter(cSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));

        //send username to server
        out.println(userName);

        //start listening
        listen = new Thread(){
            @Override
                public void run(){
                try {
                    incoming = in.readLine();
                    while (!(incoming.equals(null))) {
                        System.out.print(incoming);


incoming = in.readLine();

                    }
                } catch (IOException ex) {
                    Logger.getLogger(client.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        };
        listen.run();
        String rcvrname="wefwef";
        String message=null;
        //start messaging
        while(!(rcvrname.equals("exit"))){
            System.out.println("Enter reciever name");
            out.println(scan.nextLine());
            System.out.println("Enter message");
            out.println(scan.nextLine());

        }
        out.close();
        in.close();
        cSocket.close();

    }

    catch (UnknownHostException ex) {
        System.err.println("\ncan't find that host\n");

    }

    catch (IOException ex) {
        Logger.getLogger(client.class.getName()).log(Level.SEVERE, null, ex);
    }

   finally{
        in.close();
        out.close();
        cSocket.close();
   }


}

}

  • 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-13T16:48:05+00:00Added an answer on May 13, 2026 at 4:48 pm

    You want:

    listen.start();
    

    not

    listen.run();
    

    Also the preferred way of doing this since Java 5 is to use an ExecutorService:

    Runnable listen = new Runnable() {
      public void run() { ... }
    }
    ExecutorService exec = Executors.newSingleThreadExecutor();
    exec.submit(listen);
    

    and when you want to stop it:

    exec.shutdown();
    exec.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
    

    or simply:

    exec.shutdownNow();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use opengl in C#. I have following code which fails with
I have following Code Block Which I tried to optimize in the Optimized section
I have the following code which works just fine when the method is POST,
I have the following code which works fine. However, I only want to return
Okay so I have the following Code which appends a string to another in
I have the following JQuery code which does similar functionality like Stackoverflow where the
I have the following piece of code which replaces template markers such as %POST_TITLE%
I have the following JavaScript code: Link In which the function makewindows does not
I have the following code, which will not work. The javascript gives no errors
I have the following code, which splits up a Vector into a string vector

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.