DEV Community

Bo Sun
Bo Sun

Posted on

Local-only file sharing on Android: NSD, an embedded HTTP server, and TLS 1.3

Sometimes the simplest way to move a photo or a file to the person sitting next to you is surprisingly complicated. A chat app uploads it to a server on another continent, then downloads it back. Email has size limits. And if there is no signal at all, most of these just stop working.

I wanted something boring and reliable: pick a file on one phone, hand it to a nearby phone over the Wi-Fi that is already in the room, and never let the bytes leave the local network. That turned into SendHop. This is a tour of how the pieces fit, and the parts that were harder than they looked.

The constraints I designed around

  • Files only move over a local IP network: the same Wi-Fi, or a phone hotspot when there is no router.
  • The receiver should not be forced to install anything.
  • Nothing gets uploaded to a cloud, and there is no third-party script in the receiving page.
  • A batch can hold up to 20 items and 4 GiB, so large video files have to work, including interrupted downloads.

1. Finding each other on the network

Before any bytes move, the phones have to find each other. I used Android's NsdManager, the platform wrapper around DNS-SD/mDNS. A device advertises a service of type _sendhop._tcp., and the TXT records carry the small amount of metadata a peer needs before connecting:

  • a protocol version,
  • a capabilities flag,
  • a short session prefix,
  • an ephemeral presence id.

The important habit here is that seeing a service advertised is not the same as having a usable peer. A phone can sleep, change networks, or leave between the mDNS announcement and your connection. So reachability is treated as a verified, short-lived "presence" fact rather than a cached "this device exists" entry. History never proves a device is online right now.

2. Two transports, depending on who is receiving

There are two quite different receivers, and they use different transports.

Native app to native app: TLS 1.3 with pinned identity

When both sides run the app, the connection is TLS 1.3, and the certificate is not blindly trusted from a public CA. Each contact binds a long-term public key, and connections are validated against pinned identity/certificate fingerprints. That gives you authentication — you are talking to the contact you think — and encryption on top of a network that might be a coffee-shop hotspot.

Control messages and the receive/invite codes use a single CBOR/COSE format rather than ad-hoc JSON: one signed envelope, explicit versions, and unknown formats are rejected instead of best-effort parsed.

A receiver with just a browser: a tiny HTTP server on the phone

Forcing the other person to install an app to receive one file is a bad trade. So the sender can also spin up a short-lived guest web host. Under the hood it is a plain java.net.ServerSocket bound to the local address, serving a small page and the selected files over ordinary HTTP. The guest opens a link, or scans a QR code, in any browser, confirms, and downloads. There is no install, no account, and no third-party script.

The host is deliberately short-lived and token-gated: a guest token has an expiry, and the server only answers for the current session rather than exposing the whole filesystem.

3. The hard part: big files and broken connections

A 4 GiB transfer over a phone hotspot will, in real life, get interrupted. Browsers already know how to resume, so instead of inventing a resume protocol I implemented standard HTTP Range requests on the guest host.

The server keeps track of which byte ranges have actually been delivered for each item and merges them as chunks arrive, so a paused or reconnected download can ask for the missing slice with a Range header and continue instead of starting over. The bookkeeping that merges overlapping and adjacent ranges turned out to be fiddlier than the socket itself — off-by-one errors at chunk boundaries are easy, and the merge logic has to cap how many ranges it will track.

4. Keeping the data on-device

Because the product promise is "nothing leaves the local network," the boundary matters as much as the features:

  • File content, identity, the QR codes, and tokens never go into analytics or ad SDK payloads.
  • The guest web page has no third-party scripts at all.
  • Selected items are held as references in memory for the active transfer; the send flow does not persist a long-lived index of the user's files.

How it is structured

The Android app is a multi-module Gradle project in Kotlin with Jetpack Compose and a version catalog, split into thin app_cn and app_global shells over shared modules. The networking lives behind clear seams: lib_discovery for presence, lib_channel_wifi for NSD, the guest web host and the native transport, lib_security and lib_protocol for the CBOR/COSE envelopes and identity, and lib_transport for the transfer state machines and frame streams. Keeping the Wi-Fi channel, the protocol, and the transfer state machine separate is what let me add the browser receiver without tangling it into the native TLS path.

Try it

SendHop is live on Google Play. The landing page with the store links is here:

https://link.woolmate.cn/apps/sendhop?region=global&clientPlatform=android&locale=en-US&ref=V5IdNqxh7UmccsD6A8a20nND2ePR3H4k

The quickest way to test it is to connect two phones to the same Wi-Fi — or have one open a hotspot — and send a photo from the app to the other phone's browser.


A note on AI: I used an AI coding agent to help write parts of the code. The architecture, product decisions, and review were mine, and I am describing the implementation as it actually exists rather than as a generic "how a file-sharing app works."

Top comments (2)

Collapse
 
userxnom profile image
Abdullah Naseer •

Really solid write-up — the line that stuck with me is that presence is a verified, short-lived fact, not a cached "this device exists" entry. Most NSD examples treat discovery as a list you build once, and then everything downstream fails confusingly when the peer slept or switched networks.

The guest receiver is the right product call too. Forcing someone to install an app to receive one photo kills the whole use case, and a short-lived, token-gated server is a much better answer than a permanent one listening on the LAN.

One question on the pinned identity: since each contact binds a long-term public key, what happens when that key rotates — is there a re-binding flow in the app, or does the contact have to be removed and re-added? That's the edge I'd worry about first with pinning.

Collapse
 
yogin_parmar profile image
Yogin Parmar •

The "nothing leaves the local network" promise is the part I'd most want to check myself. Our Android app is offline too, but it's the easy case: it has no INTERNET permission at all, so anyone can run aapt2 dump permissions on the APK and check the claim in one line. Someone on here warned me that a library can pull INTERNET in through manifest merge, so we now run that check on the merged manifest.

SendHop needs network by design, so that check can't help you. How would a guest on the browser side confirm the bytes stayed on the LAN? Is it the no-third-party-scripts page plus the host's IP, or something they can verify themselves?