3
When I use the function isConnected()
of java.net.Socket
, on the client’s side, and she returns to me true
can I make sure the server is "listening"? How else can I know? Only with a waiting time between connecting and starting "writing"?
PS: I put the tag Android because I do not know if it has influence.
I cannot say, but perhaps this is related https://answall.com/a/45393/3635
– Guilherme Nascimento
@Guilhermenascimento I just want to know if the server is reading what I write in the socket before I start writing.
– Jorge B.
So, just using the Socket API simply doesn’t have a way. The client may have disconnected and not notified the Socket, and the server doesn’t know. One technique I usually use is Heart Beat, which is to send specific opaque packets, just to see if it arrives. If the answer doesn’t come, we can assume the other side of Socket is dead*.
– Wakim
Yes, so I quoted the answer, maybe I can’t be sure and so the answer suggests things like a "timer", but when it comes to "persistent communication", maybe I have a way of knowing, of course I would say that the customer would need to keep sending a signal "constant".
– Guilherme Nascimento
@Wakim is the other way around. I want to know if the server is "reading" what I send.
– Jorge B.
But in this case if you send and fail you know that the server is not accessible. But do you want to check this first? Ai has to "ping" it to check availability.
– Wakim
@Wakim the harm is that I don’t have this feature. But thanks for the tips.
– Jorge B.
So Jorge, one possibility is to use the ICMP protocol to run a ping on the server, it would be something similar to a terminal ping, it may be the simplest way, unless the server is blocking the port :( Looking more calmly, it seems that the class
InetAddress
provides this functionality through the methodisReachable
, it performs a ping on the server (remembering that it may be blocked yet).– Wakim
Possible duplicate of It is possible to know how many clients are connected to Serversocket?
– Daniel