简体   繁体   English

在 linux(Ubuntu 18.04)上设置用 java 编写的 DCHP 服务器时出现问题

[英]Problems setting up DCHP server written in java on linux (Ubuntu 18.04)

I'm new to linux and kind of new to networking so I might have made some stupid mistake along the way.我是 linux 和网络新手,所以我可能在此过程中犯了一些愚蠢的错误。

This is current "topology" of the "network" I'm trying to setup.是我正在尝试设置的“网络”的当前“拓扑”。

So, I am setting up the DHCP server on linux machine and afaik, the DHCP works like this (from the point of the server).. it receives DHCPDISCOVER which client has broadcasted then it offers a client an address from the assigned "pool" (DHCPOFFER) also by broadcast, then client requests the address (DHCPREQUEST) and finally server acknowledges (DHCPACK or NACK if some err).所以,我在 linux 机器和 afaik 上设置 DHCP 服务器,DHCP 的工作方式是这样的(从服务器的角度来看).. 它接收客户端广播的 DHCPDISCOVER,然后它为客户端提供来自分配的“池”的地址(DHCPOFFER) 也通过广播,然后客户端请求地址 (DHCPREQUEST),最后服务器确认 (DHCPACK 或 NACK,如果出现错误)。 Currently I am stuck at the DHCPOFFER step, because when I connect the linux laptop (server) with my windows laptop (client), it receives a DHCPDISCOVER and throws an exception when trying to broadcast DHCPOFFER.目前我被困在 DHCPOFFER 步骤,因为当我将 linux 笔记本电脑(服务器)与我的 windows 笔记本电脑(客户端)连接时,它会收到 DHCPDISCOVER 并在尝试广播 DHCPOFFER 时引发异常。

I'm currently using this configuration for the network, but so far I've tried multiple variations我目前正在为网络使用此配置,但到目前为止我已经尝试了多种变体

....DHCPMessage.BROADCAST_ADDR = InetAddress.getByName("255.255.255.255")....
....DHCPMessage.CLIENT_PORT = 68....

try {
        byte[] data = back.msgToByteArr();
        socket.setBroadcast(true);
        
        
        var pack = new DatagramPacket(data, data.length,
                DHCPMessage.BROADCAST_ADDR, DHCPMessage.CLIENT_PORT);
        
        var arr = new byte[]{1};
        var pack1 = new DatagramPacket(arr, arr.length,
                DHCPMessage.BROADCAST_ADDR, DHCPMessage.CLIENT_PORT);
        
        var pack2 = new DatagramPacket(arr, arr.length,
                InetAddress.getByName("192.168.0.255"), DHCPMessage.CLIENT_PORT);
        
        // after next line "java.io.IOException: Network is unreachable" is raised
        // regardless of trying to send pack, pack1 or pack2 (or many other variations I've tried so far)
        socket.send(pack2);

    } catch (IOException e) {
        return;
    }

Now, these are just a few of the last ones I've tried sending, after successfully receiving DHCPDISCOVER现在,这些只是我在成功接收 DHCPDISCOVER 后尝试发送的最后几个

pack  <<-- the real DHCPOFFER message, 
pack1 <<-- one of other attempts I've made, with just one byte sending  
pack2 <<-- another attempt where I tried to broadcast keeping in mind the mask

One more thing that might be worth mentioning is that once I turn wifi on on the server laptop (now both eth and wifi are up), the DHCPOFFER goes through, but now I never get DHCPREQUEST from client, its stuck on DHCPDISCOVER / DHCPOFFER looping few times.可能值得一提的另一件事是,一旦我在服务器笔记本电脑上打开 wifi(现在 eth 和 wifi 都已启动),DHCPOFFER 就会通过,但现在我从来没有从客户端收到 DHCPREQUEST,它卡在 DHCPDISCOVER / DHCPOFFER 循环中几次。

Last thing is that while the wifi is on on the server laptop, when I connect with my phone, the whole communication goes through DHCPDISCOVER->DHCSPOFFER->DHCPREQUEST->DHCPACK (at which point I stop having internet access on my phone, but I'm guessing that's since the assigned private IP is not good for the public domain).最后一件事是,当服务器笔记本电脑上的 wifi 开启时,当我连接手机时,整个通信都通过 DHCPDISCOVER->DHCSPOFFER->DHCPREQUEST->DHCPACK (此时我的手机停止访问互联网,但是我猜这是因为分配的私有 IP 对公共领域不利)。 This is the least important thing for me since I want to make the ethernet connected network work, not the wifi one, but maybe it carries some important info.这对我来说是最不重要的事情,因为我想让以太网连接的网络工作,而不是 wifi 网络,但它可能包含一些重要信息。

Ok so... even though I am still not 100% able to explain WHY the broadcast was throwing the said exception, BUT!好吧……尽管我仍然不能 100% 解释为什么广播会抛出上述异常,但是! I managed to find a way to make everything work .我设法找到了让一切正常的方法 Turns out, there was no need for change in the code事实证明,没有必要更改代码

but rather the setup of the network on linux itself.而是linux 本身的网络设置。

I'm posting how I fixed it if some unlucky soul as myself stumbles upon something similar.如果像我这样不幸的灵魂偶然发现类似的东西,我将发布我如何修复它。 Also, there sure are some knowledgeable people out there and some of them might take the time to explain what was happening.此外,肯定有一些知识渊博的人,他们中的一些人可能会花时间解释正在发生的事情。

MY GUESS: (and mind, its just a guess) the server laptop didn't have a way to communicate on network and was searching for the IP itself - (read - from another DHCP) so when it was auto assigned an address, that address wasn't good for the needed network, therefore the problem was in the network settings (possibly propagating to sockets) rather than in the code itself (or the receiving part - 255.255.255.255 BROADCAST).我的猜测:(请注意,这只是一个猜测)服务器笔记本电脑没有办法在网络上进行通信,并且正在搜索 IP 本身 - (从另一个 DHCP 读取)所以当它被自动分配一个地址时,地址对所需的网络不利,因此问题出在网络设置(可能传播到套接字)而不是代码本身(或接收部分 - 255.255.255.255 BROADCAST)中。

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

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