skip to content

In a mobile live trivia-show app, the phone moves from station Wi-Fi to cellular and the socket looks open but no questions arrive; how should the client detect and recover?

level: seniorimportance: must knowfreq 50%

answer

  1. the old address went away
  2. silence, not an error
  3. act on the OS network-change signal
  4. a missed reply or a missing tick
  5. new socket first, old one ignored

basics

~20 s

The connection was bound to the Wi-Fi address, so it died silently and no error arrives for a long time. Act on the OS network-change signal, a missed heartbeat reply or a missing game tick: open a new socket and resume.

solid answer

~50 s

A TCP connection is identified by both ends' addresses, so when the phone leaves Wi-Fi its socket is still bound to an address that no longer routes. Nothing tells the app: sends sit in a local buffer and the stack may retry for minutes before it reports a failure. So the client cannot wait for an error. It watches three signals: the OS's network-change notification, which it treats as a reason to probe or reconnect at once; a heartbeat with no reply inside its deadline; and the game's own cadence, since during a round a tick or question should arrive every few seconds. On any of them it opens a new socket on the current network, resumes from the last event it applied, closes the old socket and ignores anything it still delivers. An answer written to the dead socket is not delivered just because the write succeeded.

go deeper

for a junior

Recall that a socket bound to an old network address dies silently and that the client must look for its own signs of death.

for a middle

Name the three signals, network change, missed heartbeat reply and missing game ticks, and how each turns into a reconnect.

for a senior

Handle the old socket's late events with a generation number, keep answers unacknowledged until the server confirms, and choose probe versus reconnect.

for a principal

Weigh a transport that migrates connections against the client logic it does not remove, and set the detection budget for a timed product.

## Why the socket looks open A **TCP connection** is identified by four values: the phone's address and port and the server's address and port. When a commuter's phone leaves the station's Wi-Fi and moves to cellular, it gets a different address on a different network. The existing connection is still bound to the Wi-Fi address, which no longer reaches the phone. Packets the server sends go nowhere; packets the phone writes may leave over Wi-Fi's last gasp or not at all. What matters for the design is that **no error arrives**. The socket's local state still says open. A write succeeds because it only copies bytes into the local send buffer. The networking stack keeps retransmitting unacknowledged data and may take minutes before it gives up and reports a failure. For a quiz with a ten-second answer window (this scenario's assumption), minutes of false 'connected' is several lost questions. The server is in the same position from its side. It keeps the old connection for a player who has gone until its own heartbeat deadline expires, so its count of who is present lags as well. Neither end is told; each has to find out. ## Three signals the client can act on Since the error is too late, the client builds its own evidence: 1. **The OS network-change signal.** The OS can tell an app that asks when its default network changes (Wi-Fi lost, cellular now in use). This is the earliest and cheapest signal: the client does not wait to discover the death, it reacts to the cause. 2. **A missed heartbeat reply.** The client sends a heartbeat after a quiet spell and gives the reply a deadline; no reply within it means the path is dead. 3. **The game's own cadence.** During a live round the server sends a question, a tick or a result every few seconds. If several expected messages are missing, the socket is dead even if the heartbeat timer has not fired yet. Whichever signal fires first wins; the client does not wait for a second one to confirm it. A false alarm costs one reconnect, while waiting costs answer windows. ## Probe or reconnect at once? Not every network change kills the socket. If the phone was on cellular and Wi-Fi merely appeared, the old connection on cellular may still work for a while. Two reasonable policies: | Policy | How it works | Trade-off | |---|---|---| | Reconnect on every default-network change | Open a new socket on the new network immediately | One extra reconnect when the old path would have survived | | Probe first | Send a heartbeat with a short deadline, reconnect only if it fails | Saves a reconnect, costs a few seconds when the path is dead | For a timed quiz, reconnecting immediately on a change of the default network is usually the right default: a spare reconnect costs less than a missed question. ## Recovering without mixing two sockets - **Open the new socket first**, on the network the OS now reports, and resubscribe with the last event the client applied so the server can resend what was missed. - **Close the old socket and stamp connections with a generation number**, so a late message or error from the old one is ignored instead of tearing down the new one. - **Do not count an answer as sent because the write succeeded.** An answer needs an application-level acknowledgement from the server; an unacknowledged answer is resent on the new socket with the same answer id if the server's deadline has not passed, and the server ignores the duplicate. - **Tell the player.** Show 'Reconnecting...' and disable answering until the new socket is subscribed, then show the live question with the server's remaining time. ## What a newer transport changes A transport that names connections by an id instead of by addresses, such as QUIC, can move a connection to a new network without a reconnect. That shortens recovery, but the client still needs liveness checks, because a path can die without any address change, and it still needs to resume from its last applied event when a connection is lost for good. ## Common mistakes - Waiting for the socket's error or close callback before reconnecting. - Treating a successful write as proof of delivery. - Letting a late close from the old socket trigger a second reconnect of the new one. - Reconnecting but not resubscribing from the last applied event, so the round's missed questions are lost.

  • Why is a successful write on the old socket no evidence that the answer reached the server?
    A write returns once the bytes are copied into the phone's send buffer; the networking stack delivers them later. If the path is dead, they are retransmitted into nothing until the stack gives up. Only an application-level acknowledgement from the server, carrying the answer's id, proves receipt, so unacknowledged answers are resent on the new socket.
  • A late error from the old socket arrives after the new one is live and the client reconnects again; how do you prevent that?
    Give every connection a generation number and route each event and error through it. When the client opens generation 8, everything still arriving from generation 7, including its close or error, is logged and dropped. Only the current generation may trigger reconnect logic or update the screen.
  • Would a transport that migrates connections between networks remove the need for this design?
    It would remove many reconnects after a handoff, not the design. Paths still die without an address change, servers restart, and migration can fail, so the client still needs heartbeat deadlines, the network-change reaction as a trigger to check, and resuming from its last applied event.

A phone call where the line went dead without a click: nothing on the line tells you, so you either notice you just stepped into a lift or ask 'are you still there?' and hear nothing.

  • A dead socket always reports an error or close event promptly.
  • If the write call succeeds, the server has received the answer.
  • The old connection keeps working after the phone changes networks.
  • Every network change means the current socket is certainly dead.
  • Waiting for the socket's own error is enough to start reconnecting.