<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bo Sun</title>
    <description>The latest articles on DEV Community by Bo Sun (@woolmate).</description>
    <link>https://dev.to/woolmate</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4276662%2F65bc6433-4bac-46f6-80b2-d1241420f2fa.png</url>
      <title>DEV Community: Bo Sun</title>
      <link>https://dev.to/woolmate</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/woolmate"/>
    <language>en</language>
    <item>
      <title>Local-only file sharing on Android: NSD, an embedded HTTP server, and TLS 1.3</title>
      <dc:creator>Bo Sun</dc:creator>
      <pubDate>Tue, 01 Dec 2026 14:47:15 +0000</pubDate>
      <link>https://dev.to/woolmate/local-only-file-sharing-on-android-nsd-an-embedded-http-server-and-tls-13-417a</link>
      <guid>https://dev.to/woolmate/local-only-file-sharing-on-android-nsd-an-embedded-http-server-and-tls-13-417a</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  The constraints I designed around
&lt;/h2&gt;

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

&lt;h2&gt;
  
  
  1. Finding each other on the network
&lt;/h2&gt;

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

&lt;ul&gt;
&lt;li&gt;a protocol version,&lt;/li&gt;
&lt;li&gt;a capabilities flag,&lt;/li&gt;
&lt;li&gt;a short session prefix,&lt;/li&gt;
&lt;li&gt;an ephemeral presence id.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Two transports, depending on who is receiving
&lt;/h2&gt;

&lt;p&gt;There are two quite different receivers, and they use different transports.&lt;/p&gt;

&lt;h3&gt;
  
  
  Native app to native app: TLS 1.3 with pinned identity
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  A receiver with just a browser: a tiny HTTP server on the phone
&lt;/h3&gt;

&lt;p&gt;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 &lt;code&gt;java.net.ServerSocket&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. The hard part: big files and broken connections
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;Range&lt;/code&gt; 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Keeping the data on-device
&lt;/h2&gt;

&lt;p&gt;Because the product promise is "nothing leaves the local network," the boundary matters as much as the features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;File content, identity, the QR codes, and tokens never go into analytics or ad SDK payloads.&lt;/li&gt;
&lt;li&gt;The guest web page has no third-party scripts at all.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How it is structured
&lt;/h2&gt;

&lt;p&gt;The Android app is a multi-module Gradle project in Kotlin with Jetpack Compose and a version catalog, split into thin &lt;code&gt;app_cn&lt;/code&gt; and &lt;code&gt;app_global&lt;/code&gt; shells over shared modules. The networking lives behind clear seams: &lt;code&gt;lib_discovery&lt;/code&gt; for presence, &lt;code&gt;lib_channel_wifi&lt;/code&gt; for NSD, the guest web host and the native transport, &lt;code&gt;lib_security&lt;/code&gt; and &lt;code&gt;lib_protocol&lt;/code&gt; for the CBOR/COSE envelopes and identity, and &lt;code&gt;lib_transport&lt;/code&gt; 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;SendHop is live on Google Play. The landing page with the store links is here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://link.woolmate.cn/apps/sendhop?region=global&amp;amp;clientPlatform=android&amp;amp;locale=en-US&amp;amp;ref=V5IdNqxh7UmccsD6A8a20nND2ePR3H4k" rel="noopener noreferrer"&gt;https://link.woolmate.cn/apps/sendhop?region=global&amp;amp;clientPlatform=android&amp;amp;locale=en-US&amp;amp;ref=V5IdNqxh7UmccsD6A8a20nND2ePR3H4k&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;




&lt;p&gt;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."&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>showdev</category>
      <category>networking</category>
    </item>
  </channel>
</rss>
