Jump to content
  • Advertisement
Sign in to follow this  
SinoMan

Java non-blocking sockets

This topic is 3714 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();
        server.configureBlocking(false);
        InetAddress ia = InetAddress.getLocalHost();
        InetSocketAddress isa = new InetSocketAddress(ia, PORT);
        server.socket().bind(isa);
        System.out.println("OK");
        System.out.println(server.toString());
    }
    
    public void startServer() throws IOException, InterruptedException {
        init();
        SelectionKey acceptKey = server.register(sel, SelectionKey.OP_ACCEPT);
        while (acceptKey.selector().select() > 0) {
            Thread.sleep(1);
            Set readyKeys = sel.selectedKeys();
            Iterator it = readyKeys.iterator();
            
            while (it.hasNext()) {
                SelectionKey key = (SelectionKey)it.next();
                it.remove();
                if (key.isAcceptable()) {
                    System.out.println("Is acceptable");
                    ServerSocketChannel nextReady = (ServerSocketChannel)key.channel();
                    SocketChannel channel = nextReady.accept();
                    channel.configureBlocking(false);
                    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);
                    callback.getChannel().read(byteBuffer);
                    
                    byte cmd = byteBuffer.get(0);
                    if (cmd == 10 && key.isWritable()) {
                        System.out.println("Sending");
                        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
                        reply.flip();
                        
                        callback.getChannel().write(reply);
                    }
                }
            } // 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
Advertisement
Hi

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.

Regards
elFarto

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!