Search in sources :

Example 6 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project robovm by robovm.

the class ServerSocketChannelTest method test_socket_accept_Nonblocking_NotBound.

/**
     * @tests ServerSocket#socket().accept()
     */
public void test_socket_accept_Nonblocking_NotBound() throws IOException {
    // regression test for Harmony-748
    ServerSocket gotSocket = serverChannel.socket();
    serverChannel.configureBlocking(false);
    try {
        gotSocket.accept();
        fail("Should throw an IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e) {
    // expected
    }
    serverChannel.close();
    try {
        gotSocket.accept();
        fail("Should throw an IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e) {
    // expected
    }
}
Also used : ServerSocket(java.net.ServerSocket) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException)

Example 7 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project robovm by robovm.

the class ServerSocketChannelTest method test_socket_accept_Nonblocking_Bound.

/**
     * @tests ServerSocket#socket().accept()
     */
public void test_socket_accept_Nonblocking_Bound() throws IOException {
    // regression test for Harmony-748
    serverChannel.configureBlocking(false);
    ServerSocket gotSocket = serverChannel.socket();
    gotSocket.bind(localAddr1);
    try {
        gotSocket.accept();
        fail("Should throw an IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e) {
    // expected
    }
    serverChannel.close();
    try {
        gotSocket.accept();
        fail("Should throw a ClosedChannelException");
    } catch (ClosedChannelException e) {
    // expected
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) ServerSocket(java.net.ServerSocket) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException)

Example 8 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project robovm by robovm.

the class IllegalBlockingModeExceptionTest method test_Constructor.

/**
     * @tests {@link java.nio.channels.IllegalBlockingModeException#IllegalBlockingModeException()}
     */
public void test_Constructor() {
    IllegalBlockingModeException e = new IllegalBlockingModeException();
    assertNull(e.getMessage());
    assertNull(e.getLocalizedMessage());
    assertNull(e.getCause());
}
Also used : IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException)

Example 9 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project SimpleServerClient by DeBukkIt.

the class Server method startListening.

/**
 * Starts the listening thread waiting for messages from clients
 */
protected void startListening() {
    if (listeningThread == null && server != null) {
        listeningThread = new Thread(new Runnable() {

            @Override
            public void run() {
                while (server != null) {
                    try {
                        onLog("[Server] Waiting for connection" + (secureMode ? " using SSL..." : "..."));
                        @SuppressWarnings("resource") final Socket // potential resource leak. tempSocket might not be closed.
                        tempSocket = server.accept();
                        ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(tempSocket.getInputStream()));
                        Object raw = ois.readObject();
                        if (raw instanceof Datapackage) {
                            final Datapackage msg = (Datapackage) raw;
                            onLog("[Server] Message received: " + msg);
                            // inspect all registered methods
                            for (final String current : idMethods.keySet()) {
                                // if the current method equals the identifier of the Datapackage...
                                if (msg.id().equalsIgnoreCase(current)) {
                                    onLog("[Server] Executing method for identifier '" + msg.id() + "'");
                                    // execute the Executable on a new thread
                                    new Thread(new Runnable() {

                                        @Override
                                        public void run() {
                                            // Run the method registered for the ID of this Datapackage
                                            idMethods.get(current).run(msg, tempSocket);
                                            // and close the temporary socket if it is no longer needed
                                            if (!msg.id().equals(INTERNAL_LOGIN_ID)) {
                                                try {
                                                    tempSocket.close();
                                                } catch (IOException e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        }
                                    }).start();
                                    break;
                                }
                            }
                        }
                    } catch (IllegalBlockingModeException | IOException | ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        listeningThread.start();
    }
}
Also used : IOException(java.io.IOException) BufferedInputStream(java.io.BufferedInputStream) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) ObjectInputStream(java.io.ObjectInputStream)

Example 10 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project j2objc by google.

the class DatagramChannelTest method test_socket_IllegalBlockingModeException.

/**
 * @tests DatagramChannel#socket()
 */
public void test_socket_IllegalBlockingModeException() throws Exception {
    // regression test for Harmony-1036
    DatagramChannel channel = DatagramChannel.open();
    channel.configureBlocking(false);
    DatagramSocket socket = channel.socket();
    try {
        socket.send(null);
        fail("should throw IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e) {
    // expected
    }
    try {
        socket.receive(null);
        fail("should throw IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e) {
    // expected
    }
    channel.close();
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramChannel(java.nio.channels.DatagramChannel) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException)

Aggregations

IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)36 ServerSocket (java.net.ServerSocket)14 SocketChannel (java.nio.channels.SocketChannel)10 IOException (java.io.IOException)7 OutputStream (java.io.OutputStream)7 Socket (java.net.Socket)7 ClosedChannelException (java.nio.channels.ClosedChannelException)7 SelectionKey (java.nio.channels.SelectionKey)7 Selector (java.nio.channels.Selector)6 ServerSocketChannel (java.nio.channels.ServerSocketChannel)6 BindException (java.net.BindException)5 InetSocketAddress (java.net.InetSocketAddress)5 SocketTimeoutException (java.net.SocketTimeoutException)5 IllegalSelectorException (java.nio.channels.IllegalSelectorException)5 InputStream (java.io.InputStream)4 DatagramSocket (java.net.DatagramSocket)4 SocketException (java.net.SocketException)4 UnknownHostException (java.net.UnknownHostException)4 DatagramChannel (java.nio.channels.DatagramChannel)4 SocketAddress (java.net.SocketAddress)3