简体   繁体   English

无法在Android上获得HTTP服务器的工作

[英]Cannot get http server work on android

I have a http server code (I tried both TJWS or NanoHTTPD), the client from the same application would connect to server running on port 8080 or whatever. 我有一个http服务器代码(我尝试过TJWS或NanoHTTPD),来自同一应用程序的客户端将连接到端口8080或任何端口上运行的服务器。

I am starting server object in a separate AsyncTask so it should be okay. 我正在单独的AsyncTask中启动服务器对象,因此应该可以。

While NanoHTTPD completely failed to start other ways I can see from TJWS logs, it says something like; 虽然NanoHTTPD完全无法从TJWS日志中看到其他启动方式,但它表示类似:

server listening on 0.0.0.0/0.0.0.0 port:0 localport:8080

This means server started successfully, first question is 0.0.0.0 bind address acceptable? 这意味着服务器成功启动,第一个问题是0.0.0.0绑定地址可接受吗? I mean it should be 127.0.0.1 instead? 我的意思是应该改为127.0.0.1? sorry if that is a noob question. 抱歉,如果这是一个菜鸟问题。

When I connect to my emulator using adb shell and run netstat, I can see the following lines 当我使用adb shell连接到仿真器并运行netstat时,可​​以看到以下几行

Proto Recv-Q Send-Q Local Address          Foreign Address        State
 tcp       0      0 127.0.0.1:5037         0.0.0.0:*              LISTEN
 tcp       0      0 0.0.0.0:5555           0.0.0.0:*              LISTEN
 tcp       0      0 10.0.2.15:5555         10.0.2.2:52132         ESTABLISHED
tcp6       0      0 :::8080                :::*                   LISTEN

By googling I learned that 0 :::8080 means server is listening on ipv6 and ipv4 both and that is okay. 通过谷歌搜索,我了解到0 ::: 8080意味着服务器正在ipv6和ipv4上监听,这没关系。

But from my client code when i tried to access it continues to wait for eternity. 但是当我尝试访问它的客户端代码时,它继续等待永恒。

my httpClient Code 我的httpClient代码

   try {
        URL url = new URL("http://0.0.0.0:"+8080+"/media");
        URLConnection conn = url.openConnection();
        InputStream is = conn.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line;
        while((line = br.readLine()) != null){
            Log.d("server", line);
        }
    } catch (MalformedURLException e) {

        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

For the server, the address 0.0.0.0 is like a wildcard and means, it is listening on all IP addresses the device has. 对于服务器,地址0.0.0.0就像一个通配符,意味着它正在侦听设备拥有的所有IP地址。

For the client, you need to use a real IP address like 127.0.0.1 对于客户端,您需要使用真实的IP地址,例如127.0.0.1

After debugging a little I found that problem is not where client opens a connection but issue was at where server was starting it never returned onPostExecute() method, but after wrapping my server start code inside a Runnable 经过一点调试之后,我发现问题不是客户端打开连接的地方,而是服务器启动的地方,它从未返回onPostExecute()方法,而是在将服务器启动代码包装在Runnable中之后

  new Thread(new Runnable() {

        @Override
        public void run() {
            Log.d("server", "server starting on port: " + port);
            srv.serve();
        }
    }).start();  

it Works!! 有用!!

Server is a infinite loop so does it have to be started from inside thread? 服务器是一个无限循环,是否必须从线程内部启动? I thought AsyncTask can handle that? 我以为AsyncTask可以处理?

Its also worth mentioning that client side connection must also be wrapped inside AsyncTask 还值得一提的是,客户端连接也必须包装在AsyncTask中

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

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