开发者

Wildcard binding in Java does not binds to network aliases

I encountered a quite unusual issue, where the bind function to port only (i.e. wildcard bind), does not actually listens to Linux network aliases (the additional addresses on single network card).

This means that sending requests to main IP address for the network card does work normally, but any requests to additional IP's fails.

Have anyone encountered开发者_JAVA百科 this in past?


If i do this:

sudo ip addr add 192.168.1.186/24 dev eth0

Then run this:

import java.net.*;

public class Bind {
    public static void main(String... args) throws Exception {
        ServerSocket ss = new ServerSocket(8888, 50, InetAddress.getByName("0.0.0.0"));
        Socket s = ss.accept();
        System.out.println("accepted socket " + s);
    }
}

Then, in another shell, this:

telnet 192.168.1.186 8888

The program in the first shell prints:

accepted socket Socket[addr=/192.168.1.186,port=33867,localport=8888]

If i repeat that but use my 'real' address, that works too.

So, it looks to me like binding to 0.0.0.0 binds to all addresses, including extra ones added to a device.

If i change the ServerSocket constructor call to use the single-argument form, everything still works as described, so it looks like that's binding to 0.0.0.0.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜