/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
staticvoid compare(InetSocketAddress a, InetSocketAddress b) {
Inet6Address a1 = (Inet6Address)a.getAddress();
Inet6Address b1 = (Inet6Address)b.getAddress();
compare (a1, b1);
}
staticvoid compare(InetAddress a, InetAddress b) {
Inet6Address a1 = (Inet6Address)a;
Inet6Address b1 = (Inet6Address)b; if (!a1.equals(b1)) {
System.out.printf("%s != %s\n", a1, b1); thrownew RuntimeException("Addresses not the same");
}
if (!a1.getHostAddress().equals(b1.getHostAddress())) {
System.out.printf("%s != %s\n", a1, b1); if (a1.getScopeId() != b1.getScopeId()) thrownew RuntimeException("Scope ids not the same");
}
}
staticvoid socketTest(InetSocketAddress a) throws Exception {
System.out.printf("socketTest: address %s\n", a); try (ServerSocket server = new ServerSocket(0, 5, a.getAddress())) {
InetAddress saddr = server.getInetAddress(); int port = server.getLocalPort();
Socket client = new Socket(saddr, port);
compare(client.getInetAddress(), saddr); try {
client.close();
} catch (IOException e) {}
}
}
staticvoid dgSocketTest(InetSocketAddress a, boolean connect) throws Exception { try (DatagramSocket s = new DatagramSocket(null)) {
System.out.println("dgSocketTest: " + a);
s.bind(a);
String t = "Hello world";
DatagramPacket rx = new DatagramPacket(newbyte[128], 128); int port = s.getLocalPort();
InetSocketAddress dest = new InetSocketAddress(a.getAddress(), port);
DatagramPacket tx = new DatagramPacket(t.getBytes("ISO8859_1"), t.length(), dest); if (connect) {
s.connect(dest);
System.out.println("dgSocketTest: connect remote addr = " + s.getRemoteSocketAddress());
compare(a, (InetSocketAddress)s.getRemoteSocketAddress());
}
s.send(tx);
s.receive(rx);
String t1 = new String(rx.getData(), rx.getOffset(), rx.getLength(), "ISO8859_1"); if (!t1.equals(t)) thrownew RuntimeException("Packets not equal");
}
}
staticvoid dgChannelTest(InetSocketAddress a, boolean connect) throws Exception { try (DatagramChannel s = DatagramChannel.open()) {
System.out.println("dgChannelTest: " + a);
s.bind(a);
String t = "Hello world";
ByteBuffer rx = ByteBuffer.allocate(128); int port = ((InetSocketAddress)s.getLocalAddress()).getPort();
InetSocketAddress dest = new InetSocketAddress(a.getAddress(), port);
ByteBuffer tx = ByteBuffer.wrap(t.getBytes("ISO8859_1")); if (connect) {
s.connect(dest);
System.out.println("dgChannelTest: connect remote addr = " + s.getRemoteAddress());
compare(a, (InetSocketAddress)s.getRemoteAddress());
}
s.send(tx, dest);
s.receive(rx);
rx.flip();
String t1 = new String(rx.array(), 0, rx.limit(), "ISO8859_1");
System.out.printf("rx : %s, data: %s\n", rx, t1); if (!t1.equals(t)) thrownew RuntimeException("Packets not equal");
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.