-1

I need to create a single threaded watcher that monitors socket connections to see if they are open or were open before, and in case it is false, output a log line.

In the classes that opens the socket connection (3 different ones) I created a timer like the example bellow but that will create multiple threads which I do not want:

if (connectionToCheck()) {
    new Timer().schedule(new TimerTask() {
        @Override
        public void run() {
        if (!socket.isConnected()) {
            LOG.info("connection - {} second timeout: { host={}, port={} }",DEFAULT_TIMEOUT ,host ,port );
        }
    }
}, DEFAULT_TIMEOUT*1000);
socket.connect(new InetSocketAddress(host, port), connect_timeout);

I am not very experienced on java so I was wandering if someone could give me some pointers on how to get started on this.

Thanks in advance.

Edit:

The idea is that multiple connections will be going at the same time and they will all need to be watched under by this new class. So the first connection would create the watcher object and subsequent ones would just add new connections to be monitored).

eSp
  • 45
  • 5
  • 1
    Socket.isConnected does not what you expect! See: https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#isConnected() – Fildor Jan 20 '16 at 11:55
  • 1
    And if you think: oh great, then I will use `isClosed` - nope! That will only tell you if "this side" has closed the socket. If the other side cut the connection you won't know until the next attempt to write. – Fildor Jan 20 '16 at 11:57
  • @Fildor I think it does what I want, unless I am not reading properly. If the socked is connected or was connected at any point socket.isConnected() will return true, which is what I want. Apologies if I am interpreting incorrectly... – eSp Jan 20 '16 at 12:12
  • So you are monitoring if connections have been established, not if they broke? – Fildor Jan 20 '16 at 12:14
  • " or remain open on the last 10 seconds " at least that part cannot be detected with "isConnected". – Fildor Jan 20 '16 at 12:16
  • Yes, only want to monitor if they were established. Made a poor choice of words there, english is not my native language. Will edit the post, thanks for pointing that out – eSp Jan 20 '16 at 12:40
  • Once `isConnected()` has returned true once it will never return false, possibly until you close it. So starting a timer thread to watch that state is completely pointless. As you claim to already realize this, it is entirely unclear what you're actually asking, or trying to accomplish. – user207421 Jan 21 '16 at 00:48
  • @EJP I am trying to fine tune the ideal time to mark the connection as timed out. At the moment the timeout is set to 30sec, what I want to do is check after say 10 or 15 sec if the isConnected() returns true or false. If it returns true then it means to me that that connection would not have been lost by lowering the timeout time, if it returns false then I have to take that into account by the end of the day for statistic to see how many attempt to connections would have been lost if I were to actually reduce the timeout. – eSp Jan 21 '16 at 09:49

1 Answers1

-1

you can not rely on the output of

socket.isConnected();   

socket.isConnected() returns always true once the client connects (and even after the disconnect) weird !!

for general overview you can poll the state of each socket to know if the socket was open at all

for states of sockets see http://docs.oracle.com/javase/6/docs/api/java/net/Socket.html

to know if it is still open you can use heartbeats

If you just want to quickly detect silently dropped connections, you can use Socket.setKeepAlive( true ). This method ask TCP/IP to handle heartbeat probing without any data packets or application programming. If you want more control on the frequency of the heartbeat, however, you should implement heartbeat with application level packets.

From https://stackoverflow.com/a/10693807, see also these comments.

And from How do I check if a Socket is currently connected in Java?, if you decide to use multiple threads for monitoring you can use

socket.getInputStream().read()

makes the thread wait for input as long as the client is connected and therefore makes your program not do anything - except if you get some input returns -1 if the client disconnected

Community
  • 1
  • 1
ralf htp
  • 9,149
  • 4
  • 22
  • 34
  • I made an edit to better explain what I mean, as long as the socket is or was connected before I want it to return true so socket.isConnected() is up for the task, I may have explained myself poorly before – eSp Jan 20 '16 at 13:42
  • Polling the state of a socket to see if it is open at all is just as pointless as polling `isConnected().` Normally there wont even be a socket unless it is open. Issuing a read in another thread will lose data. At least one of your links is poor quality *and* clearly marked as a duplicate. This answer is partially plagiarized. – user207421 Jan 21 '16 at 00:54