Java 服务器在多个网络接口上

Java Server on multiple network interfaces

我写了一个 Java server 应用程序来侦听来自客户端的数据,然后写入数据库。 我运行在一台linuxUbuntu12.04有两块网卡的电脑上应用。当我使用启用的单个网络接口连接到网络时,它工作得非常好,但是当我启用两个网络接口时,我只能从连接到 eth0 的客户端接收数据。

我服务器的代码差不多classical:

    String address = "0.0.0.0";
    serverSocket = new ServerSocket();
    serverSocket.bind(new InetSocketAddress(address,port));

    while (true) {
            Socket clientSocket = null;
            try {
                clientSocket = serverSocket.accept();
                System.out.println("\n----Connection accepted----");
                ConnectionHandler handler = new ConnectionHandler(clientSocket, mysql);
                Thread handlerThread = new Thread(handler);
                handlerThread.setPriority(Thread.NORM_PRIORITY);
                handlerThread.start();
           catch(Exception exc){
                //catch statement
           }
     }

问题有点奇怪,因为地址“0.0.0.0”应该创建一个监听所有接口的套接字

有什么可以建议我如何解决这个问题吗?

编辑: 可能问题出在 SO 设置中。

这是我的 /etc/network/interfaces 文件的内容:

auto lo
iface lo inet loopback

这是 sudo lshw -class network 命令

的结果
~$ sudo lshw -class network
[sudo] password for administrator: 
  *-network               
       description: Ethernet interface
       product: I210 Gigabit Network Connection
       vendor: Intel Corporation
       physical id: 0
       bus info: pci@0000:02:00.0
       logical name: eth0
       version: 03
       serial: 90:1b:0e:0b:11:22
       size: 100Mbit/s
       capacity: 1Gbit/s
       width: 32 bits
       clock: 33MHz
       capabilities: pm msi msix pciexpress bus_master cap_list ethernet physical tp 10bt 10bt-fd 100bt 100bt-fd 1000bt-fd autonegotiation
       configuration: autonegotiation=on broadcast=yes driver=igb driverversion=5.0.5-k duplex=full firmware=3.16, 0x800005ae ip=10.0.0.100 latency=0 link=yes multicast=yes port=twisted pair speed=100Mbit/s
       resources: irq:18 memory:f7a00000-f7a7ffff ioport:e000(size=32) memory:f7a80000-f7a83fff
  *-network
       description: Ethernet interface
       product: I210 Gigabit Network Connection
       vendor: Intel Corporation
       physical id: 0
       bus info: pci@0000:03:00.0
       logical name: eth1
       version: 03
       serial: 90:1b:0e:0b:0f:25
       size: 100Mbit/s
       capacity: 1Gbit/s
       width: 32 bits
       clock: 33MHz
       capabilities: pm msi msix pciexpress bus_master cap_list ethernet physical tp 10bt 10bt-fd 100bt 100bt-fd 1000bt-fd autonegotiation
       configuration: autonegotiation=on broadcast=yes driver=igb driverversion=5.0.5-k duplex=full firmware=3.16, 0x800005b0 ip=192.168.150.100 latency=0 link=yes multicast=yes port=twisted pair speed=100Mbit/s
       resources: irq:19 memory:f7900000-f797ffff ioport:d000(size=32) memory:f7980000-f7983fff

我认为您需要指定 null 来表示任何地址:

serverSocket.bind(new InetSocketAddress(null,port));

或替代:

serverSocket.bind(new InetSocketAddress(port));

两者都应映射到任何地址。

最后,我使用类似于此解决方案的方法解决了具有多个网关的多个接口的问题: NetworkManager: per device routing tables

特别是我将网络分成两部分,将一些地址分配给隧道接口,另一个分配给以太网接口。

唯一要注意的是,VPN 使用 vpn-up 而不是 up 作为他的状态。