Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Socket.Accept() does not ork


  • Please log in to reply
3 replies to this topic

#1 ashishmgupta

ashishmgupta

    New Member

  • Members
  • Pip
  • 3 posts

Posted 29 September 2012 - 07:44 PM

In the below code (Taken from "Getting started with Netduino"), I do get my IP address and my ethernet cable is connected to the router and the Netduino Plus board. However, when I try to access the http://<IpAddress>/ON, I don't see LED on and I see "Internet explorer can not display the webpage". The debugger does not break as well from

Socket clientSocket = listnerSocket.Accept();

. Any help would be much appreciated.

namespace Serve_web_page
{
    public class Program
    {
        public static void Main()
        {

            OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
            int port = 80;
            Thread.Sleep(5000);
            NetworkInterface networkInterface = NetworkInterface.GetAllNetworkInterfaces()[0];
            Debug.Print("My IP address :- " + networkInterface.IPAddress);

            Socket listnerSocket = new Socket
                (AddressFamily.InterNetwork,
                SocketType.Stream,
                ProtocolType.Tcp);

            IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, port);
            listnerSocket.Bind(endpoint);
            listnerSocket.Listen(1);

            while (true)
            {
                Socket clientSocket = listnerSocket.Accept();
                bool dataReady = clientSocket.Poll(5000000, SelectMode.SelectRead);
                if (dataReady && clientSocket.Available > 0)
                {
                    byte[] buffer = new byte[clientSocket.Available];
                    int byteRead = clientSocket.Receive(buffer);
                    string request = new String(System.Text.Encoding.UTF8.GetChars(buffer));
                    if (request.IndexOf("ON") >= 0)
                    {
                        led.Write(true);
                    }
                    else if (request.IndexOf("OFF") >= 0)
                    {
                        led.Write(false);
                    }

                    string statusText = "LED is "+ (led.Read() ? "ON" : "OFF" + ".");

                    string response =  "HTTP/1.1 200 OK\r\n"+
                        "Content-Type: text/html; charset=utf-8\r\n\r\n"+
                        "<html><head><title>NetDuino Plud LED Sample</title></head>"+
                        "<body>" + statusText + "</body></html>";
                    clientSocket.Send(System.Text.Encoding.UTF8.GetBytes(response));
                }

                clientSocket.Close();
                
            }

        }

    }
}


#2 laurentiss

laurentiss

    New Member

  • Members
  • Pip
  • 8 posts
  • LocationGrenoble France

Posted 01 October 2012 - 09:50 AM

I have a server that usually works fine.
Except today, I would like to change the Netduino IP Address.
Do you know how to do that ?
The server needs to reply to as simple ping before anything.

Regards


In the below code (Taken from "Getting started with Netduino"), I do get my IP address and my ethernet cable is connected to the router and the Netduino Plus board. However, when I try to access the http://<IpAddress>/ON, I don't see LED on and I see "Internet explorer can not display the webpage". The debugger does not break as well from . Any help would be much appreciated.

}
}
[/code]



#3 carb

carb

    Advanced Member

  • Members
  • PipPipPip
  • 352 posts
  • LocationCrystal River, Florida

Posted 01 October 2012 - 09:59 AM

I have a server that usually works fine.
Except today, I would like to change the Netduino IP Address.
Do you know how to do that ?
The server needs to reply to as simple ping before anything.

Regards

Laurentiss,

Mfdeploy.exe configuration section allows for setting the a fixed IP address.

Chuck

#4 laurentiss

laurentiss

    New Member

  • Members
  • Pip
  • 8 posts
  • LocationGrenoble France

Posted 01 October 2012 - 11:19 AM

:rolleyes: Works fine whith IP addres now fixed. This Netduino is just great! NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].EnableStaticIP("20.20.249.8","255.255.0.0","0.0.0.0"); NetworkInformation.NetworkInterface networkInterface0 = NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0]; string MyIpAdress = networkInterface0.IPAddress.ToString(); listenerSocket.Bind(new IPEndPoint(IPAddress.Any, port)); Debug.Print("Server version 4 port " + port.ToString() + " \r\n " + MyIpAdress); listenerSocket.Listen(10);




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.