WSL2 - Connection Refused on the Host for Sockets opened in Java

Adriano Kretschmer 0 Reputation points
2023-02-20T18:30:49.2566667+00:00

I cannot connect to services written in Java running on WSL2, e.g., Spring Boot or Tomcat.

Steps to reproduce:


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Hello world!
 *
 */
public class App {
    private ServerSocket serverSocket;
    private Socket clientSocket;
    private PrintWriter out;
    private BufferedReader in;

    public void start(int port) throws IOException {
        serverSocket = new ServerSocket(port);
        clientSocket = serverSocket.accept();
        out = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String greeting = in.readLine();
        if ("hello server".equals(greeting)) {
            out.println("hello client");
        } else {
            out.println("unrecognised greeting");
        }
    }

    public void stop() throws IOException {
        in.close();
        out.close();
        clientSocket.close();
        serverSocket.close();
    }

    public static void main(String[] args) {
        App server = new App();
        try {
            server.start(6666);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Compile and run on WSL2 (Ubuntu 22.04).

Try to connect from Powershell console

telnet 127.0.0.1 6666

You will get Connection Refused. If you connect from ANY WSL2 system running(Another Ubuntu/Debian/Suse), it works fine.

It also does not connect from the Firefox installed on WSL2 (Newly graphical interface support for Windows 11).

Surprisingly, when I host an angular application (node/typescript) on the same Ubuntu 22.04, the connection goes ok from the Host side.

I don't know if it has something related to AF_UNIX sockets used in some weird way on JVM, because there is no support for it on WSL2.

UFW is inactive.

I tried to add port forward rules using this script -> https://github.com/microsoft/WSL/issues/4150#issuecomment-504209723

Any thoughts?

Windows for business Windows Client for IT Pros User experience Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 44,751 Reputation points
    2023-02-21T17:02:10.55+00:00

    Hi. Thank you for your question and reaching out. I’d be more than happy to help you with your query

    If you are receiving a "Connection Refused" error on the host machine when trying to connect to a socket opened in Java on WSL2, the issue may be related to the firewall configuration of the Windows host. To resolve the issue, you will need to open the necessary ports in the Windows firewall. Additionally, you may need to ensure that the IP address used for the connection is either the WSL2 IP address or the Windows host IP address.

    If the reply was helpful, please don’t forget to upvote or accept as answer, thank you.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.