Search code examples
c#windows-runtimewindows-phonewindows-store-appswindows-phone-8.1

How to use StreamSocket?


I want to know how to use StreamSocket in Windows Runtime, I searched but couldn't find a good way.

I need a point, where should I start from? thanks


Solution

  • Create a StreamSocketListener and implement a HTTP server. You know, browser will request something like:

    GET / HTTP/1.1
    Host: yourPhone
    Connection: Keep-Alive
    

    You can return a list of links.

    HTTP/1.1 OK 200
    Content-Lnegth: <number of bytes>
    Content-Type: text/html
    Connection: Keep-Alive
    
    <html>
    <body>
        <a href="/file1">file 1</a>
    ...
    

    Notice that you can only receive connections and reply when the app is in the foreground. When the app is suspended, connections are frozen. To accomplish this, you may ask the user to stick around and show a chipmunk dancing.