4242import java .io .*;
4343import java .net .ServerSocket ;
4444import java .net .Socket ;
45+ import java .net .SocketTimeoutException ;
4546import java .nio .ByteBuffer ;
4647import java .security .KeyManagementException ;
4748import java .security .KeyStore ;
@@ -212,7 +213,6 @@ public void run() {
212213 while (true ) {
213214 try {
214215 Socket socket = this .serverSocket .accept ();
215- socket .setSoTimeout (binaryDataReceiverConfiguration .getSocketTimeout ());
216216 sslReceiverExecutorService .submit (new BinaryTransportReceiver (socket ));
217217 } catch (IOException e ) {
218218 log .error ("Error while accepting the connection. " , e );
@@ -233,7 +233,6 @@ public void run() {
233233 while (true ) {
234234 try {
235235 Socket socket = this .serverSocket .accept ();
236- socket .setSoTimeout (binaryDataReceiverConfiguration .getSocketTimeout ());
237236 tcpReceiverExecutorService .submit (new BinaryTransportReceiver (socket ));
238237 } catch (IOException e ) {
239238 log .error ("Error while accepting the connection. " , e );
@@ -252,6 +251,7 @@ public BinaryTransportReceiver(Socket socket) {
252251 @ Override
253252 public void run () {
254253 try {
254+ this .socket .setSoTimeout (binaryDataReceiverConfiguration .getSocketTimeout ());
255255 InputStream inputstream = new BufferedInputStream (socket .getInputStream ());
256256 OutputStream outputStream = new BufferedOutputStream ((socket .getOutputStream ()));
257257 int messageType = inputstream .read ();
@@ -261,8 +261,18 @@ public void run() {
261261 processMessage (messageType , message , outputStream );
262262 messageType = inputstream .read ();
263263 }
264+ } catch (SocketTimeoutException socketTimeoutException ) {
265+ log .error ("Socket read timed out for client" , socketTimeoutException );
264266 } catch (IOException ex ) {
265267 log .error ("Error while reading from the socket. " , ex );
268+ } finally {
269+ if (socket != null && !socket .isClosed ()) {
270+ try {
271+ socket .close ();
272+ } catch (IOException ex ) {
273+ log .error ("Error while closing socket" , ex );
274+ }
275+ }
266276 }
267277 }
268278 }
0 commit comments