Skip to content

Commit

Permalink
Merge pull request #4805 from kwvanderlinde/bugfix/774-socket-read-ti…
Browse files Browse the repository at this point in the history
…meout

Add read timeouts to server sockets
  • Loading branch information
cwisniew authored May 30, 2024
2 parents 08fad4f + 2da5770 commit e9a5a85
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.io.*;
import java.net.Socket;
import java.net.SocketTimeoutException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -161,6 +162,9 @@ public void run() {
try {
byte[] message = SocketConnection.this.readMessage(in);
SocketConnection.this.dispatchCompressedMessage(message);
} catch (SocketTimeoutException e) {
log.warn("Lost client {}", SocketConnection.this.getId(), e);
return;
} catch (IOException e) {
log.error(e);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.TimeUnit;
import net.rptools.clientserver.simple.connection.SocketConnection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -103,6 +104,9 @@ public void run() {
while (!stopRequested) {
try {
Socket s = socket.accept();
// Client heartbeat frequency is 20 seconds, so a minute should permit two or three
// heartbeats to come in if still connected.
s.setSoTimeout((int) TimeUnit.MILLISECONDS.convert(1, TimeUnit.MINUTES));
log.debug("Client connecting ...");

String id = nextClientId(s);
Expand Down

0 comments on commit e9a5a85

Please sign in to comment.