package serverUtils; import java.io.IOException; import java.nio.channels.SelectionKey;…
- User
package serverUtils;
import java.io.IOException; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.util.Iterator;
public class Console {
public void start() throws IOException { ConnectionManager connectionManager = new ConnectionManager(6789);
connectionManager.initialize();
Selector selector = Selector.open(); connectionManager.registerSelector(selector);
while (true) { int readyChannelsCount = selector.select(); if (readyChannelsCount == 0) { continue; } Iterator selectedKeysIterator = selector.selectedKeys().iterator(); while (selectedKeysIterator.hasNext()) { SelectionKey key = selectedKeysIterator.next(); selectedKeysIterator.remove(); connectionManager.processQuery(selector, key); } } } }
package serverUtils;
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.*; import java.nio.charset.StandardCharsets;
public class ConnectionManager { private static final String ACK_MESSAGE_PREFIX = “<%s>Ack”; private static final int BUFFER_SIZE = 1024 * 1024;
ServerSocketChannel serverSocketChannel; int port;
public ConnectionManager(int port) { this.port = port; }
public void initialize() throws IOException { serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.socket().bind(new InetSocketAddress(port)); serverSocketChannel.configureBlocking(false); System.out.println(“Server started on port " + port); System.out.println(”============ * ============"); }
public void registerSelector(Selector selector) throws ClosedChannelException { serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); }
public void processQuery(Selector selector, SelectionKey key) throws IOException { if (!key.isValid()) { return; } if (key.isAcceptable()) { acceptConnection(key, selector); } else if (key.isReadable()) { handleRead(key); } } private void acceptConnection(SelectionKey key, Selector selector) throws IOException { ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel(); SocketChannel clientSocketChannel = serverSocketChannel.accept(); clientSocketChannel.configureBlocking(false); clientSocketChannel.register(selector, SelectionKey.OP_READ);
System.out.println(“Accepted connection from " + clientSocketChannel.getRemoteAddress()); }
private void handleRead(SelectionKey key) throws IOException { ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE); SocketChannel clientSocketChannel = (SocketChannel) key.channel(); int bytesRead; while ((bytesRead = clientSocketChannel.read(buffer)) > 0) { buffer.flip();
// проверяем наличие данных в буфере if (buffer.remaining() < 8) { continue; }
// читаем длину имени файла в байтах int nameLength = buffer.getInt();
// проверяем наличие данных в буфере if (buffer.remaining() < nameLength + 4) { buffer.position(buffer.position() - 4); buffer.compact(); continue; }
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.