Laire
Laire

Reputation: 1330

Webserver on my Raspberry Pi 3 with Win 10 IoT

I try to run a webserver on my Raspberry Pi 3 with Win 10 IoT.

It shows me the debug line "START WEBSERVER" but not the "HANDLE REQUEST" line in the output console. When I try to open the page with my browser (from another device in the same network) it doesn't load the page.

The I can access the device portal on port 8080 without problems, so it is not a problem with a firewall or something like that.

In the manifest i add Private Networks (Client & Server).

WebServer server = new WebServer();
server.Initialise();

public class WebServer
{
    private StreamSocketListener listener;
    private const uint BufferSize = 8192;

    public WebServer(){}

    public async void Initialise()
    {
        Debug.WriteLine("START WEBSERVER");
        listener = new StreamSocketListener();
        await listener.BindServiceNameAsync("80");

        listener.ConnectionReceived += (sender, args) =>
        {
            HandleRequest(sender, args);
        };
    }

    public async void HandleRequest(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
    {
        StringBuilder request = new StringBuilder();
        using (IInputStream input = args.Socket.InputStream)
        {
            byte[] data = new byte[BufferSize];
            IBuffer buffer = data.AsBuffer();
            uint dataRead = BufferSize;
            while (dataRead == BufferSize)
            {
                await input.ReadAsync(buffer, BufferSize, InputStreamOptions.Partial);
                request.Append(Encoding.UTF8.GetString(data, 0, data.Length));
                dataRead = buffer.Length;
            }
        }

        using (IOutputStream output = args.Socket.OutputStream)
        {
            using (Stream response = output.AsStreamForWrite())
            {
                byte[] bodyArray = Encoding.UTF8.GetBytes("<html><body>Hello, World!</body></html>");

                var bodyStream = new MemoryStream(bodyArray);

                var header = "HTTP/1.1 200 OK\r\n" +
                            $"Content-Length: {bodyStream.Length}\r\n" +
                                "Connection: close\r\n\r\n";

                byte[] headerArray = Encoding.UTF8.GetBytes(header);

                await response.WriteAsync(headerArray, 0, headerArray.Length);
                await bodyStream.CopyToAsync(response);
                await response.FlushAsync();
            }
        }

    }

}

Upvotes: 0

Views: 795

Answers (2)

Laire
Laire

Reputation: 1330

Adding in the manifest "Internet (Client & Server)" was the solution. Private Network doesn't works.

Upvotes: 0

Left panda
Left panda

Reputation: 148

This generally happen , because one of those causes below :

- the acces to the page is limited just for the device where you load the service .
-the lane doesnt support services between devices
-Firewall issues or the port forward .

Try to check your service or you can try to run it on an actual Opened Port in the lane . Open a Port ex (554) on your Retour Gateway and use it in your listen device .

please note that sometimes the issue can happen if you don't have a virtualization in your bios.

Upvotes: 1

Related Questions