Results 1 to 5 of 5
  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Help with NIO Server

    I'm trying to make a basic server application (which I will eventually mold into something for a game I am working on), but I am having a lot of trouble understanding exactly what's going on. I included my Server class below.

    Server started on port 4123
    To test, I just run telnet -> open localhost 4123 -> type stuff
    When I open the connection, the server prints this (as it should):
    Accepted: /0:0:0:0:0:0:0:1:50543
    Then, every time I type a single character it prints:
    In From: /0:0:0:0:0:0:0:1:50540
    Read: 0000000000000000

    The data is always all zeros.
    Then, when I disconnect by closing the cmd prompt (telnet), it just constantly prints the read message again forever.
    In From: /0:0:0:0:0:0:0:1:50540
    Read: 0000000000000000
    In From: /0:0:0:0:0:0:0:1:50540
    Read: 0000000000000000
    In From: /0:0:0:0:0:0:0:1:50540
    Read: 0000000000000000
    In From: /0:0:0:0:0:0:0:1:50540
    Read: 0000000000000000
    ...


    Really not sure what I'm doing wrong. :/

    Java Code:
    package org.nubcraft.api.net.server;
    
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.net.ServerSocket;
    import java.nio.ByteBuffer;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.nio.channels.ServerSocketChannel;
    import java.nio.channels.SocketChannel;
    import java.util.Iterator;
    import java.util.Set;
    
    public class Server implements Runnable //, IServer
    {
    	private boolean running;
    	private ServerSocketChannel channel;
    	private ServerSocket socket;
    	private Selector selector;
    	private final int port;
    	
    	public static void main(String[] args)
    	{
    		Server s = new Server(4123);
    		try
    		{
    			s.start();
    			System.out.println("Server started on port 4123");
    		}
    		catch (IOException e)
    		{
    			e.printStackTrace();
    		}
    	}
    	
    	public Server(int port)
    	{
    		running = false;
    		channel = null;
    		socket = null;
    		selector = null;
    		this.port = port;
    	}
    	
    	public void start() throws IOException
    	{
    		channel = ServerSocketChannel.open();
    		channel.configureBlocking(false);
    		socket = channel.socket();
    		InetSocketAddress address = new InetSocketAddress(port);
    		socket.bind(address);
    		
    		Thread iothread = new Thread(this, "IO Thread");
    		iothread.start();
    		
    		running = true;
    	}
    
    	@Override
    	public void run()
    	{
    		try
    		{
    			selector = Selector.open();
    			channel.register(selector, SelectionKey.OP_ACCEPT);
    		}
    		catch(IOException e)
    		{
    			handleException(e);
    			return;
    		}
    		
    		while(running)
    		{
    			try
    			{
    				selector.select();
    				Set<SelectionKey> keys = selector.selectedKeys();
    				Iterator<SelectionKey> it = keys.iterator();
    				while(it.hasNext())
    				{
    					SelectionKey key = it.next();
    					try
    					{
    						if(key.isReadable())
    							handleRead(key);
    						else if(key.isAcceptable())
    							handleAccept(key);
    						else if(key.isConnectable())
    							handleConnect(key);
    						else if(key.isWritable())
    							handleWrite(key);
    					}
    					catch(IOException e)
    					{
    						key.cancel();
    						handleException(e);
    					}
    					finally
    					{
    						it.remove();
    					}
    				}
    			}
    			catch(IOException e)
    			{
    				handleException(e);
    			}
    		}
    		
    		if(selector != null)
    		{
    			try
    			{
    				selector.close();
    			}
    			catch(IOException e)
    			{
    				handleException(e);
    			}
    		}
    	}
    	
    	public void handleAccept(SelectionKey key) throws IOException
    	{
    		ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
    		SocketChannel sc = ssc.accept();
    		sc.configureBlocking(false);
    		System.out.println("Accepted: " + sc.getRemoteAddress().toString());
    		sc.register(selector, SelectionKey.OP_READ);
    	}
    	
    	public void handleConnect(SelectionKey key) throws IOException
    	{
    		SocketChannel sc = (SocketChannel) key.channel();
    		System.out.println("Connected: " + sc.getRemoteAddress().toString());
    	}
    	
    	public void handleRead(SelectionKey key) throws IOException
    	{
    		SocketChannel sc = (SocketChannel) key.channel();
    		System.out.println("In From: " + sc.getRemoteAddress().toString());
    		ByteBuffer bb = ByteBuffer.allocate(16);
    		sc.read(bb);
    		System.out.print("Read: ");
    		while(bb.hasRemaining())
    		{
    			System.out.print(bb.get());
    		}
    		System.out.println();
    	}
    	
    	public void handleWrite(SelectionKey key) throws IOException
    	{
    		SocketChannel sc = (SocketChannel) key.channel();
    		System.out.println("Out To: " + sc.getRemoteAddress().toString());
    		ByteBuffer bb = ByteBuffer.allocate(16);
    		sc.read(bb);
    		System.out.print("Wrote: ");
    		while(bb.hasRemaining())
    		{
    			System.out.print(bb.get());
    		}
    		System.out.println();
    	}
    	
    	public void handleException(Exception e)
    	{
    		e.printStackTrace();
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help with NIO Server

    I haven't worked with the nio packages yet. Did you put this program together yourself or did you copy if from the tutorials or somewhere else?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Help with NIO Server

    Little bit of both. I based it off a few tutorials I was reading.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help with NIO Server

    Have you tried doing some debugging by printing out some values For example how many bytes are read by the read() method? What are the values for all the states of the ByteBuffer object? It has lots of methods to call for status.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Potato is offline Member
    Join Date
    Dec 2011
    Posts
    25
    Rep Power
    0

    Default Re: Help with NIO Server

    You forgot to flip the ByteBuffer after you read..

Similar Threads

  1. Replies: 6
    Last Post: 12-25-2011, 11:33 PM
  2. from local server to remote server
    By IDH in forum Java Servlet
    Replies: 1
    Last Post: 03-24-2011, 09:05 AM
  3. smtp server configuration with jboss server
    By vilas_patil in forum Java Servlet
    Replies: 0
    Last Post: 01-05-2009, 01:18 PM
  4. Does any file in an FTP server ends up in an HTTP server?
    By islamfunny in forum CLDC and MIDP
    Replies: 4
    Last Post: 08-15-2008, 04:30 PM

Posting Permissions

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