[snmp] TrapPduv1.getIpAddress() returns negative values

Jordi Vea vea at inicia.es
Tue Nov 7 11:21:27 GMT 2006


Hi Birgit,

You're right. I didn't know this method.
Thanks again.

Kind regards,
Jordi

Birgit Arkesteijn escribió:
> Hi Jordi,
>
> I realise I missed out the subject line yesterday and only read the
> message body. Yes, Josh hit the nail on the head.
>
> For printing the IP address, why don't you use the JDK, instead of doing
> it yourself? By using
>     java.net.InetAddress.getByAddress(byte[] addr).getHostName()
> you'll make sure the code works for IPv4 and IPv6.
>
> Cheers, Birgit
>
>
> On Tue, Nov 07, 2006 at 12:19:47AM +0100, Jordi Vea i Barbany wrote:
>   
>> You are right. The stack wasn't the problem. The thing is that you 
>> can't cast byte to int so easily.
>>
>> To represent it as a String I was doing:
>>
>>    byte[] addr = trap.getIpAddress();
>>    String agentAddr = addr[0] + "." + addr[1] + "." + addr[2] + "." + 
>> addr[3];
>>
>> Doing the following solves the problem:
>>
>>    byte[] addr = trap.getIpAddress();
>>    String agentAddr = ( addr[0] & 0xFF ) + "." + ( addr[1] & 0xFF ) + 
>> "." + ( addr[2] & 0xFF ) + "." + ( addr[3] & 0xFF ) ;
>>
>> Thanks a lot.
>>
>> Jordi Vea
>>     
>
>   



More information about the snmp mailing list