Results 1 to 5 of 5
Thread: Help with NIO Server
- 08-23-2012, 10:04 PM #1
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
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(); } }
- 08-23-2012, 11:39 PM #2
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.
- 08-24-2012, 12:20 AM #3
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
Re: Help with NIO Server
Little bit of both. I based it off a few tutorials I was reading.
- 08-24-2012, 02:01 AM #4
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.
- 08-25-2012, 06:31 AM #5
Member
- Join Date
- Dec 2011
- Posts
- 25
- Rep Power
- 0
Similar Threads
-
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorre
By nandhinianand in forum New To JavaReplies: 6Last Post: 12-25-2011, 11:33 PM -
from local server to remote server
By IDH in forum Java ServletReplies: 1Last Post: 03-24-2011, 09:05 AM -
smtp server configuration with jboss server
By vilas_patil in forum Java ServletReplies: 0Last Post: 01-05-2009, 01:18 PM -
Does any file in an FTP server ends up in an HTTP server?
By islamfunny in forum CLDC and MIDPReplies: 4Last Post: 08-15-2008, 04:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks