简体   繁体   English

如何从客户端关闭套接字通道,以便服务器引发java.nio.channels.ClosedChannelException

[英]How to close a socket channel from client side so that the server throws java.nio.channels.ClosedChannelException

I have an issue where a server(Apache Synapse) intermittently throws "java.nio.channels.ClosedChannelException". 我遇到一个服务器(Apache Synapse)间歇性地抛出“ java.nio.channels.ClosedChannelException”的问题。

java.nio.channels.ClosedChannelException
at sun.nio.ch.SocketChannelImpl.ensureWriteOpen(SocketChannelImpl.java:135)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:326)
at org.apache.http.impl.nio.reactor.SessionOutputBufferImpl.flush(SessionOutputBufferImpl.java:167)
at org.apache.http.impl.nio.DefaultNHttpServerConnection.produceOutput(DefaultNHttpServerConnection.java:323)
at org.apache.synapse.transport.http.conn.LoggingNHttpServerConnection.produceOutput(LoggingNHttpServerConnection.java:112)
at org.apache.synapse.transport.passthru.ServerIODispatch.onOutputReady(ServerIODispatch.java:87)
at org.apache.synapse.transport.passthru.ServerIODispatch.onOutputReady(ServerIODispatch.java:39)
at org.apache.http.impl.nio.reactor.AbstractIODispatch.outputReady(AbstractIODispatch.java:143)
at org.apache.http.impl.nio.reactor.BaseIOReactor.writable(BaseIOReactor.java:180)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:342)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:316)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:277)
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:105)
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:584)
at java.lang.Thread.run(Thread.java:662)

Apparently the server tries to do some IO operation on an already closed channel. 显然,服务器尝试在已经关闭的通道上执行一些IO操作。 I want to write a client program to reproduce the issue at will. 我想编写一个客户端程序以随意重现该问题。 The client deliberately closes the channel resulting the server to throw the Exception. 客户端故意关闭通道,导致服务器抛出异常。 This is the code for the client. 这是客户端的代码。

public class Client
{
    String hostIp;
    int hostPort;

    public Client(String hostIp, int hostPort) {
        this.hostIp = hostIp;
        this.hostPort = hostPort;
    }

    public void runClient() throws IOException {
        Socket clientSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            clientSocket = new Socket(hostIp, hostPort);
            out = new PrintWriter(clientSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Unknown host: " + hostIp);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't connect to: " + hostIp);
            System.exit(1);
        }

        String getMessage = "GET /services/echo?wsdl2 HTTP/1.1\n" +
                "Host:localhost\n";

        System.out.println("Client connected to host : " + hostIp + " port: " + hostPort);

        out.println(getMessage);

        in.close();
        out.close();
        clientSocket.close();
    }

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

        Client client = new Client("localhost", 8280);
        client.runClient();
    }
}

When the client is run, the server just throws an IOException informing that the pipe is broken. 运行客户端时,服务器仅引发IOException,以通知管道已损坏。

java.io.IOException: Broken pipe
    at sun.nio.ch.FileDispatcher.write0(Native Method)
    at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:29)
    at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:69)
    at sun.nio.ch.IOUtil.write(IOUtil.java:40)
    at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:336)
    at org.apache.http.impl.nio.reactor.SessionOutputBufferImpl.flush(SessionOutputBufferImpl.java:100)
    at org.apache.http.impl.nio.DefaultNHttpServerConnection.produceOutput(DefaultNHttpServerConnection.java:220)
    at org.apache.synapse.transport.http.conn.LoggingNHttpServerConnection.produceOutput(LoggingNHttpServerConnection.java:112)
    at org.apache.synapse.transport.passthru.ServerIODispatch.onOutputReady(ServerIODispatch.java:87)
    at org.apache.synapse.transport.passthru.ServerIODispatch.onOutputReady(ServerIODispatch.java:39)
    at org.apache.http.impl.nio.reactor.AbstractIODispatch.outputReady(AbstractIODispatch.java:141)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.writable(BaseIOReactor.java:181)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:346)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:320)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:280)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:106)
    at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:604)
    at java.lang.Thread.run(Thread.java:662)

What should I do from the client side to make the server throw a "ClosedChannelException" ? 我应如何从客户端使服务器抛出“ ClosedChannelException”?

How to close a socket channel from client side so that the server throws java.nio.channels.ClosedChannelException 如何从客户端关闭套接字通道,以便服务器引发java.nio.channels.ClosedChannelException

You can't. 你不能

What should I do from the client side to make the server throw a "ClosedChannelException" ? 我应如何从客户端使服务器抛出“ ClosedChannelException”?

Nothing. 没有。

You have a basic misunderstanding of ClosedChannelException. 您对ClosedChannelException.有一个基本的误解ClosedChannelException. It is thrown when you have closed your local Channel and have kept using it. 关闭本地频道并继续使用它时,会抛出该错误。 Not when the connection has been closed by the peer. 对等方关闭连接不可以。

If you are looking for an end-of-stream indication, use the case where read() returns -1. 如果您正在寻找流结束指示,请使用read()返回-1的情况。

If you are looking for an indication of a dropped or failed connection, use the case where write() throws IOException: connection reset . 如果要查找连接丢失或失败的迹象,请使用write()抛出IOException: connection reset

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 写入客户端通道时出现异常java.nio.channels.ClosedChannelException - Exception java.nio.channels.ClosedChannelException when write to client channel java.nio.channels.ClosedChannelException - java.nio.channels.ClosedChannelException java.nio.channels.ClosedChannelException-客户端关闭SSL - java.nio.channels.ClosedChannelException -Client shuts down SSL 使用SSL和Postman的java.nio.channels.ClosedChannelException - java.nio.channels.ClosedChannelException with SSL and Postman java.nio.channels.ClosedChannelException HTTP/2 与码头 - java.nio.channels.ClosedChannelException HTTP/2 with jetty 异常-java.nio.channels.ClosedChannelException - exception - java.nio.channels.ClosedChannelException kafka.consumer.SimpleConsumer:由于套接字错误而重新连接:java.nio.channels.ClosedChannelException - kafka.consumer.SimpleConsumer: Reconnect due to socket error: java.nio.channels.ClosedChannelException jenkins主连接失败,出现java.nio.channels.ClosedChannelException - jenkins master connection fails with java.nio.channels.ClosedChannelException Stardog Connection.commit()引发java.nio.channels.ClosedChannelException - Stardog Connection.commit() raising java.nio.channels.ClosedChannelException GATLING Rest API 测试 - java.nio.channels.ClosedChannelException: Z3099A62586648C1 - GATLING Rest API testing - java.nio.channels.ClosedChannelException: null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM