using System;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
namespace web_server
{
public class Program
{
public static void Main()
{
if (Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces().Length < 1) // Call the network interface on the board
{
Debug.Print("No Active network interfaces. Bombing out.");
Thread.CurrentThread.Abort();
}
Microsoft.SPOT.Net.NetworkInformation.NetworkInterface NI = Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0]; // retrieve the network interface
#region Static IP code
NI.EnableStaticIP("192.168.0.25", "255.255.255.0", "192.168.0.1"); // use a static IP address
#endregion
Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Creates the Socket to send data over a TCP connection
IPAddress hostIP = IPAddress.Parse(NI.IPAddress); // Bind the listening socket to the port
IPEndPoint ep = new IPEndPoint(hostIP, 80);
listenSocket.Bind(ep);
listenSocket.Listen(1); // Start listening Socket for incoming connection attempts.
while (true)
{
try
{
Debug.Print("listening...");
Socket newSock = listenSocket.Accept();
Debug.Print("Accepted a connection from " + newSock.RemoteEndPoint.ToString());
string s = "<html>\n"; // creae the web page.
s += "<head><title>Welcome</title></head>\n";
s += "<META http-equiv=\"REFRESH\" content=\"60;URL=" + NI.IPAddress + "\">";
s += "<body>\n";
s += "<p>Hello, This is the FEZ Cobra Test Page!</p>\n";
s += "</body>";
s += "</html>";
byte[] messageBytes = Encoding.UTF8.GetBytes(s);
newSock.Send(messageBytes);
newSock.Close();
}
catch (Exception e)
{
Debug.Print(e.Message);
}
}
}
}
i’m putting the web page in to resources and now in getting the web page i created by entering the ip address but problem is that im not able to see any pictures of the web site…
any suggetions ?
do i need to encode pictures in my web page from and give the referenced path of the images from C#?
how to give referenced path and encode from C#
<p align="center"><img src="file:///path/Visual Studio 2010/Projects/ web server/ web server/Resources/New folder/h641_beng_dce.jpg" width="291" height="150"></p>
The path to the file that is coded into your html should NOT be a physical path on your hard disk.
It should be a relative url from the root of your web site.
For example:
The content type in your response should be set as follows:
contentType = "image/jpeg"
You shouldn’t need to encode the image bytes. Here is an example of how I send a file that is stored on the SD card. You can use a similar approach for sending bytes from a resource.
using (FileStream input = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
httpResponse.ContentLength64 = input.Length;
byteBuffer = new byte[512];
while (true)
{
bytesRead = input.Read(byteBuffer, 0, byteBuffer.Length);
if (bytesRead == 0) break;
httpResponse.OutputStream.Write(byteBuffer, 0, bytesRead);
}
}
You then need to store your image file in Resources as a BinaryResource so that you can retrieve it with GetBytes. Once you have an array of bytes you can pass that to Wouter’s web server and it will send them back to the client.
Is this the wite method to get image from resources.
it giives me error “No overload for method ‘Write’ takes 1 arguments” although i put correct references.i really dont understand what 's wrong there because im bad with codings
if you’re “bad with codings” then you should take someone else’s code as-is and not change it. It’s probably better that you take Wouter’s code like you were pointed to before, as it works and you can step through it and start to understand the code you’re writing.
In this case, what that message is telling you is that the WRITE method needs more parameters, as you have only supplied one parameter - your parameter is:
Use Intellisense and it will point out what the parameters of this call is, from memory you need an offset from the start of your buffer to start sending data, and then the number of bytes to send.
hi,
I tried with store web page in the sd card and receive it .it works fine but still gif images are not display… is this the correct method to show images in the web page.
@ max12
Your code never downloads the image file to the client.
Your ProcessClientRequest is hardcoded to download the html file every time.
You should modify your code to look at the request.url to determine which file the client is requesting, and then download that file.
The client browser will first request the html file, and then after it is parsed, it will determine that you have an embedded image, and will then request the image file from your web server.