Page 1 of 1

Socket Server Multithreading Java nio

Posted: 06 May 2025, 09:35
by Anonymous
Ich brauche Hilfe, um einen Multithread -Java -Socket -Server zu erstellen, der die Anforderung asynchron verarbeiten und verarbeiten kann. Die Anforderung wird asynchron bearbeitet, ohne dass die zweite Anfrage abgeschlossen ist.import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
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.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;

public class NIOServer {
private static Selector selector = null;

public static void main(String[] args)
{

try {
selector = Selector.open();
// We have to set connection host,port and
// non-blocking mode
ServerSocketChannel serverSocketChannel
= ServerSocketChannel.open();
ServerSocket serverSocket
= serverSocketChannel.socket();
serverSocket.bind(
new InetSocketAddress("localhost", 8089));
serverSocketChannel.configureBlocking(false);
int ops = serverSocketChannel.validOps();
serverSocketChannel.register(selector, ops,
null);
while (true) {
selector.select();
Set selectedKeys
= selector.selectedKeys();
Iterator i
= selectedKeys.iterator();

while (i.hasNext()) {
SelectionKey key = i.next();

if (key.isAcceptable()) {
// New client has been accepted
handleAccept(serverSocketChannel,
key);
}
else if (key.isReadable()) {
// We can run non-blocking operation
// READ on our client
handleRead(key);
}
i.remove();
}
}
}
catch (IOException e) {
e.printStackTrace();
}
}

private static void
handleAccept(ServerSocketChannel mySocket,
SelectionKey key) throws IOException
{

System.out.println("Connection Accepted..");

// Accept the connection and set non-blocking mode
SocketChannel client = mySocket.accept();
client.configureBlocking(false);

// Register that client is reading this channel
client.register(selector, SelectionKey.OP_READ);
}

private static void handleRead(SelectionKey key)
throws IOException
{
// System.out.println("Reading client's message.");

// create a ServerSocketChannel to read the request
SocketChannel client = (SocketChannel)key.channel();

// Create buffer to read data
ByteBuffer buffer = ByteBuffer.allocate(1024);

client.read(buffer);

// Parse data from buffer to String
String data = new String(buffer.array()).trim();
if (data.length() > 0) {
System.out.println("Received message: " + data);

System.out.println(new Date());

if (data.equalsIgnoreCase(
"Hello, server! Request 2")) {
try {
Thread.sleep(30000);
System.out.println("Sleep is executed");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

if (data.equalsIgnoreCase(
"Testing5")) {
client.close();
System.out.println("Connection closed...");
}
}
}
}
< /code>
Habt ihr einen Rat, wie man mit diesem Mechanismus umgeht?
Danke < /P.>