Package java.nio.channels

Examples of java.nio.channels.SocketChannel


    } catch (Exception e) {
      port = PookaMessagingConstants.S_PORT;
    }
    getLogger().log(Level.FINE, "opening port " + port);
    SocketAddress address = new InetSocketAddress("localhost",port);
    SocketChannel channel = SocketChannel.open();
    channel.configureBlocking(false);
    if (! channel.connect(address)) {
      // we're willing to wait for about a second.
      for (int i = 0; (! channel.finishConnect()) && i < 4; i++) {
  try {
    getLogger().log(Level.FINE, "not connected; sleeping (" + i + ").");
    Thread.currentThread().sleep(250);
  } catch (Exception e) {
  }
      }
    }
    if (channel.isConnected()) {
      getLogger().log(Level.FINE, "got connection.");
      mChannel = channel;
      mChannel.configureBlocking(true);

      mConnected = true;
View Full Code Here


    public void Open(String host,int port)
    {
        InetSocketAddress isa = new InetSocketAddress(host, port);
        try
        {
            SocketChannel sc = SocketChannel.open();
            //        SocketChannel sc = (SocketChannel)GetChannel();
            sc.configureBlocking(false);
            if (!sc.connect(isa))
            {
                SetConnecting();
            }
            else
            {
View Full Code Here

    public void OnRead()
    {
        m_ibuf.clear();
        try
        {
            SocketChannel sc = (SocketChannel)GetChannel();
            int n = sc.read(m_ibuf);
            m_ibuf.flip();
            if (n == -1)
            {
                Handler().LogError(this, "OnRead", 0, "error while reading", SocketHandler.LOG_LEVEL_FATAL);
                SetCloseAndDelete();
View Full Code Here

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

    public void OnWrite()
    {
        m_obuf.flip();
        try
        {
            SocketChannel sc = (SocketChannel)GetChannel();
            int n = sc.write(m_obuf);
            System.out.println(n + " bytes written");
        }
        catch (Exception e)
        {
            Handler().LogError(this, "OnWrite", 0, e.toString(), SocketHandler.LOG_LEVEL_FATAL);
View Full Code Here

                SelectionKey key = (SelectionKey)it.next();
               
                // What kind of activity is it?
                if ((key.readyOps() & SelectionKey.OP_READ) == SelectionKey.OP_READ)
                {
                    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 + ": OnRead");
                        s.OnRead();
                        if (s.LineProtocol())
                        {
                            s.ReadLine(); // eat ibuf to m_line, calls OnLine
                        }
                    }
                }
                if ((key.readyOps() & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE)
                {
                    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 + ": OnWrite");
                        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

                java.net.Socket js = ss.accept();
                System.out.println( "Got connection from " + js);
               
                // Make sure to make it non-blocking, so we can
                // use a selector on it.
                SocketChannel sc = js.getChannel();
                sc.configureBlocking( false );
               
                // Register it with the selector, for reading
                //        sc.register( selector, SelectionKey.OP_READ );
               
                Socket s = m_creator.Create();
View Full Code Here

        int remotePort = getPortFrom(context.getRuntime(), args[1]);
        String localHost = args.length >= 3 ? args[2].convertToString().toString() : null;
        int localPort = args.length == 4 ? getPortFrom(context.getRuntime(), args[3]) : 0;

        try {
            SocketChannel channel = null;
            if(localHost == null) {
                InetSocketAddress addr = new InetSocketAddress(InetAddress.getByName(remoteHost), remotePort);
                channel = SocketChannel.open(addr);
            } else {
                channel = SocketChannel.open();
                Socket socket = channel.socket();
                socket.bind(new InetSocketAddress(InetAddress.getByName(localHost), localPort));
                socket.connect(new InetSocketAddress(InetAddress.getByName(remoteHost), remotePort));
            }
            channel.finishConnect();
            initSocket(context.getRuntime(), new ChannelDescriptor(channel, RubyIO.getNewFileno(), new ModeFlags(ModeFlags.RDWR), new FileDescriptor()));
        } catch (InvalidValueException ex) {
            throw context.getRuntime().newErrnoEINVALError();
        } catch(ConnectException e) {
            throw context.getRuntime().newErrnoECONNREFUSEDError();
View Full Code Here

                if (!ready) {
                    // we were woken up without being selected...poll for thread events and go back to sleep
                    context.pollThreadEvents();
                } else {
                    try {
                        SocketChannel connected = ssc.accept();
                        connected.finishConnect();
                       
                        //
                        // Force the client socket to be blocking
                        //
                        synchronized (connected.blockingLock()) {
                            connected.configureBlocking(false);
                            connected.configureBlocking(true);
                        }
       
                        // otherwise one key has been selected (ours) so we get the channel and hand it off
                        socket.initSocket(context.getRuntime(), new ChannelDescriptor(connected, RubyIO.getNewFileno(), new ModeFlags(ModeFlags.RDWR), new FileDescriptor()));
                    } catch (InvalidValueException ex) {
View Full Code Here

        {
            try {
                if(_length == -1){
                    throw new JodbIOException("no client transaction data size available");
                }
                SocketChannel socketChannel = _readSocket.getChannel();
                long transfered = _file.transferFrom(socketChannel, 0, _length);
                if(JODBConfig.DEBUG){
                    Utils.getLogger(getClass().getName()).log(Level.INFO, "Transfered "+ transfered +" to file "+_file);
                }
                /*
 
View Full Code Here

TOP

Related Classes of java.nio.channels.SocketChannel

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.