Tag Archives: java.net.SocketException: socket closed

Solution of JMeter pressure test “Java. Net. Socketexception: socket closed”

Open source software supply chain lighting plan, waiting for you>>>

error reporting details:

java.net.SocketException: Socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:161)
at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:82)
at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:278)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:138)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:286)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:257)
at org.apache.jmeter.protocol.http.sampler.hc.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:199)
at org.apache.jmeter.protocol.http.sampler.MeasuringConnectionManager$MeasuredConnection.receiveResponseHeader(MeasuringConnectionManager.java:212)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:684)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:486)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:835)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.executeRequest(HTTPHC4Impl.java:697)
at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sample(HTTPHC4Impl.java:455)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:74)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1189)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1178)
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:490)
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:416)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:250)
at java.lang.Thread.run(Thread.java:748)

Java.net.socketexception: socket closed error is usually caused by not setting the connection timeout

Solution:

This problem can be solved by the following methods

If use keepalive is checked in the basic of HTTP request sampler, it is recommended to use the Advanced tab:

1. Implementation is httpclient4

2. Connect in timeouts sets a value of 10-60 seconds, which indicates the idle timeout of the connection, so as to avoid the disconnection of the connection caused by the keep alive header that does not receive the response from the tested end

Reference:

https://help.aliyun.com/knowledge_ detail/112842.html

https://cwiki.apache.org/confluence/display/jmeter/JMeterSocketClosed?spm=a2c4g.11186623.2.16.41ff41eaJzLjlR

java.net.SocketException: socket closed

Open source software supply chain lighting plan, waiting for you>>>

When using socket programming, it is found that when the getinputstream method is used, the OutputStream will be returned

java.net.SocketE xception:socketclosed Wrong

See the Java API

Java.net.socket.getinputstream method:

getInputStream

public InputStream getInputStream()throws IOException

Returns the input stream for this socket

If the socket has an associated channel, the resulting input stream delegates all its operations to the channel. If the channel is in non blocking mode, the read operation of the input stream will throw an illegalblockingmodeexception

Under abnormal conditions, the underlying connection may be interrupted by the remote host or network software (for example, connection reset in the case of TCP connection). When the network software detects an interrupted connection, the following operations will be applied to the returned input stream:

Network software may discard the bytes buffered by the socket. The bytes not discarded by network software can be read by read

If no bytes are buffered on the socket, or read has consumed all the buffered bytes, all subsequent calls to read will throw an IOException

If no bytes are buffered on the socket and the socket is not closed with close, available returns 0

Closing the returned InputStream will close the associated socket

Return:

The input stream that reads bytes from this socket

Throw out:

IOException – if an I/O error occurs while creating an input stream, if the socket is not closed, if the socket is not connected, or if the socket input is closed by using shutdown input()

But

I found that InputStream was not shut down

Finally, it was found that the wrapper object of InputStream was closed, so the conclusion is that you should not try to close any stream when you want to transfer data

More articles: http://blog.gavinzh.com