Есть приложение на java. Вот серверная часть: package serverUtils; import java.io.File;…
- User
Есть приложение на java. Вот серверная часть: 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; }
// читаем имя файла byte[] nameBytes = new byte[nameLength]; buffer.get(nameBytes); String fileName = new String(nameBytes, StandardCharsets.UTF_8);
// читаем длину файла в байтах int fileLength = buffer.getInt();
// проверяем наличие данных в буфере if (buffer.remaining() < fileLength + 8) { buffer.position(buffer.position() - 8); buffer.compact(); continue; }
// читаем содержимое файла byte[] fileContent = new byte[fileLength]; buffer.get(fileContent, 0, fileLength);
// проверяем разделитель long separator = buffer.getLong(); if (separator != 0) { throw new RuntimeException(“Invalid separator”); }
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.