Connecting to a local network Raspberry Pi

2019-09-24 09:22发布

问题:

I have a:

Rasberry Pi 2

running

Raspbian Jessie Version:November 2015

I am using Undertow (a Java http server) to serve a website. This is the code that I use to build the server.

Undertow server = Undertow.builder()
                   .addHttpListener(8890, "localhost")
                   .setHandler(Handlers.pathTemplate()
                      .add("/", resource(new PathResourceManager(staticFilePath, 100))
                              .setDirectoryListingEnabled(false))
                   .build();

Problem: I am unable to see the webserver from another machine on the local network despite being able to ping and SSH into the PI.

What I have done (on the Pi2):

wget localhost:8890

returns the index.html correctly

netstat -lptn

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -               
tcp6       0      0 :::22                   :::*                    LISTEN      -               
tcp6       0      0 127.0.0.1:8890          :::*                    LISTEN      1743/java  

Chrome on my development machine 192.168.1.8:8890 gives ERR_CONNECTION_REFUSED

wget 192.168.1.8:8890

Connecting to 192.168.1.8:8890... failed: Connection refused.

nmap 192.168.1.8

Starting Nmap 6.40 ( http://nmap.org ) at 2015-12-05 14:05 CST
Nmap scan report for 192.168.1.8
Host is up (0.039s latency).
Not shown: 999 closed ports
PORT   STATE SERVICE
22/tcp open  ssh

Nmap done: 1 IP address (1 host up) scanned in 1.83 seconds

It is my understanding that there is no firewall so I am baffled as to why I can't see the server from my development machine.

回答1:

See:

tcp6       0      0 127.0.0.1:8890    :::*      LISTEN      1743/java  

Your web server listens only on localhost address (127.0.0.1). This way it couldn't be accessed from anywhere but localhost.

And your nmap scan shows the same: the only remotely accessible port is 22.

To access this service remotely you have to bind web server to any non-local address belonging to this raspberry pi (192.168.1.8) or to "any address" 0.0.0.0, as SSH service is bound.

How to do this is written in the manual of your web server. Probably, you have to start is with a "-d" param, i.e.

standalone.sh -b=0.0.0.0
standalone.sh -Djboss.bind.address=0.0.0.0

or something like this.

In listener setup code this looks like

"localhost" have to be replaced with some public name. This could be "0.0.0.0" or "192.168.1.8". We also can

cat "192.168.1.8 somename" >> /etc/hosts

and then use somename:

Undertow server = Undertow.builder() .addHttpListener(8890, "somename")