网络字节顺序到主机字节顺序的转换Java [英] Network byte order to host byte order conversion Java

查看:84
本文介绍了网络字节顺序到主机字节顺序的转换Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过jna使用Windows-dll与某些加密狗服务器进行通信. dll提供了三个功能,分别是连接,断开连接和获取加密狗的连接状态.连接和断开工作.但是,当我查询状态时,设备应返回一个我保存到DWORD中的IP地址.代码类似于,其中DWORDByReference IpAddress是输出参数:

I am using a windows-dll through jna for communicating with some dongle servers. The dll provides three functions, which are connect, disconnect and getting the connection status of the dongles. Connecting and disconnecting works. But when I query the status, the device should return an ip-address which i save into a DWORD. The code is something like, where DWORDByReference IpAddress is an out-parameter:

int AwUsbGetConnectionStatus(String Hub, DWORDByReference IpAddress, IntByReference Status, DWORD Timeout, HANDLE Event); DWORD value = ipAddress.getValue();

int AwUsbGetConnectionStatus(String Hub, DWORDByReference IpAddress, IntByReference Status, DWORD Timeout, HANDLE Event); DWORD value = ipAddress.getValue();

dll的规范指出:"IpAddress以主机字节顺序,可以使用WinSock htonl函数转换为TCP/IP网络字节顺序."

The specification of the dll states: "IpAddress is in host byte order and can be converted to TCP/IP network byte order using the WinSock htonl function."

在另一个线程中,我读到与htonl等效的java是:

In another thread i read that the java equivalent to htonl is:

static int htonl(int val) { return ByteBuffer.allocate(4).putInt(val) .order(ByteOrder.nativeOrder()).getInt(0); }

static int htonl(int val) { return ByteBuffer.allocate(4).putInt(val) .order(ByteOrder.nativeOrder()).getInt(0); }

但是问题是,我得到了奇怪的值.但是它们确实以某种方式对应于ip地址的假定值.
例如,当原始IP为:192.168.102.118
它返回1986439360
当我使用上述方法将其转换为-1062705546.
但我想正确转换后应该是192168102118

我尝试的另一个Ip地址是: 192.168.102.112 为此我得到1885776064 而当我将其转换为-106270552

But the problem is, that i get weird values. But somehow they do correspond to the supposed value of the ip-addresses.
For example when the original Ip was: 192.168.102.118
it returns 1986439360
and when i convert it with the method above, it's -1062705546.
But i guess it should be 192168102118 when converted properly

Also another Ip-Address i tried was: 192.168.102.112 for which i get 1885776064 and when i convert that it's -106270552

因此,对于原始地址和转换后的值,Ip-Addresses之间的差仍然为6.我尝试使用不同的Ips进行尝试,并且Ips之间的差异始终与假定的值匹配. 所以有人知道,如果我转换dll函数返回的值的方式有问题,或者当我获得DWORD参数的值时甚至可能有问题吗?

So the difference between the Ip-Addresses is still 6 for the original Address and for the converted value. I tried this with different Ips and the difference between the Ips always matches the supposed values. So does anyone know, if there is something wrong with the way i convert the value the dll-function returns or could there even be a problem when i get the value of the DWORD parameter?

推荐答案

您应设置顺序之前将小端地址放入其中.

You should set the order before putting the little-endian address into it.

然后(您的注释正确转换后我应该是192168102118")出现您想要的点分四进制输出;为此,使用字节数组可能更自然:

And it appears (from your comment "i guess it should be 192168102118 when converted properly") that you want dotted-quad output; for that, using a byte array may be more natural:

static byte[] htoip(int ipv4) {
  return ByteBuffer.wrap(new byte[4])
                   .order(ByteOrder.nativeOrder())
                   .putInt(ipv4)
                   .array();
}

此结果适合传递给 InetAddress.getByAddress()

This result is suitable for passing to InetAddress.getByAddress(), etc.

虽然这不是反转4个字节的最简单方法,但它使用的ByteOrder.nativeOrder()使其具有可移植性和面向未来的特点.

While this isn't the simplest way to reverse 4 bytes, its use of ByteOrder.nativeOrder() makes it portable and future-proof.

这篇关于网络字节顺序到主机字节顺序的转换Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆