Linux- Socket server listen on localhost, not on the machine IP

C

cyril

Hi,

I have a little problem with my socket server running on Linux (RH pro
3). Actually, my server socket is listening on the wanted port but at
the localhost IP and not at the "external" IP of my PC. Is there
something to add in my code to specify the wanted IP to listen
(external or not) ?

Or maybe this is a Linux pb ? Maybe someone have ever meet the same
pb.

Thanks for all.
 
G

Gordon Beaton

Actually, my server socket is listening on the wanted port but at
the localhost IP and not at the "external" IP of my PC. Is there
something to add in my code to specify the wanted IP to listen
(external or not) ?

There are various ServerSocket constructors to let you do that.

If you only specify a port number when you create the ServerSocket, it
will listen on all of the interfaces.

If you only want it to listen on the external interface, specify the
address of that interface in addition to the port number.

Also, have a look at java.io.NetworkInterface, which provides a
mechanism for enumerating all of the machines interfaces and getting
their addresses.

/gordon
 
I

iksrazal

Hi,

I have a little problem with my socket server running on Linux (RH pro
3). Actually, my server socket is listening on the wanted port but at
the localhost IP and not at the "external" IP of my PC. Is there
something to add in my code to specify the wanted IP to listen
(external or not) ?

Or maybe this is a Linux pb ? Maybe someone have ever meet the same
pb.

Thanks for all.

Your linux box has two NIC's, right? So does mine. Do an ifconfig .
You should see two ip`s . There's a lot of ways to do java sockets,
but you could have something like:

new InetSocketAddress(hostName, port)

Change hostname from localhost to your external ip. Should be as
simple as that. if in doubt, do a print on Socket.getInetAddress() or
SocketChannel.getInetAddress() .

Note however, typically you would want your socket to listen on your
internal ip, and use NAT/iptables to port forward/masquerade as a
firewall to your external ip. That`s a bit OT here.

HTH

Outsource to an American programmer living in brazil!
http://www.braziloutsource.com/
iksrazal
 
M

Missaka Wijekoon

Are you binding to the correct ip/interface?

try running the following command:

netstat -an | grep LISTEN | grep tcp

You should see something like:

tcp 0 0 0.0.0.0:32770 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:32771 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 :::22 :::*

If the linux box is listening on ,say, port 25 on all ips, you should
see something like:

tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN

If it is listening on a specific interface only, you should see the ip
address:
i.e. 192.168.77.2:25
or 127.0.0.1:25 (for localhost)

If it is listening on a specific ip address or 0.0.0.0 and it is still
not responding then you probably have a firewall. Most standard RedHat
distros ship with iptables or ipchains.

-Misk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top