Search code examples
c#windows-runtimewindows-phonewindows-store-apps

How to share an address using StreamSocket?


I'm able to pass a text, and it works like a charm!

StreamSocketListener streamSocketListener = new StreamSocketListener();
streamSocketListener.ConnectionReceived += streamSocketListener_ConnectionReceived;
await streamSocketListener.BindEndpointAsync(hostName, port);

It shows the link in the other device's browser.


Solution

  • Pseudo code:

    IStorageFile fileToSend = await KnownFolders.PicturesLibrary.GetFileAsync("foo.jpg");
    BasicProperties basicProperties = await fileToSend.GetBasicPropertiesAsync();
    IInputStream streamToSend = await fileToSend.OpenReadAsync();
    
    string headers = "HTTP/1.1 OK 200\r\n" +
        "Content-Length:" + basicProperties.Size + "\r\n" +
        "Content-Type: " + fileToSend.ContentType + "\r\n" +
        "Connection: Keep-Alive\r\n\r\n";
    
    writer.WriteString(stringToSend);
    await writer.StoreAsync();
    writer.DetachStream();
    
    await RandomAccessStream.CopyAndCloseAsync(streamToSend, socket.OutputStream);