简体   繁体   中英

Android tcp socket set etimedout value

I have tcp socket client on android. Also I have tcp socket server on nodejs, which sends to connected client test message every 2 seconds. When I launch application, it receives test messages from server. But when I switch off wi-fi connection, my client socket closes and throws SocketException (etimedout), and if I switch on wi-fi I cannot receive server messages. But server messages are queued in server already and waits to send to client.

As I know, if tcp client losts connection, it should try to repair connection and receive server messages. But in my case, client cannot receive messages, because socket closed.

With windows and linux socket client on my pc, I'm doing same things, but when I switch off wi-fi, client socket not closed, and when I switch wi-fi on, I received messages, which has sent, when wi-fi disabled.

Question: Why in android, socket closes in no time? Can I set any timeout for socket client to close?

I tried :

 setKeepAlive(true); setSoTimeout(10000);

Tried to use SocketFactory.getDefault().createSocket();

No effect.

What is your SoLinger set to?

Try using

setSoLinger(boolean on, int timeout)

to see if it helps your situation.

But when I switch off wi-fi connection, my client socket closes and throws SocketException

Your socket doesn't close. There are problems with socket inputstream when WiFi switches off. It returns -1, but socket is alive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM