Gradle:消息IO异常:无法将消息[EndOfStream]写入127.0.0.1(防火墙)?

omtl5h9j  于 2023-03-08  发布在  其他
关注(0)|答案(2)|浏览(168)

我编写了一个简单的测试项目,它打开端口9123一段时间后退出:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.Date;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
public class TimeServer
{
    private static final int PORT = 9123;
    public static void main( String[] args ) throws IOException
    {
        IoAcceptor acceptor = new NioSocketAcceptor();
        acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
        acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));
        acceptor.setHandler( new TimeServerHandler() );
        acceptor.getSessionConfig().setReadBufferSize( 2048 );
        acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );
        acceptor.bind( new InetSocketAddress(PORT) );
    }

    private static class TimeServerHandler extends IoHandlerAdapter {
        @Override
        public void exceptionCaught(IoSession session, Throwable cause ) throws Exception
        {
            cause.printStackTrace();
        }
        @Override
        public void messageReceived( IoSession session, Object message ) throws Exception
        {
            String str = message.toString();
            if( str.trim().equalsIgnoreCase("quit") ) {
                session.close();
                return;
            }
            Date date = new Date();
            session.write( date.toString() );
            System.out.println("Message written...");
        }
        @Override
        public void sessionIdle( IoSession session, IdleStatus status ) throws Exception
        {
            System.out.println( "IDLE " + session.getIdleCount( status ));
        }
    }
}

...

import java.io.IOException;

public class TimeServerTest {

   @Test
   public void runningTimeServerForTime() throws IOException, InterruptedException {

      int period = 15000;

      System.out.println("Running time server for " + period + "ms");

      TimeServer.main(new String[] {});

      Thread.sleep(period);

      System.out.println("Done, exiting");

      System.exit(0);

   }
}

当Windows防火墙关闭时,此测试在IntelliJ下运行正常。
当windows防火墙打开时,它失败,并出现异常

>gradle test
:compileJava
Note: PATH\TimeServer.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
:processResources UP-TO-DATE
:classes
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses
:test
Unexpected exception thrown.
org.gradle.messaging.remote.internal.MessageIOException: Could not write message [EndOfStream] to '/127.0.0.1:58895'.
        at org.gradle.messaging.remote.internal.inet.SocketConnection.dispatch(SocketConnection.java:111)
        at org.gradle.messaging.remote.internal.hub.MessageHub$ConnectionDispatch.run(MessageHub.java:284)
        at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
        at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.IOException: An existing connection was forcibly closed by the remote host
        at sun.nio.ch.SocketDispatcher.write0(Native Method)
        at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:51)
        at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
        at sun.nio.ch.IOUtil.write(IOUtil.java:51)
        at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471)
        at org.gradle.messaging.remote.internal.inet.SocketConnection$SocketOutputStream.flush(SocketConnection.java:236)
        at org.gradle.messaging.remote.internal.inet.SocketConnection.dispatch(SocketConnection.java:109)
        ... 6 more

BUILD SUCCESSFUL

发生什么事了?
为什么引用的端口58895与打开的端口9123非常不同?
如何使此测试运行而不禁用防火墙?
要添加到windows防火墙白名单中的程序是否运行正常?

rkttyhzu

rkttyhzu1#

这可能与gradle处理System.exit()调用的方式有关,请参见GRADLE-2759
根据经验,在测试中使用System.exit()调用不是一个好主意,因为它可能会扰乱测试框架(如果所有的东西都从同一个JVM启动呢?您会突然关闭它)。您应该添加一种方法来优雅地关闭服务器。
请注意,网络错误似乎与您的网络代码或服务器无关:您可以从堆栈跟踪(org.gradle.messaging.remote.internal)中的软件包名称中看到,该软件包来自Gradle。这就是端口与您指定的端口不同且无关的原因。
不知道为什么这会通过与Windows防火墙关闭虽然。

4ioopgfo

4ioopgfo2#

对我来说,这是因为我试图连接到的指定IP地址和端口中有一个错别字。一旦我修复了错别字,客户端就连接到了服务器,错误(未知端口54345或61927)就消失了。

相关问题