因为jdk没有提供socket equals 方法,本人是这样进行比较的,见如下代码:
- Java code
Socket soc1 = new Socket();Socket soc2 = new Socket();soc1.connect(new InetSocketAddress("localhost", 6868), 20000);soc2.connect(new InetSocketAddress("localhost", 6868), 20000);System.out.println(soc1);System.out.println(soc2);System.out.println([color=#FF0000]soc1.getInetAddress().equals(soc2.getInetAddress()) && soc1.getPort() == soc2.getPort()[/color]);System.out.println(soc1.getInetAddress().toString() + " " + soc2.getInetAddress().toString() + "---" + soc1.getPort() + " " + soc2.getPort());System.out.println(soc1.equals(soc2));
输出为:
Socket[addr=localhost/127.0.0.1,port=6868,localport=3432]
Socket[addr=localhost/127.0.0.1,port=6868,localport=3434]
true
localhost/127.0.0.1 localhost/127.0.0.1---6868 6868
false
求高手,对吗?(红色部分)
------解决方案--------------------