简体   繁体   English

如何使用Sockets.io + Node.js服务器获取客户端IPv4地址(而不是IPv6)

[英]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 . 我想获得使用Socket.io版本1.3.5连接我的Node.js服务器的客户端的IPv4 IP地址。

I tried most of the ways suggested in SO but most of them are or deprecated or they return an IPv6 address. 我尝试了SO中建议的大部分方法,但大多数都是或已弃用,或者它们返回IPv6地址。

The only thing that returned some value was 唯一能带来一些价值的是

socket.request.connection.remoteAddress

but it was in a IPv6 format. 但它采用的是IPv6格式。

Any way getting an IPv4 address? 有没有获得IPv4地址?

If client is connecting using IPv6 you could not get IPv4 address. 如果客户端使用IPv6进行连接,则无法获取IPv4地址。 If you want to have IPv4 addresses you need to turn off IPv6 support on your server. 如果您想拥有IPv4地址,则需要关闭服务器上的IPv6支持。

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; 比我用它来获取IP地址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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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