0

I am using ws with Electron for making a desktop app. I have a WebSocket server which runs in the app, but the problem is that I can't get the IP address of that server. The IP address I'm looking for is that of the device which is listed under ipv4 of the ipconfig in CMD since I'm using Windows 11. The server.address() only returns "::" for the IP address and I need it instead saying "192.168.0.41".

My code goes like this:

wss = new WebSocket.Server({ port: 6000 })
return wss.address() // returns "::" for address

Now on another post someone responded saying that .address() should be called after setting up the server. Well I tried it by making a new Node.js server and I got the same results for the address from the code below.

const net = require("node:net")

const s = net.createServer((socket) => {})

s.listen(() => {
    console.log("opened server on: ", s.address()) // returns "::" for address
})

Now the result I'm looking for is in the following screenshot.

  • 1
    Are you sure it's not `::1`? That's the IPv6 address of `localhost`. – Barmar Jun 18 '22 at 19:00
  • @Barmar Well here is a screenshot I perhaps should have included https://gyazo.com/5b19bec8a0ad2fb40dcb16a588a147c5 – Marko Ranković Jun 18 '22 at 19:07
  • `::` means all ipv6 addresses assigned to your computer, so you server is listening on all of them, but it does not have single one – Iłya Bursov Jun 18 '22 at 19:09
  • After you accept a connection, you can get the local address of the connection to see what address was used. – Barmar Jun 18 '22 at 19:11
  • So the app I'm making is a server and I want to be able to display the IP on the app so that users can know what IP to connect to in the first place. At this point in time, the server is open but there is no connection with any clients. Can I obtain the local IP without yet having any connections? – Marko Ranković Jun 18 '22 at 19:13
  • The question you linked to shows the same result you're asking about. If you don't specify an address when you create the server, it returns `::`. – Barmar Jun 18 '22 at 19:13
  • If your server has multiple NICs, there's no single address to connect to, it can be the addresses of any of them. And if it supports both IPv4 and IPv6, you can connect to either address. – Barmar Jun 18 '22 at 19:15
  • @Barmar Can I not just get the 192.168.0.41 using js? Is there anyway to get that IPv4 address that's listed in the CMD? Or do I have to make one up for the server? – Marko Ranković Jun 18 '22 at 19:16
  • @Barmar is it then possible to at least get one of the IP addresses? Or a list of them? – Marko Ranković Jun 18 '22 at 19:19

0 Answers0