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 6804315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:27:03+00:00 2026-05-26T19:27:03+00:00

EDIT: The code below throws no exception but has no output and hangs. It

  • 0

EDIT: The code below throws no exception but has no output and hangs. It should output “Test message”. In main(), we start a thread that’s given a server socket listening on a random port. The main thread the tries to connect and communicate with the ServerSocket on that same random port, but is apparently failing. Why?

public class IntraProcSockTest {
private static int port;

private class Listener extends Thread {
    public Listener() {
    }

    public void run() {
        try {
            ServerSocket serverSocket = new ServerSocket(0);
            port = serverSocket.getLocalPort();
            Socket socket = serverSocket.accept();

            BufferedReader in;
            String fromClient;

            in = new BufferedReader(new InputStreamReader(
                    socket.getInputStream()));

            while ((fromClient = in.readLine()) != null) {
                System.out.println("From client: " + fromClient);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public IntraProcSockTest() {
    new Listener().start();
}

public static void main(String[] args) {
    new IntraProcSockTest();

    try {
        Thread.sleep(5000);
        Socket socket = new Socket("localhost", port);
        PrintWriter socketOut = new PrintWriter(socket.getOutputStream());

        socketOut.println("Test message");
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
  • 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-26T19:27:03+00:00Added an answer on May 26, 2026 at 7:27 pm

    A process can connect to a socket created by itself, there is no problem. Show us the code that throws an exception and/or more details about the exception.

    First of all, be careful not to specify a local port for the client socket (the one connecting to the other which is listening). Let the OS choose a random port. Remember that any socket is identified by four elements (remote host, local host, remote port, local port), if you bind both the server socket and the client socket on the same local port, let it be 4498, both sockets are defined as follows: (localhost, localhost, 4498, 4498) and this doesn’t work. I suspect this might be your problem.
    To avoid such problems, client sockets are often bound to a random port, chosen by the OS. Show us your code, expecially the part in which the client sockets gets created and connects to the server socket.

    And about IPC, it is not always bad to use sockets as an inter-process or even intra-process communication technique. The performance is worse, obviously, and you might loose some code readability, but your software will be easily portable to a network (distributed) application. It’s up to your plans, it’s not like IPC sockets == bad.

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

Sidebar

Related Questions

In the below code onclick edit how can the value of tag test be
EDIT: See my working code in the answers below. In brief: I have a
Edit: The code here still has some bugs in it, and it could do
The example below may not be problematic as is, but it should be enough
This piece of code below is causing my app to crash EDIT @interface termsAndConditions
Edit: This code is fine. I found a logic bug somewhere that doesn't exist
EDIT - The code looks strange here, so I suggest viewing the files directly
I am supposed edit some code for an assignment, and he gave us the
I am using the following code --- EDIT @annakata : Adding the complete code
I have a piece of code: EDIT: The _penParams are initialized as the added

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.