gpt4 book ai didi

java - 从 NIO 服务器发送消息

转载 作者:行者123 更新时间:2023-12-01 08:54:32 26 4
gpt4 key购买 nike

下面的代码成功创建了服务器并接受传入的客户端。

package socket;

import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import java.util.*;
import java.io.IOException;

public class NonBlockingServer {

public static void main(String[] args) throws InterruptedException, IOException {

// Create a new Thread
Server s = new Server();
new Thread(s).start();

// Give 10 seconds for client to connect
Thread.sleep(10000);

// This Doesn't work?

s.Write("Hello, Client!");

System.out.println("Done");
}
}

//A class which implements Runnable Interface
class Server implements Runnable {

SocketChannel AcceptedClient;
ServerSocketChannel serverChannel;
Selector selector;

void Write(String s) throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(s.length());
buffer.put(s.getBytes());

int numWrite = -1;
numWrite = AcceptedClient.write(buffer);

while (buffer.hasRemaining())
{
numWrite += AcceptedClient.write(buffer);
}

System.out.println(numWrite);
}

@Override
public void run()
{

int port = 4041;

System.out.println("Listening for connections on port " + port);

try {
// Bind the port
serverChannel = ServerSocketChannel.open();
ServerSocket ss = serverChannel.socket();
InetSocketAddress address = new InetSocketAddress(port);
ss.bind(address);

// Non-blocking Server
serverChannel.configureBlocking(false);

// Register with Selector
selector = Selector.open();
serverChannel.register(selector, SelectionKey.OP_ACCEPT);

} catch (IOException ex) {
ex.printStackTrace();
return;
}

while (true) {

try {

// Blocks until a 'socket' is ready registered with selector is ready.
selector.select();

} catch (IOException ex) {
ex.printStackTrace();
break;
}

Set<SelectionKey> readyKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = readyKeys.iterator();

while (iterator.hasNext()) {

SelectionKey key = iterator.next();
iterator.remove();

try {

if (key.isAcceptable()) {

ServerSocketChannel server = (ServerSocketChannel) key.channel();
SocketChannel client = server.accept();
System.out.println("Accepted connection from " + client);
client.configureBlocking(false);

// Client accepted by server can read.
SelectionKey key2 = client.register(selector, SelectionKey.OP_READ);

AcceptedClient = (SocketChannel) key2.channel();

}

} catch (IOException ex) {
key.cancel();
try {
key.channel().close();
} catch (IOException cex) {
}
}
}
}
}
}

但是当我在连接到服务器后尝试向客户端发送消息时,它不起作用,即客户端没有收到该消息。

从服务器向特定客户端发送消息的正确方法是什么?

我查遍了互联网,没有找到任何服务器向客户端发送消息的示例。

最佳答案

您需要在 write() 之前flip() 缓冲区,如果您要保留它,则需要 compact() 之后。

注意关闭 channel 会取消 key 。

关于java - 从 NIO 服务器发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42135893/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com