How to share an address using StreamSocket?

2019-08-25 03:33发布

问题:

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.

回答1:

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);