简体   繁体   中英

How to get client IPv4 address (not IPv6) using Sockets.io + Node.js server

I would like to get an IPv4 IP address of clients that connect my Node.js server using Socket.io version 1.3.5 .

I tried most of the ways suggested in SO but most of them are or deprecated or they return an IPv6 address.

The only thing that returned some value was

socket.request.connection.remoteAddress

but it was in a IPv6 format.

Any way getting an IPv4 address?

If client is connecting using IPv6 you could not get IPv4 address. If you want to have IPv4 addresses you need to turn off IPv6 support on your server.

Than I'm using this to get IP address var ip = socket.client.request.headers['x-forwarded-for'] || socket.client.conn.remoteAddress || socket.conn.remoteAddress || socket.request.connection.remoteAddress; var ip = socket.client.request.headers['x-forwarded-for'] || socket.client.conn.remoteAddress || socket.conn.remoteAddress || socket.request.connection.remoteAddress;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM