0

DHCP サーバーを介して情報を取得するアプリケーションを開発しています。そのため、ポート 67 UDP ブロードキャストで DHCPINFORM パケットを送信しています。私が直面している問題は、応答が得られないことです。つまり、DHCP サーバーからの DHCPACK またはその他のメッセージが常に返されます。つまり、一度に DHCPACK パケットが返される場合とまったく返されない場合があります。この問題の原因は何ですか?

これが私のネットワーク構成です 私のIP:192.168.3.31 DHCPサーバーは:192.168.3.108です

私が使用しているC#コードは次のとおりです

private void SendDHCPINFORM()
    {
        UdpClient udpClient = new UdpClient();
        IPEndPoint dhcpClientEndpoint = new IPEndPoint(IPAddress.Broadcast, 67);

        #region Making Data DHCPINFORM
        D_op = 1;
        D_htype = 1;
        D_hlen = 6;
        D_hops = 0;
        D_xid = new byte[4];
        Random objRandom = new Random(8000);
        objRandom.NextBytes(D_xid);

        D_secs = new byte[2];
        D_secs = BitConverter.GetBytes(Convert.ToInt16(0));

        D_flags = new byte[2];
        D_flags = BitConverter.GetBytes(Convert.ToInt16(0));

        D_ciaddr = new byte[4];
        D_ciaddr[0] = 192;
        D_ciaddr[1] = 168;
        D_ciaddr[2] = 3;
        D_ciaddr[3] = 31;

        D_yiaddr = new byte[4];
        D_yiaddr = BitConverter.GetBytes(0);

        D_siaddr = new byte[4];
        D_siaddr = BitConverter.GetBytes(0);

        D_giaddr = new byte[4];
        D_giaddr = BitConverter.GetBytes(0);

        //00-13-D3-D5-27-73
        D_chaddr = new byte[16];
        D_chaddr[0] = 0;
        D_chaddr[1] = 19;
        D_chaddr[2] = 211;
        D_chaddr[3] = 213;
        D_chaddr[4] = 39;
        D_chaddr[5] = 115;

        D_sname = new byte[64];


        D_file = new byte[128];

        M_Cookie = new byte[4];
        M_Cookie[0] = 99;
        M_Cookie[1] = 130;
        M_Cookie[2] = 83;
        M_Cookie[3] = 99;


        D_options = new byte[60];

        //Making DHCPINFORM message
        byte[] messageType = new byte[3];
        messageType[0] = 53;
        messageType[1] = 1;
        messageType[2] = 8; //Message Type from 1-8
        Array.Copy(messageType, D_options, messageType.Length);

        int options_offset = 3;

        byte[] serverIdentifier = new byte[6];
        serverIdentifier[0] = 54;
        serverIdentifier[1] = 4;
        serverIdentifier[2] = 192;
        serverIdentifier[3] = 168;
        serverIdentifier[4] = 3;
        serverIdentifier[5] = 108;
        Array.Copy(serverIdentifier, 0, D_options, options_offset, serverIdentifier.Length);

        options_offset += serverIdentifier.Length;
        byte[] dummyPacket = new byte[3];
        dummyPacket[0] = 116;
        dummyPacket[1] = 1;
        dummyPacket[2] = 1;
        Array.Copy(dummyPacket, 0, D_options, options_offset, dummyPacket.Length);

        options_offset += dummyPacket.Length;
        byte[] clientIdentifier = new byte[9];
        clientIdentifier[0] = 61;
        clientIdentifier[1] = 7;
        clientIdentifier[2] = 1;
        Array.Copy(D_chaddr, 0, clientIdentifier, 3, 6);
        Array.Copy(clientIdentifier, 0, D_options, options_offset, clientIdentifier.Length);

        options_offset += clientIdentifier.Length;
        byte[] hostName = new byte[7];
        hostName[0] = 12;
        hostName[1] = 5;
        Array.Copy(Encoding.ASCII.GetBytes("host1"), 0, hostName, 2, Encoding.ASCII.GetBytes("host1").Length);
        Array.Copy(hostName, 0, D_options, options_offset, hostName.Length);

        options_offset += hostName.Length;
        byte[] vendorClassID = new byte[10];
        vendorClassID[0] = 60;
        vendorClassID[1] = 8;
        //Array.Copy(Encoding.ASCII.GetBytes("Unspecified"), 0, vendorClassID, 2, Encoding.ASCII.GetBytes("Unspecified").Length);
        Array.Copy(Encoding.ASCII.GetBytes("MSFT 5.0"), 0, vendorClassID, 2, Encoding.ASCII.GetBytes("MSFT 5.0").Length);
        Array.Copy(vendorClassID, 0, D_options, options_offset, vendorClassID.Length);

        options_offset += vendorClassID.Length;
        byte[] paramRequestList = new byte[13];
        paramRequestList[0] = 55;
        paramRequestList[1] = 11;
        paramRequestList[2] = 1;
        paramRequestList[3] = 15;
        paramRequestList[4] = 3;
        paramRequestList[5] = 6;
        paramRequestList[6] = 44;
        paramRequestList[7] = 46;
        paramRequestList[8] = 47;
        paramRequestList[9] = 31;
        paramRequestList[10] = 33;
        paramRequestList[11] = 249;
        paramRequestList[12] = 43;
        //Array.Copy(paramRequestList, 0, D_options, 31, paramRequestList.Length);
        Array.Copy(paramRequestList, 0, D_options, options_offset, paramRequestList.Length);

        options_offset += paramRequestList.Length;
        byte[] vendorSpecificInfo = new byte[5];
        vendorSpecificInfo[0] = 43;
        vendorSpecificInfo[1] = 2;
        vendorSpecificInfo[2] = 220;
        vendorSpecificInfo[3] = 0;
        vendorSpecificInfo[4] = 255;
        Array.Copy(vendorSpecificInfo, 0, D_options, options_offset, vendorSpecificInfo.Length);

        byte[] dhcpMessage = new byte[300];
        dhcpMessage[0] = D_op;
        dhcpMessage[1] = D_htype;
        dhcpMessage[2] = D_hlen;
        dhcpMessage[3] = D_hops;

        int destinationIndex = 4;
        Array.Copy(D_xid, 0, dhcpMessage, destinationIndex, D_xid.Length);

        destinationIndex = destinationIndex + D_xid.Length;
        Array.Copy(D_secs, 0, dhcpMessage, destinationIndex, D_secs.Length);

        destinationIndex = destinationIndex + D_secs.Length;
        Array.Copy(D_flags, 0, dhcpMessage, destinationIndex, D_flags.Length);

        destinationIndex = destinationIndex + D_flags.Length;
        Array.Copy(D_ciaddr, 0, dhcpMessage, destinationIndex, D_ciaddr.Length);

        destinationIndex = destinationIndex + D_ciaddr.Length;
        Array.Copy(D_yiaddr, 0, dhcpMessage, destinationIndex, D_yiaddr.Length);

        destinationIndex = destinationIndex + D_yiaddr.Length;
        Array.Copy(D_siaddr, 0, dhcpMessage, destinationIndex, D_siaddr.Length);

        destinationIndex = destinationIndex + D_siaddr.Length;
        Array.Copy(D_giaddr, 0, dhcpMessage, destinationIndex, D_giaddr.Length);

        destinationIndex = destinationIndex + D_giaddr.Length;
        Array.Copy(D_chaddr, 0, dhcpMessage, destinationIndex, D_chaddr.Length);

        destinationIndex = destinationIndex + D_chaddr.Length;
        Array.Copy(D_sname, 0, dhcpMessage, destinationIndex, D_sname.Length);

        destinationIndex = destinationIndex + D_sname.Length;
        Array.Copy(D_file, 0, dhcpMessage, destinationIndex, D_file.Length);

        destinationIndex = destinationIndex + D_file.Length;
        Array.Copy(M_Cookie, 0, dhcpMessage, destinationIndex, M_Cookie.Length);

        destinationIndex = destinationIndex + M_Cookie.Length;
        Array.Copy(D_options, 0, dhcpMessage, destinationIndex, D_options.Length);
        #endregion

        udpClient.Send(dhcpMessage, 300, dhcpClientEndpoint);

        UdpClient udpServerResponse = new UdpClient(68);
        IPEndPoint dhcpServerEndPoint = new IPEndPoint(IPAddress.Any, 0);

        //The following line is receiving the response from DHCP server
        //works some time immediately and some time not even after couple 
        //of minutes and program goes to halt state? Am I making some mistake?
        byte[] dataReceived = udpServerResponse.Receive(ref dhcpServerEndPoint);

        Console.WriteLine("Message Received");
    }

私を助けてください?

4

2 に答える 2

1

何が問題なのかはよくわかりませんが、代わりに API を使用する方が簡単ではないでしょうか。

DHCP クライアント API http://msdn.microsoft.com/en-us/library/aa363344(v=VS.85).aspx

DHCP サーバー コールアウト API http://msdn.microsoft.com/en-us/library/aa363372(v=VS.85).aspx

于 2010-10-27T10:40:59.960 に答える
0

この問題は、私には単純な操作順序の問題のように見えますUdpClient。. .DHCPINFORMDHCPACK

UdpClient単一のバインディング ポート 68を使用して、トランザクション全体 (送受信) を実行できるはずです。

于 2013-11-13T21:52:46.947 に答える