use of java.nio.channels.ServerSocketChannel in project robovm by robovm.
the class AbstractSelectorTest method test_register_LSelectorI.
/**
* @tests AbstractSelector#register(Selector,int)
*/
public void test_register_LSelectorI() throws Exception {
Selector acceptSelector = SelectorProvider.provider().openSelector();
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
assertFalse(ssc.isRegistered());
SelectionKey acceptKey = ssc.register(acceptSelector, SelectionKey.OP_ACCEPT);
assertTrue(ssc.isRegistered());
assertNotNull(acceptKey);
assertTrue(acceptSelector.keys().contains(acceptKey));
}
use of java.nio.channels.ServerSocketChannel in project robovm by robovm.
the class SocketTest method checkSocketLocalAndRemoteAddresses.
public void checkSocketLocalAndRemoteAddresses(boolean setOptions) throws Exception {
InetAddress host = InetAddress.getLocalHost();
// Open a local server port.
ServerSocketChannel ssc = ServerSocketChannel.open();
InetSocketAddress listenAddr = new InetSocketAddress(host, 0);
ssc.socket().bind(listenAddr, 0);
ServerSocket ss = ssc.socket();
// Open a socket to the local port.
SocketChannel out = SocketChannel.open();
out.configureBlocking(false);
if (setOptions) {
out.socket().setTcpNoDelay(false);
}
InetSocketAddress addr = new InetSocketAddress(host, ssc.socket().getLocalPort());
out.connect(addr);
while (!out.finishConnect()) {
Thread.sleep(1);
}
SocketChannel in = ssc.accept();
if (setOptions) {
in.socket().setTcpNoDelay(false);
}
InetSocketAddress outRemoteAddress = (InetSocketAddress) out.socket().getRemoteSocketAddress();
InetSocketAddress outLocalAddress = (InetSocketAddress) out.socket().getLocalSocketAddress();
InetSocketAddress inLocalAddress = (InetSocketAddress) in.socket().getLocalSocketAddress();
InetSocketAddress inRemoteAddress = (InetSocketAddress) in.socket().getRemoteSocketAddress();
System.err.println("inLocalAddress: " + inLocalAddress);
System.err.println("inRemoteAddress: " + inRemoteAddress);
System.err.println("outLocalAddress: " + outLocalAddress);
System.err.println("outRemoteAddress: " + outRemoteAddress);
assertEquals(outRemoteAddress.getPort(), ss.getLocalPort());
assertEquals(inLocalAddress.getPort(), ss.getLocalPort());
assertEquals(inRemoteAddress.getPort(), outLocalAddress.getPort());
assertEquals(inLocalAddress.getAddress(), ss.getInetAddress());
assertEquals(inRemoteAddress.getAddress(), ss.getInetAddress());
assertEquals(outLocalAddress.getAddress(), ss.getInetAddress());
assertEquals(outRemoteAddress.getAddress(), ss.getInetAddress());
in.close();
out.close();
ssc.close();
assertNull(in.socket().getRemoteSocketAddress());
assertNull(out.socket().getRemoteSocketAddress());
assertEquals(in.socket().getLocalSocketAddress(), ss.getLocalSocketAddress());
}
use of java.nio.channels.ServerSocketChannel in project chuidiang-ejemplos by chuidiang.
the class Client method main.
public static void main(String[] args) {
try (ServerSocketChannel server = ServerSocketChannel.open()) {
server.bind(new InetSocketAddress("0.0.0.0", 5557));
while (true) {
SocketChannel client = server.accept();
new Thread(new Client(client)).start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of java.nio.channels.ServerSocketChannel in project chuidiang-ejemplos by chuidiang.
the class Client method main.
public static void main(String[] args) throws IOException {
ServerSocketChannel server = ServerSocketChannel.open();
server.bind(new InetSocketAddress("127.0.0.1", 5557));
while (true) {
SocketChannel client = server.accept();
new Thread(new Client(client)).start();
}
}
use of java.nio.channels.ServerSocketChannel in project jdk8u_jdk by JetBrains.
the class CloseAfterConnect method main.
public static void main(String[] args) throws Exception {
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(0));
InetAddress lh = InetAddress.getLocalHost();
final SocketChannel sc = SocketChannel.open();
final InetSocketAddress isa = new InetSocketAddress(lh, ssc.socket().getLocalPort());
// establish connection in another thread
Runnable connector = new Runnable() {
public void run() {
try {
sc.connect(isa);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
};
Thread thr = new Thread(connector);
thr.start();
// terminate
do {
try {
thr.join();
} catch (InterruptedException x) {
}
} while (thr.isAlive());
// check connection is established
if (!sc.isConnected()) {
throw new RuntimeException("SocketChannel not connected");
}
// close channel - this triggered the bug as it attempted to signal
// a thread that no longer exists
sc.close();
// clean-up
ssc.accept().close();
ssc.close();
}
Aggregations