Package java.nio.channels

Examples of java.nio.channels.ServerSocketChannel


   
    private ServerSocket serverSocket;
   
    public NioSocketServer(boolean useNIO) throws IOException {
        if (useNIO) {
            ServerSocketChannel ssc = ServerSocketChannel.open();
            ssc.configureBlocking(false);
            ServerSocket ss = ssc.socket();
            ss.bind(new InetSocketAddress(LISTEN_PORT));
           
            this.selector = Selector.open();
            ssc.register(this.selector, SelectionKey.OP_ACCEPT);
        } else {
            this.serverSocket = new ServerSocket(LISTEN_PORT);
            this.serverSocket.setSoTimeout(500);
        }
View Full Code Here


        Set selectedKeys = this.selector.selectedKeys();
        Iterator i = selectedKeys.iterator();
        while (i.hasNext()) {
            SelectionKey key = (SelectionKey) i.next();
            if (key.isAcceptable()) {
                ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
                SocketChannel sc = ssc.accept();
                sc.configureBlocking(false);
                sc.register(this.selector, SelectionKey.OP_READ);
            } else if (key.isReadable()) {
                SocketChannel sc = (SocketChannel) key.channel();
                ByteBuffer buffer = ByteBuffer.allocate(10);
View Full Code Here

  }

  private void accept(SelectionKey key) throws IOException {
    // For an accept to be pending the channel must be a server socket
    // channel.
    ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();

    // Accept the connection and make it non-blocking
    SocketChannel socketChannel = serverSocketChannel.accept();
    socketChannel.configureBlocking(false);

    // Register the new SocketChannel with our Selector, indicating
    // we'd like to be notified when there's data waiting to be read
    socketChannel.register(this.selector, SelectionKey.OP_READ);
View Full Code Here

        }

        _isShuttingDown = false;
        ConnectionAcceptor acceptor = null;
        ChannelRegistrar processors[] = new ChannelRegistrar[this._serverInfo.getTargetedConnectionProcessors()];
        ServerSocketChannel channel = null;

        try
        {
            channel = ServerSocketChannel.open();
            channel.configureBlocking( false );
            channel.socket().setReuseAddress( true );
            InetSocketAddress address = new InetSocketAddress( _serverInfo.getInterface(), _serverInfo.getPort() );
            channel.socket().bind( address, 1024 );
            this._serverInfo.setHost( address.getHostName() + ':' + address.getPort() );
            // this._connectionHeader.setChannel(channel);
            for ( int i = 0; i < processors.length; i++ )
            {
                processors[i] = new CombinedConnectionProcessor( this._serverInfo );
                ((Thread) processors[i]).setDaemon( true );
            }

            acceptor = new ConnectionAcceptor( channel, processors );
            //acceptor.setDaemon( true );
        }
        catch ( IOException e )
        {
            logger.log( Level.SEVERE, "!!! IOException occured !!! ", e );
            IOUtil.closeQuiet( channel );
            throw (e);
        }

        for ( int i = 0; i < processors.length; i++ )
        {
            ((CombinedConnectionProcessor) processors[i]).start();
        }
        acceptor.start();

        this._serverInfo.setServerRunning( true );
        this._processors = processors;
        this._acceptor = acceptor;
        this._channel = channel;

        EJServerRegistry.getInstance().register( this );

        if ( logger.isLoggable( Level.INFO ) )
        {
            logger.log( Level.INFO, "EJOE server listening on: " + channel.socket().getLocalSocketAddress() );
            logger.log( Level.INFO, "Using " + processors.length + " Connection Processor"
                    + (processors.length > 1 ? "s" : "") );
            logger.log( Level.INFO, "Using non-blocking IO: " + this._serverInfo.hasNonBlockingReadWrite() );
            logger.log( Level.INFO, "Allowing persistent client connections: " + this._serverInfo.isPersistent() );
            logger.log( Level.INFO, "Using compression: " + this._serverInfo.hasCompression() );
View Full Code Here

      final RecordingAcceptorMonitor monitor = new RecordingAcceptorMonitor();
      manager.setMonitor( monitor );
      manager.setSoTimeout( 10 );
      final String name = "name";
      assertEquals( "isConnected pre connect", false, manager.isConnected( name ) );
      final ServerSocketChannel channel = ServerSocketChannel.open();
      final ServerSocket serverSocket = channel.socket();
      serverSocket.setReuseAddress( true );
      final InetAddress localAddress = InetAddress.getLocalHost();
      final Random random = new Random();
      final int port = Math.abs( random.nextInt() % 5000 ) + 1024;
      final InetSocketAddress address = new InetSocketAddress( localAddress, port );
View Full Code Here

                        s.OnWrite();
                    }
                }
                if ((key.readyOps() & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT)
                {
                    ServerSocketChannel ch = (ServerSocketChannel)key.channel();
                    java.net.ServerSocket ss = (java.net.ServerSocket)ch.socket();
                    Socket s = (Socket)key.attachment();
                    if (s != null)
                    {
//                        System.out.println(s + ": OnRead(ACCEPT)");
                        s.OnRead(); // ListenSocket.OnRead will call OnAccept on new Socket
                    }
                }
                if ((key.readyOps() & SelectionKey.OP_CONNECT) == SelectionKey.OP_CONNECT)
                {
                    SocketChannel ch = (SocketChannel)key.channel();
                    java.net.Socket ss = (java.net.Socket)ch.socket();
                    Socket s = (Socket)key.attachment();
                    if (s != null)
                    {
//                        System.out.println(s + ": OnConnect");
                        ch.finishConnect();
                        s.SetConnecting(false);
                        s.GetKey().interestOps(SelectionKey.OP_READ);
                        s.OnConnect();
                    }
                }
View Full Code Here

    {
        // Instead of creating a ServerSocket,
        // create a ServerSocketChannel
        try
        {
            ServerSocketChannel ssc = ServerSocketChannel.open();
           
            // Set it to non-blocking, so we can use select
            ssc.configureBlocking( false );
           
            // Get the Socket connected to this channel, and bind it
            // to the listening port
            ServerSocket ss = ssc.socket();
            InetSocketAddress isa = new InetSocketAddress( port );
            ss.bind( isa );
           
            attach(ssc);
            return 0;
View Full Code Here

   
    public void OnRead()
    {
        try
        {
            ServerSocketChannel ssc = (ServerSocketChannel)GetChannel();
            java.net.ServerSocket ss = (java.net.ServerSocket)ssc.socket();
            // It's an incoming connection.
            // Register this socket with the Selector
            // so we can listen for input on it
            try
            {
View Full Code Here

        }
    } // OnRead

    public void OnDelete()
    {
        ServerSocketChannel sc = (ServerSocketChannel)GetChannel();
        try
        {
            sc.close();
        }
        catch (IOException e)
        {
            Handler().LogError(this, "OnDelete", 0, e.toString(), SocketHandler.LOG_LEVEL_ERROR);
        }
View Full Code Here

  }

  public Selector getSelector()
  {
    try {
      ServerSocketChannel channel = _ss.getChannel();

      if (channel == null)
        return null;
     
      SelectorProvider provider = channel.provider();

      if (provider != null)
        return provider.openSelector();
      else
        return null;
View Full Code Here

TOP

Related Classes of java.nio.channels.ServerSocketChannel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.