Java Socket 查找真实客户端 IP 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6692401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:55:58  来源:igfitidea点击:

Java Socket to find real client IP Address

java

提问by user844415

I am trying to get Real Client IP address using a Java applet. I want to eventually use it in a PHP script, to help with security and authentication. None of the PHP methods will work as the various HTTP headers are not available and can be spoofed easily.

我正在尝试使用 Java 小程序获取真实客户端 IP 地址。我想最终在 PHP 脚本中使用它,以帮助安全和身份验证。没有任何 PHP 方法将起作用,因为各种 HTTP 标头不可用并且很容易被欺骗。

So I am adopting the method suggested in Get the correct local IP address from java appletand http://www.jguru.com/faq/view.jsp?EID=15832

所以我采用了从java小程序获取正确的本地IP地址http://www.jguru.com/faq/view.jsp?EID=15832中建议的方法

However, I cannot compile my simple Applet. I am new to Java, so am a little confused.

但是,我无法编译我的简单 Applet。我是 Java 新手,所以有点困惑。

Code is:

代码是:

import java.net.*;
import java.io.*;

public class SimpleSocketClient
{
    public SimpleSocketClient()
    {
        try
        {
            Socket socket = new Socket("89.185.150.131", 80);
        }
        catch(Exception exc)
        {
            System.out.println("Error in initialising the network - " + exc.toString());
        }

        InetAddress addr = socket.getLocalAddress();
        String hostAddr = addr.getHostAddress();
        System.out.println("Addr: " + hostAddr);

    }

}

When compiling, I get the following error:

编译时,我收到以下错误:

C:\mba>javac SimpleSocketClient.java
SimpleSocketClient.java:18: cannot find symbol
symbol  : variable socket
location: class SimpleSocketClient
        InetAddress addr = socket.getLocalAddress();
                           ^
1 error

C:\mba>

Thanks

谢谢

回答by Rob Harrop

Your socketvariable is declared inside a tryblock and is therefore not accessible outside that block. You can make a slight change by either pushing all code inside the try:

您的socket变量是在try块内声明的,因此无法在该块外访问。您可以通过将所有代码推送到以下内容中来进行轻微更改try

public SimpleSocketClient()
{
    try
    {
        Socket socket = new Socket("89.185.150.131", 80);

        InetAddress addr = socket.getLocalAddress();
        String hostAddr = addr.getHostAddress();
        System.out.println("Addr: " + hostAddr);
    }
    catch(Exception exc)
    {
        System.out.println("Error in initialising the network - " + exc.toString());
    }
}

Or by declaring socketoutside the try:

或者通过在socket之外声明try

public SimpleSocketClient()
{
    Socket socket = null;
    try
    {
        socket = new Socket("89.185.150.131", 80);
    }
    catch(Exception exc)
    {
        System.out.println("Error in initialising the network - " + exc.toString());
    }
    if(socket != null) {
        InetAddress addr = socket.getLocalAddress();
        String hostAddr = addr.getHostAddress();
        System.out.println("Addr: " + hostAddr);
    }
}