Jump to content
  • Advertisement
Sign in to follow this  

Java non-blocking sockets

This topic is 3795 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Yo, I will keep this short. I'm writing a non-blocking server. Here's the main code:
 * To change this template, choose Tools | Templates
 * and open the template in the editor.

package javaapplication1;
import java.io.*;
import java.nio.channels.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.util.*;
import java.nio.channels.spi.SelectorProvider;

 * @author SinoMan
 * @thanks owlmountain.com
public class NonBlockingServer {
    public static final int CLIENTS_MAX = 10;
    public static final int PORT = 2000;
    public Selector sel = null;
    public ServerSocketChannel server = null;
    public SocketChannel socket = null;
    public void init() throws IOException,UnknownHostException {
        System.out.print("Initializing the server...");
        sel = SelectorProvider.provider().openSelector();
        server = ServerSocketChannel.open();
        InetAddress ia = InetAddress.getLocalHost();
        InetSocketAddress isa = new InetSocketAddress(ia, PORT);
    public void startServer() throws IOException, InterruptedException {
        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();
                if (key.isAcceptable()) {
                    System.out.println("Is acceptable");
                    ServerSocketChannel nextReady = (ServerSocketChannel)key.channel();
                    SocketChannel channel = nextReady.accept();
                    SelectionKey readKey =
                            channel.register(sel, SelectionKey.OP_READ|SelectionKey.OP_WRITE);
                    readKey.attach(new ChannelCallback(channel));
                else if (key.isReadable()) {
                    System.out.println("Is readable");
                    SelectableChannel nextReady = (SelectableChannel)key.channel();
                    ChannelCallback callback = (ChannelCallback)key.attachment();

                    ByteBuffer byteBuffer = ByteBuffer.allocate(100);
                    byte cmd = byteBuffer.get(0);
                    if (cmd == 10 && key.isWritable()) {
                        ByteBuffer reply = ByteBuffer.allocate(100);
                        reply.put((byte)0); // RAW_TEXT
                        reply.put((byte)4); // length
                        reply.put((byte)0); // length
                        reply.put((byte)2); // in-game channel
                        reply.put((byte)(127 + 10)); // 10 = color
                        reply.put((byte)'a'); // the text
                        reply.put((byte)0); // end of message
            } // while (it.hasNext())
        } // while (acceptKey.selector().select() > 0)
The thing is, how do I choose between writing/reading from client 1, 2, 3 etc.? Thx

Share this post

Link to post
Share on other sites

Firstly, you'll need to explain your question a bit more, currently it's a little vague.

Secondly, when registring the channel with the selector, do NOT register it for OP_WRITE. If you do the select() call will return when a channel is writable, which is 99.99% of the time (if not 100%).

I personally (and I've written a few NIO servers) don't bother with OP_WRITE and just write to the socket directly when I need to. You just need to be careful multiple threads don't attempt to write to the channel at the same time.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!