2

I have an app that I am now developing multiplayer for. However I do not have 2 phones so I have been running a separate program on my pc (note: not the emulator!) which simulates my program and the multi-player aspects.

However on the PC I have the following code:

Packet input = inputQueue.take();

                if (clientAddress == input.getAddress())
                {
                    switch (input.type)
                    {

This works fine, Packet is simply a simple class I wrote to take info out of a Datagram packet and getAddress() returns an InetAddress. Client address is set previously in the code.

However the android app has exactly the same code, literally line for line exactly the same and this InetAddress will not equate to the other? If I take the String of the 2 InetAddress's using getHostName() or something and compare them then it DOES equal the other.

Am I doing something wrong and assuming something by thinking I can compare the two objects with an == sign? Should it be .equals() ? I thought android used the same java.net code but could there be a difference?

What IS the best way to ensure I have the same address using InetAddress's?

4

2 回答 2

10

It should be equals(). In Java, always compare objects using equals. == operator compares references, not content.

于 2011-10-31T14:32:13.890 回答
3

不要比较 using ==,它会检查相同的对象,而不是相同的内容。而是使用.equals().

于 2011-10-31T14:32:40.263 回答