use of java.nio.channels.ServerSocketChannel in project robovm by robovm.
the class SocketChannelTest method test_finishConnect.
/**
* Regression test for Harmony-1947.
*/
public void test_finishConnect() throws Exception {
SocketAddress address = new InetSocketAddress("localhost", 0);
ServerSocketChannel theServerChannel = ServerSocketChannel.open();
ServerSocket serversocket = theServerChannel.socket();
serversocket.setReuseAddress(true);
// Bind the socket
serversocket.bind(address);
boolean doneNonBlockingConnect = false;
// Loop so that we make sure we're definitely testing finishConnect()
while (!doneNonBlockingConnect) {
channel1 = SocketChannel.open();
// Set the SocketChannel to non-blocking so that connect(..) does
// not block
channel1.configureBlocking(false);
boolean connected = channel1.connect(new InetSocketAddress("localhost", serversocket.getLocalPort()));
if (!connected) {
// Now set the SocketChannel back to blocking so that
// finishConnect() blocks.
channel1.configureBlocking(true);
doneNonBlockingConnect = channel1.finishConnect();
}
if (doneNonBlockingConnect) {
tryFinish();
}
channel1.close();
}
if (!serversocket.isClosed()) {
serversocket.close();
}
}
use of java.nio.channels.ServerSocketChannel in project robovm by robovm.
the class OldSocketChannelTest method test_socketChannel_read_DirectByteBuffer.
public void test_socketChannel_read_DirectByteBuffer() throws InterruptedException, IOException {
// RoboVM note: This test has been modified to properly close down the ServerThread and
// to close the SocketChannels opened even if the test fails half-way through.
Thread server = null;
try {
final ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(null, 0);
server = new Thread() {
@Override
public void run() {
try {
for (int i = 0; i < 2; ++i) {
ByteBuffer buf = ByteBuffer.allocate(10);
buf.put(data);
buf.rewind();
ssc.accept().write(buf);
}
} catch (Exception ignored) {
}
}
};
server.start();
// First test with array based byte buffer
ByteBuffer buf = null;
SocketChannel sc = SocketChannel.open();
try {
sc.connect(ssc.socket().getLocalSocketAddress());
buf = ByteBuffer.allocate(data.length);
buf.limit(data.length / 2);
sc.read(buf);
buf.limit(buf.capacity());
sc.read(buf);
} finally {
sc.close();
}
// Make sure the buffer is filled correctly
buf.rewind();
assertSameContent(data, buf);
// Now test with direct byte buffer
sc = SocketChannel.open();
try {
sc.connect(ssc.socket().getLocalSocketAddress());
buf = ByteBuffer.allocateDirect(data.length);
buf.limit(data.length / 2);
sc.read(buf);
buf.limit(buf.capacity());
sc.read(buf);
} finally {
sc.close();
}
// Make sure the buffer is filled correctly
buf.rewind();
assertSameContent(data, buf);
} finally {
done = true;
server.interrupt();
server.join(2000);
}
}
use of java.nio.channels.ServerSocketChannel in project robovm by robovm.
the class SelectorTest method test_57456.
public void test_57456() throws Exception {
Selector selector = Selector.open();
ServerSocketChannel ssc = ServerSocketChannel.open();
try {
// Connect.
ssc.configureBlocking(false);
ssc.socket().bind(null);
SocketChannel sc = SocketChannel.open();
sc.connect(ssc.socket().getLocalSocketAddress());
sc.finishConnect();
// Switch to non-blocking so we can use a Selector.
sc.configureBlocking(false);
// Have the 'server' write something.
ssc.accept().write(ByteBuffer.allocate(128));
// At this point, the client should be able to read or write immediately.
// (It shouldn't be able to connect because it's already connected.)
SelectionKey key = sc.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE);
assertEquals(1, selector.select());
assertEquals(SelectionKey.OP_READ | SelectionKey.OP_WRITE, key.readyOps());
assertEquals(0, selector.select());
} finally {
selector.close();
ssc.close();
}
}
use of java.nio.channels.ServerSocketChannel in project robovm by robovm.
the class OldServerSocketChannelTest method testConstructor.
// -------------------------------------------------------------------
// Test for methods in abstract class.
// -------------------------------------------------------------------
public void testConstructor() throws IOException {
ServerSocketChannel channel = SelectorProvider.provider().openServerSocketChannel();
assertNotNull(channel);
assertSame(SelectorProvider.provider(), channel.provider());
}
use of java.nio.channels.ServerSocketChannel in project java-in-action by xinghalo.
the class Server method main.
public static void main(String[] args) throws IOException {
ThreadPoolExecutor excutor = new ThreadPoolExecutor(3, 10, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(100));
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress(12345));
while (true) {
SocketChannel socketChannel = serverSocketChannel.accept();
if (socketChannel != null) {
System.out.println("接收心情求,启动新线程处理");
excutor.submit(new SocketChannelThread(socketChannel));
}
}
}
Aggregations