Thread: Non blocking server example...

Results 1 to 7 of 7
  1. #1 Non blocking server example... 
    Respected Member

    Join Date
    Jan 2009
    Posts
    5,682
    Thanks given
    1,093
    Thanks received
    3,494
    Discord
    View profile
    Rep Power
    5000
    Well I was having a good look through the NIO API libary and came up with this.

    Code:
    import java.io.*;
    import java.nio.*;
    import java.nio.channels.*;
    import java.net.*;
    import java.util.*;
    import java.nio.charset.*;
    import java.lang.*;
    
    public class RSX
    {
        public Selector sel = null;
        public ServerSocketChannel server = null;
        public SocketChannel socket = null;
        public int port = 5659;
        String result = null;
    
    
        public RSX()
        {
        }
        
    	public RSX(int port)
        {
    		port = port;
        }
    
        public void initializeOperations() throws IOException,UnknownHostException
        {
    		sel = Selector.open();
    		server = ServerSocketChannel.open();
    		server.configureBlocking(false);
    		InetAddress ia = InetAddress.getLocalHost();
    		InetSocketAddress isa = new InetSocketAddress(ia,port);
    		server.socket().bind(isa);
        }
        
    	public void startServer() throws IOException
        {
            initializeOperations();
    		SelectionKey acceptKey = server.register(sel, SelectionKey.OP_ACCEPT );	
    	
    		while (acceptKey.selector().select() > 0 )
    		{	
    	    
    			Set readyKeys = sel.selectedKeys();
    			Iterator it = readyKeys.iterator();
    
    			while (it.hasNext()) {
    				SelectionKey key = (SelectionKey)it.next();
    				it.remove();
                    
    				if (key.isAcceptable()) {
    					ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
    					socket = (SocketChannel) ssc.accept();
    					socket.configureBlocking(false);
    					SelectionKey another = socket.register(sel,SelectionKey.OP_READ|SelectionKey.OP_WRITE);
    				}
    			}
    		}
        }
        public static void main(String args[])
        {
    		RSX nb = new RSX();
    		try
    		{
    			nb.startServer();
    		}
    		catch (IOException e)
    		{
    			e.printStackTrace();
    			System.exit(-1);
    		}
    		
    	}
    }
    I have tested this works really well, this will accept all connections in the matter of a few millisecounds...

    WARNING: IF YOU HAVE A SMALL CPU PUT A SLEEP IN THE THREAD OR THIS WILL PWN YOUR CPU!
    Reply With Quote  
     

  2. #2  
    Respected Member


    Luke132's Avatar
    Join Date
    Dec 2007
    Age
    32
    Posts
    12,549
    Thanks given
    177
    Thanks received
    5,784
    Discord
    View profile
    Rep Power
    5000
    I put it in boolean process and got errors m8??

    EDIT : How can this 'pwn your cpu' when it isn't even running?

    EDIT2 : Why 2 constructors?

    Gj Mr.Rogue for posting code which dosen't work. i'm sure you will fit in around here.

    Reply With Quote  
     

  3. #3  
    SERGEANT OF THE MASTER SERGEANTS MOST IMPORTANT PERSON OF EXTREME SERGEANTS TO THE MAX!

    cube's Avatar
    Join Date
    Jun 2007
    Posts
    8,881
    Thanks given
    1,854
    Thanks received
    4,741
    Rep Power
    5000
    cool imports mate rep++



    Reply With Quote  
     

  4. #4  
    SERGEANT OF THE MASTER SERGEANTS MOST IMPORTANT PERSON OF EXTREME SERGEANTS TO THE MAX!

    cube's Avatar
    Join Date
    Jun 2007
    Posts
    8,881
    Thanks given
    1,854
    Thanks received
    4,741
    Rep Power
    5000
    Code:
    	public RSX(int port)
        {
    		port = port;
        }



    Reply With Quote  
     

  5. #5  
    Respected Member


    Luke132's Avatar
    Join Date
    Dec 2007
    Age
    32
    Posts
    12,549
    Thanks given
    177
    Thanks received
    5,784
    Discord
    View profile
    Rep Power
    5000
    Quote Originally Posted by S Quare Quxx View Post
    Code:
    	public RSX(int port)
        {
    		port = port;
        }
    Oh yeah looking at that, that won't work either, since it's setting the paramter port, to whatever the parameter is, you need to use this.variable = parameter if a class variable and parameter are named the same.

    Times like this i wish we still had the facepalm smiley.

    Reply With Quote  
     

  6. #6  
    SERGEANT OF THE MASTER SERGEANTS MOST IMPORTANT PERSON OF EXTREME SERGEANTS TO THE MAX!

    cube's Avatar
    Join Date
    Jun 2007
    Posts
    8,881
    Thanks given
    1,854
    Thanks received
    4,741
    Rep Power
    5000
    Quote Originally Posted by Luke132 View Post
    Oh yeah looking at that, that won't work either, since it's setting the paramter port, to whatever the parameter is, you need to use this.variable = parameter if a class variable and parameter are named the same.

    Times like this i wish we still had the facepalm smiley.
    ya we'd need facepalm



    Reply With Quote  
     

  7. #7  
    Registered Member
    DaraX's Avatar
    Join Date
    Apr 2009
    Posts
    1,183
    Thanks given
    18
    Thanks received
    29
    Rep Power
    262
    Nice.
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •