Incorrect transfer of data from device to server, how to fix?

So, write a simple server in the standard java library.nio.
The following problems arose:
1) Buffer is not cleaned at all. I.e. the information remains, making the application not possible.
2) the Server will be lightly loaded, to control the smart home system. Because the server has only one physical port for connection to external periphery. The question arises, how can we limit the class responsible for communication with several clients?
3) When the client disconnects from the server. The server starts randomly output the data from the buffer. Ie behaves as if the client is not disconnected and outputs the information from the buffer again and again.
The code itself:
import java.io.IOException;
import java.net.InetSocketAddress;
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.nio.charset.Charset;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;

public class Server {
 public static void main (String [] args) throws IOException{
 Selector selector = Selector.open(); // Open the selector.
 ServerSocketChannel serverSocket = ServerSocketChannel.open(); // Open the channel.
 serverSocket.bind(new InetSocketAddress(4321)); // Associate the socket with the given IP and port.
 serverSocket.configureBlocking(false); // Translate in not blocking mode.
 serverSocket.register(selector, SelectionKey.OP_ACCEPT); // register the selector and ustanavlivaem to the standby mode of the client.
 ByteBuffer buffer = ByteBuffer.allocate(256); // Allocate 256 byte for a buffer.


 while (true) {
 selector.select(); // Create a list.
 Set <selectionkey> selectedKey = selector.selectedKeys(); // Pass the list of keys.

 Iterator <selectionkey> iter = selectedKey.iterator(); // create the iterator.

 while (iter.hasNext()) {
 SelectionKey key = iter.next(); // Get the key to the channel.

 if (key.isAcceptable()) {
 register(selector, serverSocket);
}

 if (key.isReadable()) {
 reader (buffer, key);
}

iter.remove();
}
}

}


 private static void register (final Selector selector, final ServerSocketChannel serverSocket) throws IOException {
 assert !Objects.isNull(selector) && !Objects.isNull(serverSocket);
 SocketChannel client = serverSocket.accept(); // new user Connected.
 client.configureBlocking(false); // Translate into the blocking mode.
 client.register(selector, SelectionKey.OP_READ); // Registreren client reading mode.
}

 private static void reader (final ByteBuffer buffer, final SelectionKey key) throws IOException {
 assert !Objects.isNull(buffer) && Objects.isNull(key);
 SocketChannel client = (SocketChannel) key.channel(); // Get the channel.
 client.read(buffer); // write the value in the buffer.

 MessageFromClient String = new String(buffer.array()).trim();
System.out.println(messageFromClient);

buffer.flip();
client.write(buffer);
buffer.clear();
}
}</selectionkey></selectionkey>
June 3rd 19 at 20:23
0 answer

Find more questions by tags Java