Jones
Jones

Reputation: 399

Java echo server client using IO multiplexing

I am relatively new to both client server and java programming. Have an assignment to do and I'm stuck with this program to write an echo client and server in java. I've always found my answers here and so once again I resort to stackoverflow.

This is the server:

public class Server_select {
public static void main(String[] args) throws IOException {
    int port = 4666;
    String address = "localhost";
    String channelserver = "Server Channel";
    String channelclient = "Client Channel";
    String typechannel = "Channel Type";
    ArrayList<Server_sockets> socketlist = new ArrayList<Server_sockets>();
    Iterator<Server_sockets> socketiterator = socketlist.iterator();
    ServerSocketChannel serverchannel = ServerSocketChannel.open();
    serverchannel.bind(new InetSocketAddress(address,port));
    serverchannel.configureBlocking(false);
    Selector selector = Selector.open();
    SelectionKey newconnectionkey = serverchannel.register(selector, SelectionKey.OP_ACCEPT);
    socketlist.add(new Server_sockets(serverchannel, newconnectionkey, channelserver));
    ArrayList<SelectionKey> selectedkeys = new ArrayList<SelectionKey>();
    Iterator<SelectionKey> keyiterator = selectedkeys.iterator();
    ByteBuffer buffer = ByteBuffer.allocate(4096);
    StringBuffer message = null;
    int count;
    for(;;){
        System.out.println("Entering the infinite for loop");
        if(selector.select() == 0){
            continue;
        }
        selectedkeys = (ArrayList<SelectionKey>) selector.selectedKeys();
        while(keyiterator.hasNext()){
            SelectionKey tempkey = keyiterator.next();
            while(socketiterator.hasNext()){
                if(socketiterator.next().key.equals(tempkey)){
                    if(socketiterator.next().channeltype == channelserver){
                        SocketChannel clientchannel = socketiterator.next().serverchannel.accept();
                        SelectionKey clientkey = clientchannel.register(selector, SelectionKey.OP_READ, SelectionKey.OP_WRITE);
                        socketlist.add(new Server_sockets(clientchannel,clientkey,channelclient));
                        System.out.println("Client connection established");
                    }
                    if(socketiterator.next().channeltype == channelclient){
                        if(socketiterator.next().key.isReadable()){
                            buffer.clear();
                            message = new StringBuffer("");
                            while((count = socketiterator.next().clientchannel.read(buffer))>0){
                                buffer.flip();
                                message.append(Charset.defaultCharset().decode(buffer));
                            }
                            System.out.println("Server here " + message);
                            buffer.clear();
                            while(!(socketiterator.next().key.isWritable())){
                                buffer.wrap(message.toString().getBytes());
                                while(buffer.hasRemaining()){
                                    socketiterator.next().clientchannel.write(buffer);
                                }
                                buffer.clear();
                            }
                        }
                    }
                }
            }
        }
    }
}

}

this is the client:

public class Client_select {


SocketChannel clientchannel;
int port = 4666;
String address = "localhost";
String message = "Hey there!";
Selector selector = null;
SelectionKey key = null;

Client_select() throws IOException{
    this.clientchannel = SocketChannel.open();
    this.clientchannel.configureBlocking(false);
    this.clientchannel.connect(new InetSocketAddress(this.address,this.port));
    this.selector = Selector.open();
    this.key = this.clientchannel.register(this.selector, SelectionKey.OP_READ, SelectionKey.OP_WRITE);
}

public void write_to_socket() throws IOException{
    ByteBuffer buffer = ByteBuffer.wrap(message.getBytes());
    while(buffer.hasRemaining()){
        this.clientchannel.write(buffer);
    }
}

public void read_from_socket() throws IOException{
    ByteBuffer buffer = ByteBuffer.allocate(4096);
    StringBuffer message = new StringBuffer("");
    int count=0;
    while((count = this.clientchannel.read(buffer)) > 0){
        buffer.flip();
        message.append(Charset.defaultCharset().decode(buffer));
    }
    System.out.println("Read From Socket" + message);
}
public static void main(String[] args) throws Exception {
    Client_select obj = new Client_select();
    ArrayList<SelectionKey> keylist = new ArrayList<SelectionKey>();
    Iterator<SelectionKey> iterator = keylist.iterator();
    for(;;){
        System.out.println("Entering Infinite loop Client");
        if(obj.selector.select() == 0){
            continue;
        }
        keylist = (ArrayList<SelectionKey>) obj.selector.selectedKeys();
        while(iterator.hasNext()){
            if(iterator.next().isWritable()){
                obj.write_to_socket();
            }
            if(iterator.next().isReadable()){
                obj.read_from_socket();
            }
        }
    }
}

}

The error is that the Server is getting terminated because of this error: Exception in thread "main" java.lang.ClassCastException: sun.nio.ch.Util$2 cannot be cast to java.util.ArrayList at Server_select.main(Server_select.java:40)

I just wanted to know if the rest is alright? I wanted to clarify a few other doubts that I have regarding this. Thanks in advance :)

Upvotes: 0

Views: 1177

Answers (1)

Rolf Rander
Rolf Rander

Reputation: 3321

I am not very experienced with java.nio, but it seems Selector.selectedKeys() returns a Set, not an ArrayList.

Upvotes: 2

Related Questions