Hello people. I am using a MyTcpListener as a webserver locally on my PC. I am trying to get my images to show when I run the program and use my firefox to punch in the IP Address. The images are in the bin folder as well as the HTML file is. My text shows up but my images wont. Here is my code so far.
Can I get some help with what lines I need to add to the headers or whatever to get my images to show when I log into the program?
Thanks In Advance!!
Code:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
class MyTcpListener
{
public static void Main()
{
TcpListener server = null;
try
{
int intStart = -1, intEnd = -1;
string strRequest = "", strPage = "",strFile = "", strResponse = "";
// Set the TcpListener on port 11000.
Int32 port = 11000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Enter the listening loop.
while (true)
{
Console.Write("Waiting for a connection... \n");
try
{
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Client Connected from IP Address" + client.Client.AddressFamily.ToString());
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
// Figure Out What the client wants
intStart = data.IndexOf("GET");
intEnd = data.IndexOf("\r\n");
strRequest = data.Substring(intStart, intEnd);
intStart = data.IndexOf(" ") + 1;
intEnd = data.IndexOf(" ", intStart);
strRequest = strRequest.Substring(intStart, intEnd - intStart);
if (strRequest == "/")
{
// Request is the Home page
strPage = "index.html";
}
else
{
// Request is a specific page
strPage = strRequest.Substring(1);
}
// We have the page the browser is requesting
Console.WriteLine("Page Requested: " + strPage);
//strResponse = "<html><body><h1>Hello World!</h1></body></html>";
strFile = GetFile(strPage);
strResponse = "HTTP/1.1 200 OK\n" +
"Connection: close\n" +
"Date: " + System.DateTime.Now + "\n" +
"Server: Apache/2.2.3 (CentOS)\n" +
"Last-Modified: Tue, 09 Aug 2011 15:11:03 GMT\n" +
"Content-Length: " + Convert.ToString(strFile.Length) + "\n" +
"Content-Type: text/html\n\n" +
"Content-Type: image/jpg\n\n" +
strFile;
// Convert String back into BYTE array.
byte[] msg = System.Text.Encoding.ASCII.GetBytes(strResponse);
// Send back a response.
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: {0}", data);
}
// Shutdown and end connection
client.Close();
}
catch
{
}
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
public static string GetFile(String strFileName)
{
string strPath = "";
string strResult = "";
if (!File.Exists(strFileName))
{
// Warning message that file does not exist
strResult = "404 - File does not exist";
}
else
{
strResult = File.ReadAllText(strFileName);
}
return strResult;
}
}
Thanks In Advance!!