Package java.nio.channels

Examples of java.nio.channels.ServerSocketChannel


            _sndBufDTM.order(ByteOrder.LITTLE_ENDIAN);
        }
    }

    public void start() throws ServiceException {
        final ServerSocketChannel channel = createServerSocketChannel();
        final Thread pagerThread = new Thread(SRV_NAME) {
            public void run() {
                try {
                    acceptConnections(channel);
                } catch (IOException e) {
View Full Code Here


        public void handle(SelectionKey key) throws ClosedChannelException, IOException {
            if(!key.isValid()) {
                return;
            }
            ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
            SocketChannel socketChannel = serverChannel.accept();
            if(LOG.isDebugEnabled()) {
                LOG.debug("accepted a connection from " + socketChannel.socket().getInetAddress());
            }
            socketChannel.configureBlocking(false);
            socketChannel.register(key.selector(), SelectionKey.OP_READ, nextHandler);
View Full Code Here

            }
            continue;
          }

          if ((ops & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT) {
            ServerSocketChannel serverChannel = this.serverChannel;
            if (serverChannel == null) continue;
            try {
              SocketChannel socketChannel = serverChannel.accept();
              if (socketChannel != null) acceptOperation(socketChannel);
            } catch (IOException ex) {
              if (DEBUG) debug("kryonet", "Unable to accept new connection.", ex);
            }
            continue;
View Full Code Here

    if (INFO && connections.length > 0) info("kryonet", "Closing server connections...");
    for (int i = 0, n = connections.length; i < n; i++)
      connections[i].close();
    connections = new Connection[0];

    ServerSocketChannel serverChannel = this.serverChannel;
    if (serverChannel != null) {
      try {
        serverChannel.close();
        if (INFO) info("kryonet", "Server closed.");
      } catch (IOException ex) {
        if (DEBUG) debug("kryonet", "Unable to close server.", ex);
      }
      this.serverChannel = null;
View Full Code Here

    }

    public void start() throws Exception {
        Map.Entry           entry;
        Selector            selector;
        ServerSocketChannel sock_channel;
        MyInetSocketAddress key, value;

        if (remote !=null && local !=null)
            mappings.put(new InetSocketAddress(local, local_port), new InetSocketAddress(remote, remote_port));
       
        if (mapping_file !=null) {
            try {
                populateMappings(mapping_file);
            }
            catch (Exception ex) {
                log("Failed reading " + mapping_file);
                throw ex;
            }
        }

        log("\nProxy started at " + new java.util.Date());

        if (verbose) {
            log("\nMappings:\n---------");
            for (Iterator it=mappings.entrySet().iterator(); it.hasNext();) {
                entry=(Map.Entry) it.next();
                log(toString((InetSocketAddress) entry.getKey()) + " <--> "
                    + toString((InetSocketAddress) entry.getValue()));
            }
            log("\n");
        }

        // 1. Create a Selector
        selector=Selector.open();

        // Create a thread pool (Executor)
        executor=new ThreadPoolExecutor(MIN_THREAD_POOL_SIZE, MAX_THREAD_POOL_SIZE, 30000, TimeUnit.MILLISECONDS,
                                        new LinkedBlockingQueue(1000));

        for (Iterator it=mappings.keySet().iterator(); it.hasNext();) {
            key=(MyInetSocketAddress) it.next();
            value=(MyInetSocketAddress) mappings.get(key);

            // if either source or destination are SSL, we cannot use JDK 1.4
            // NIO selectors, but have to fall back on separate threads per connection

            if (key.ssl() || value.ssl()) {
                // if(2 == 2) {
                SocketAcceptor acceptor=new SocketAcceptor(key, value);
                executor.execute(acceptor);
                continue;
            }

            // 2. Create a ServerSocketChannel
            sock_channel=ServerSocketChannel.open();
            sock_channel.configureBlocking(false);
            sock_channel.socket().bind(key);

            // 3. Register the selector with all server sockets. 'Key' is attachment, so we get it again on
            //    select(). That way we can associate it with the mappings hashmap to find the corresponding
            //    value
            sock_channel.register(selector, SelectionKey.OP_ACCEPT, key);
        }

        // 4. Start main loop. won't return until CTRL-C'ed       
        loop(selector);
    }
View Full Code Here

    /** We handle only non-SSL connections */
    void loop(Selector selector) {
        Set                 ready_keys;
        SelectionKey        key;
        ServerSocketChannel srv_sock;
        SocketChannel       in_sock, out_sock;
        InetSocketAddress   src, dest;

        while (true) {
            if (verbose)
                log("[Proxy] ready to accept connection");

            // 4. Call Selector.select()
            try {
                selector.select();

                // get set of ready objects
                ready_keys=selector.selectedKeys();
                for (Iterator it=ready_keys.iterator(); it.hasNext();) {
                    key=(SelectionKey) it.next();
                    it.remove();

                    if (key.isAcceptable()) {
                        srv_sock=(ServerSocketChannel) key.channel();
                        // get server socket and attachment
                        src=(InetSocketAddress) key.attachment();
                        in_sock=srv_sock.accept(); // accept request
                        if (verbose)
                            log("Proxy.loop()", "accepted connection from " + toString(in_sock));
                        dest=(InetSocketAddress) mappings.get(src);
                        // find corresponding dest
                        if (dest == null) {
View Full Code Here

       
        Logger.log(new LogEvent(LOGID, "NetworkAdmin: ipv4 supported: "+supportsIPv4+"; ipv6: "+supportsIPv6+"; probing v6+nio functionality"));
       
        if(newV6)
        {
          ServerSocketChannel channel = ServerSocketChannel.open();
         
          try
          {
            channel.configureBlocking(false);
            channel.socket().bind(new InetSocketAddress(anyLocalAddressIPv6, 0));
            Logger.log(new LogEvent(LOGID, "NetworkAdmin: testing nio + ipv6 bind successful"));

            supportsIPv6withNIO = true;
          } catch (Exception e)
          {
            Logger.log(new LogEvent(LOGID,LogEvent.LT_WARNING, "nio + ipv6 test failed",e));
            supportsIPv6withNIO = false;
          }
         
          channel.close();
        } else
          supportsIPv6withNIO = false;
         
        if ( !first_time ){
         
View Full Code Here

   
    protected boolean
    canBind(
      InetAddress  bind_ip )
    {
      ServerSocketChannel ssc = null;
     
      try{
        ssc = ServerSocketChannel.open();
     
        ssc.socket().bind( new InetSocketAddress(bind_ip,0), 16 );
       
        return( true );
       
      }catch( Throwable e ){
       
        return( false );
       
      }finally{
       
        if ( ssc != null ){
   
          try{
            ssc.close();
           
          }catch( Throwable e ){
           
            Debug.out( e );
          }
View Full Code Here

        localAddresses = TcpOutgoingConnector.findLocalAddresses();
    }

    public URI accept(Action<ConnectEvent<Connection<Object>>> action) {
        ServerSocketChannel serverSocket;
        URI localAddress;
        try {
            serverSocket = ServerSocketChannel.open();
            serverSockets.add(serverSocket);
            serverSocket.socket().bind(new InetSocketAddress(0));
            localAddress = new URI(String.format("tcp://localhost:%d", serverSocket.socket().getLocalPort()));
            LOGGER.debug("Listening on {}.", localAddress);
        } catch (Exception e) {
            throw UncheckedException.asUncheckedException(e);
        }
View Full Code Here

    connect_selector = new VirtualChannelSelector( name, VirtualChannelSelector.OP_CONNECT, true );
    write_selector   = new VirtualChannelSelector( name, VirtualChannelSelector.OP_WRITE, true );
   
    try{
     
      final ServerSocketChannel  ssc = ServerSocketChannel.open();
     
      ServerSocket ss  = ssc.socket();
     
      ss.setReuseAddress(true);

      ss.bindnew InetSocketAddress( InetAddress.getByName("127.0.0.1"), port), 128 );
     
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.