Is this statement correct in Java?

I want to use datagram socket to transfer data between two computers I use the following lines:

host=InetAddress.getByAddress("mypc",new byte[]{192,168,1,110});

But when I use the above statement, I get this error: "precision may be lost"

So I convert int to bytes in this way:

InetAddress.getByAddress("mypc",new byte[]{(byte)192,(byte)168,(byte)1,(byte)110});

Will the above statement work now?

Solution

If you already use it in a string, just use getbyname():

InetAddress host = InetAddress.getByName("192.168.1.110");

Using bytes is confusing and can be dangerous (due to the signature bytes used in Java) If you can, stick to strings

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>