Java 在HttpRequestHandler上获取客户端的本地IP

Java 在HttpRequestHandler上获取客户端的本地IP,java,android,apache,Java,Android,Apache,我正在编写一个需要与本地其他手机通信的应用程序。(在同一个wifi网络中)在下面的教程之后,我将在端口8202上运行一个非常简单的apache Web服务器,但是我想知道发出请求的客户端的本地IP Web服务器类: public class WebServer { public static boolean RUNNING = false; public static int serverPort = 8202; private Context context = null; private

我正在编写一个需要与本地其他手机通信的应用程序。(在同一个wifi网络中)在下面的教程之后,我将在端口8202上运行一个非常简单的apache Web服务器,但是我想知道发出请求的客户端的本地IP

Web服务器类:

public class WebServer {

public static boolean RUNNING = false;
public static int serverPort = 8202;
private Context context = null;

private BasicHttpProcessor httpproc = null;
private BasicHttpContext httpContext = null;
private HttpService httpService = null;
private HttpRequestHandlerRegistry registry = null;

public WebServer(Context context) {
    this.setContext(context);

    httpproc = new BasicHttpProcessor();
    httpContext = new BasicHttpContext();

    httpproc.addInterceptor(new ResponseDate());
    httpproc.addInterceptor(new ResponseServer());
    httpproc.addInterceptor(new ResponseContent());
    httpproc.addInterceptor(new ResponseConnControl());

    httpService = new HttpService(httpproc, new DefaultConnectionReuseStrategy(), new DefaultHttpResponseFactory());

    registry = new HttpRequestHandlerRegistry();
    registry.register("*", new CommandHandler(context));

    httpService.setHandlerResolver(registry);
}

private ServerSocket serverSocket;

public void runServer() {
    try {
        serverSocket = new ServerSocket(serverPort);

        serverSocket.setReuseAddress(true);

        while (RUNNING) {
            try {
                final Socket socket = serverSocket.accept();

                DefaultHttpServerConnection serverConnection = new DefaultHttpServerConnection();

                serverConnection.bind(socket, new BasicHttpParams());

                httpService.handleRequest(serverConnection, httpContext);

                serverConnection.shutdown();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (HttpException e) {
                e.printStackTrace();
            }
        }

        serverSocket.close();
    } catch (SocketException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    RUNNING = false;
}

public synchronized void startServer() {
    RUNNING = true;
    Log.d("SEVRER", "running");
    runServer();
}

public synchronized void stopServer() {
    RUNNING = false;
    if (serverSocket != null) {
        try {
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    Log.d("SEVRER", "stopping");
}

public void setContext(Context context) {
    this.context = context;
}

public Context getContext() {
    return context;
}
CommandHandler类:

public class CommandHandler implements HttpRequestHandler {
private Context context = null;

public CommandHandler(Context context) {
    this.context = context;
}

@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext httpContext) throws HttpException, IOException {

    HttpEntity entity = new EntityTemplate(new ContentProducer() {
        public void writeTo(final OutputStream outstream) throws IOException {
            OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
            writer.write("output");
            writer.flush();
        }
    });
    response.setHeader("Content-Type", "text/html");
    response.setEntity(entity);
}

public Context getContext() {
    return context;
}
我是否缺少一些东西,或者应该使用其他东西来运行服务器?

您可以使用

socket.getInetAddress().getHostAddress();

在runServer()方法中。

您只能看到客户端请求来自的IP。如果另一台设备决定进入手机网络而不是本地wifi网络,那么您将获得手机提供商的代理/NAT网关的IP,而不是手机的IP。如果有某种本地代理,你会得到该代理的IP。这应该不是问题,应用程序不会连接,但这是用户错误。这会给我自己的IP还是客户端的IP?(很抱歉这么问,但听起来像是第一个)这会给你客户的IP。我已经更正了我的答案。