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?