Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

ITSTEIN

java.net.SocketException : Connection reset 에러 해결기 본문

JAVA

java.net.SocketException : Connection reset 에러 해결기

the awesomest 2018. 3. 23. 10:48

spring mvc 웹서버에서 외부서버로 https 통신을 하여서, 데이터를 주고 받아야 할 일이 생겼다. 

개발 완료후, 로컬 테스트 , TB테스트를 하고 상용 서버 테스트를 진행하던중 Connection reset에러가 발생하였다.


에러로그를 확인하였지만, 정확한 원인 파악아 안되어서 골머리를 앓던중 스택오버플로우에서 글을 발견하였다. 

몇년전 나와 같은 문제로 고민하던 사람이 질문을 했는데. 수만은 해결방법과 원인에 대한 논쟁이 오가고 있었다. 

connection reset이 되는 이유는 여러가지가 있는듯하다. 서버 부하로 인한 connection timeout, 일시적인 네트워크 속도 문제, Client에서 연결을 끊는경우, 서버에 설정된 maxHttpHeaderBody옵션, 메모리 이슈...등...


정확한 원인 파악을 위해서는 에러로그에 대한 완벽한 분석이 되야 하는듯하다. 


해결에 실마리를 주는 답변이 2가지였다. 

===================================================================================

This error happens on your side and NOT the other side. If the other side reset the connection, then the exception message should say:

java.net.SocketException reset by peer

The cause is the connection inside HttpClient is stale. Check stale connection for SSL does not fix this error. Solution: dump your client and recreate.

===================================================================================

단순 Connection reset이슈는 서버쪽이 아니라, 연결을 시도하는 우리쪽 이슈라는것. 상대방에서 연결을 끊는경우 java.net.SocketException reset by peer  에러가 발생한다.


===================================================================================

I did also stumble upon this error. In my case the problem was I was using JRE6, with support for TLS1.0. The server only supported TLS1.2, so this error was thrown.

  • Very difficult to believe. You should have got an SSLHandshakeException. – EJP Sep 14 '16 at 12:27
  • 2
    @EJP: My answer was written a long time ago, so I can't check out the issue again. I only remember stumbling to this error + updaten to TLS 1.2 fixed the problem. After a short googeling, I see others got the java.net.SocketException as well, when using TLS1.0 in combination of a server supporting TLS1.2 at mimimum. For example assuresign.tenderapp.com/kb/how-to/…. – Jacob van Lingen Sep 15 '16 at 8:31
  • 1
    Yes, this can happen with Java 7 too. The default TLS implementation is TLSv1, which is sent in a ClientHello; if the server doesn't support it, you'll get a Connection reset, not a SSLHandshakeException. – Dan Gravell Jan 5 at 15:28

===================================================================================

자바 http 통신의 ssl 설정중, TLS버전 문제라는 것이였다. 

발생한 에러로그를 보면, sun.security.ssl에 관한 내용이 있다. 

기존에 적용된 ssl 설정 문제가 없이 다른쪽 https 통신에서는 잘 사용하던 소스라서 ssl 관련이슈는 염두에 두지 않았었다. 

HttpClient의 sslcontext설정에서 TLS -> TLSv1.2로 변경후, 테스트 하니 문제가 해결되었다. 


=================== 에러 로그 ===================

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://xxx.test.com/insert.json":Connection reset; nested exception is java.net.SocketException: Connection reset

at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:580) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]

at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:530) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]

Caused by: java.net.SocketException: Connection reset

at java.net.SocketInputStream.read(SocketInputStream.java:196) ~[na:1.7.0_45]

at java.net.SocketInputStream.read(SocketInputStream.java:122) ~[na:1.7.0_45]

at sun.security.ssl.InputRecord.readFully(InputRecord.java:442) ~[na:1.7.0_45]

at sun.security.ssl.InputRecord.read(InputRecord.java:480) ~[na:1.7.0_45]

at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927) ~[na:1.7.0_45]

at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312) ~[na:1.7.0_45]

at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339) ~[na:1.7.0_45]

at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323) ~[na:1.7.0_45]

at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:394) ~[httpclient-4.4.1.jar:4.4.1]

at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:353) ~[httpclient-4.4.1.jar:4.4.1]




https://stackoverflow.com/questions/585599/whats-causing-my-java-net-socketexception-connection-reset

https://stackoverflow.com/questions/62929/java-net-socketexception-connection-reset/31741436#31741436


'JAVA' 카테고리의 다른 글

javax.net.ssl.SSLHandshakeException 해결기  (1) 2018.03.23
spring과 mybatis 연동  (0) 2018.03.15
java equals(), hashCode() 분석  (0) 2018.03.14
JDBC, DBCP, JNDI는? DBCP vs JNDI  (0) 2017.07.11
Comments