EDDYMENS

Published 4 days ago

How To Access A Windows Localhost App From WSL

I ran into a problem while working with IBKR’s API [↗] using the IB Gateway [↗] on Windows. The API runs on localhost:4002, but when I tried to connect from WSL [↗], I couldn't reach the API.

I suspected there wasn't a 1:1 mapping between IPs and Ports between Windows and WSL, turns out I was right.

Finding Your Windows IP Address

Open up Command Prompt (cmd) on Windows and run the command below:

cmd.exe

$ ipconfig

CMD Ipconfig command [→]

Look for the Ethernet adapter or Wi-Fi section and find the IPv4 Address (e.g., 172.22.32.1).

Use the Windows IP in WSL

Instead of localhost, use your Windows IP when connecting to the API from WSL.

CMD Ipconfig command [→]

That’s it! Now, WSL can talk to apps running on Windows localhost.

Alternative solution

You can also look up the IP from within WSL, this is particularly useful if the IP changes often.

In this case use the following Linux command to get the Windows IP Address:

01: WINDOWS_IP=$(ip route | awk '/default/ {print $3}') # Get and store IP as a variable i.e: WINDOWS_IP 02: echo $WINDOWs_IP # 172.22.32.1 03: curl http://$WINDOWS_IP:4002 # using IP Dynamically

I hope this helps someone, happy coding!

Here is another article you might like 😊 How To Identify Users In A WebSocket Connection|NodeJS