gpt4 book ai didi

java - 非阻塞 write() 无限 - java

转载 作者:行者123 更新时间:2023-12-02 05:03:51 26 4
gpt4 key购买 nike

我有一个简单的网络 getter 。并且 OP_WRITE 始终触发选择器,因此 select() 永远不会结束。 Select 始终返回 1,等待写入,尽管 channel 已完成其工作并关闭。我该如何处理?我的 CPU 也变得非常高。

代码如下:

public static void main(String [] args)
{

try
{
String adder="";

Selector selector= simpleFetcher.selector;
selector = Selector.open();

Charset charset = Charset.forName("ISO-8859-1");

try{

//add to selector the host and settings
addHost(selector, "localhost", 80);
addHost(selector, "site.org", 80);

} catch (UnresolvedAddressException ex) {System.out.println("Address Not exist "+ex.getMessage());}

while(true)
{

int selectedn = selector.select(2000);
if (selectedn==0)
{
Set<SelectionKey> SelectorKeys = selector.keys();
Iterator<SelectionKey> iterator = SelectorKeys.iterator();

while(iterator.hasNext())
{
SelectionKey k = (SelectionKey) iterator.next();
SocketChannel channel = (SocketChannel) k.channel();

String rHost;
InetAddress addr = channel.socket().getInetAddress();
rHost= addr.getHostAddress();
System.out.println(rHost+" is timeout");

}
System.out.println("adder:"+adder);
selector.close();

System.out.println("No more keys.. Exiting");
return;
}

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

while(iterator.hasNext())
{
SelectionKey key = (SelectionKey) iterator.next();
iterator.remove();

if (key.isValid()==false) {System.out.println("key not valid"); continue;}


try{
if (key.isConnectable())
{

System.out.println("Will connect right now");

SocketChannel channel = (SocketChannel) key.channel();

if (!channel.finishConnect()) { System.out.println("Not finished connection. continue.."); continue; }
if (channel.isConnectionPending()==false && channel.socket().isClosed()==true) System.out.println("CLOSED?");


SelectionKey k = channel.register(key.selector(),SelectionKey.OP_READ);
SelectionKey k2 = channel.register(key.selector(),SelectionKey.OP_WRITE);
//SelectionKey k = channel.register(key.selector(),SelectionKey.OP_READ | SelectionKey.OP_WRITE);
k.attach(ByteBuffer.allocate(4000));


System.out.println("Just Connected");
continue;
}
if (key.isReadable())
{
int readB;
System.out.println("Ready for reading..");
SocketChannel channel = (SocketChannel) key.channel();
ByteBuffer buffer = (ByteBuffer) key.attachment();
CharBuffer charbuffer = buffer.asCharBuffer();

if ((readB=channel.read(buffer)) !=-1)
{
System.out.println("Key accepted - reading..");
int current_capacity=buffer.position();

buffer.flip();
CharBuffer c = charset.decode(buffer);
char[] arr = c.array();


String data = new String(arr);

String[] lines = data.split("\\r\\n\\r\\n");
if (lines.length>1)
{
String header = lines[0];
String rest_body = lines[1];
//System.out.println("Header is: "+header);
//System.out.println("Body is: "+rest_body);
adder+=header+rest_body;
}
else { adder+=new String(arr); }


}
else
{
key.channel().close();
key.cancel();
System.out.println("Key cancled");
System.out.println(adder);

continue;
}
if (readB==0) System.out.println("The READ RETURNS 0");

System.out.println();
buffer.clear();

continue;

}
if (key.isWritable())
{

//System.out.println("Ready to write");

SocketChannel channel = (SocketChannel) key.channel();
String rHost;

Socket s = channel.socket();
s.shutdownOutput();



InetAddress addr = channel.socket().getInetAddress();
rHost= addr.getHostAddress();
//System.out.println(rHost);

if (s.isOutputShutdown()) continue;

String bytestowrite="GET / HTTP/1.1\r\nHost: "+rHost+"\r\nUser-agent: Agent 1.0 Experimental\r\nAccept: */*\r\nAccept-Language: en-US,en;q=0.5\r\nConnection: keep-alive\r\n\r\n";
ByteBuffer buffer = ByteBuffer.wrap(bytestowrite.getBytes());

while(buffer.hasRemaining()) channel.write(buffer);
key.cancel();
continue;
}


} catch (IOException ex) {
System.out.println("EXCEPTION: "+ex.getMessage());
key.cancel();
try { key.channel().close();}
catch (IOException cex) {};
}

System.out.println("End of iterator loop");
}
}
}catch (IOException ex) {System.out.println("Timeout");}



}

最佳答案

套接字 channel 几乎总是可写的,这意味着套接字发送缓冲区中几乎总是有可用空间。只有当发送者跑赢接收者时,这种情况才不再成立。

因此,为 OP_WRITE 注册 channel 是不正确的,除非它们已经经历了缓冲区已满的情况,即如果 write() 返回零,并且 OP_WRITE 应尽快取消注册后续写入已成功。

关于java - 非阻塞 write() 无限 - java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27992762/

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