473,324 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

MyTcpListener code help. Cannot get images to post when running it as a web server.

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.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6.  
  7.  
  8. class MyTcpListener
  9. {
  10.     public static void Main()
  11.     {
  12.         TcpListener server = null;
  13.         try
  14.         {
  15.             int intStart = -1, intEnd = -1;
  16.             string strRequest = "", strPage = "",strFile = "", strResponse = "";
  17.  
  18.             // Set the TcpListener on port 11000.
  19.             Int32 port = 11000;
  20.             IPAddress localAddr = IPAddress.Parse("127.0.0.1");
  21.  
  22.             // TcpListener server = new TcpListener(port);
  23.             server = new TcpListener(localAddr, port);
  24.  
  25.             // Start listening for client requests.
  26.             server.Start();
  27.  
  28.             // Buffer for reading data
  29.             Byte[] bytes = new Byte[256];
  30.             String data = null;
  31.  
  32.             // Enter the listening loop.
  33.             while (true)
  34.             {
  35.                 Console.Write("Waiting for a connection... \n");
  36.  
  37.                 try
  38.                 {
  39.  
  40.  
  41.                 // Perform a blocking call to accept requests.
  42.                 // You could also user server.AcceptSocket() here.
  43.                 TcpClient client = server.AcceptTcpClient();
  44.                 Console.WriteLine("Client Connected from IP Address" + client.Client.AddressFamily.ToString());
  45.  
  46.                 data = null;
  47.  
  48.                 // Get a stream object for reading and writing
  49.                 NetworkStream stream = client.GetStream();
  50.  
  51.                 int i;
  52.  
  53.                 // Loop to receive all the data sent by the client.
  54.                 while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
  55.                 {
  56.                     // Translate data bytes to a ASCII string.
  57.                     data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
  58.                     Console.WriteLine("Received: {0}", data);
  59.  
  60.                     // Figure Out What the client wants
  61.                     intStart = data.IndexOf("GET");
  62.                     intEnd = data.IndexOf("\r\n");
  63.                     strRequest = data.Substring(intStart, intEnd);
  64.  
  65.                     intStart = data.IndexOf(" ") + 1;
  66.                     intEnd = data.IndexOf(" ", intStart);
  67.                     strRequest = strRequest.Substring(intStart, intEnd - intStart);
  68.  
  69.  
  70.                     if (strRequest == "/")
  71.                     {
  72.                         // Request is the Home page
  73.                         strPage = "index.html";
  74.                     }
  75.                     else
  76.                     {
  77.                         // Request is a specific page
  78.                         strPage = strRequest.Substring(1);
  79.                     }
  80.  
  81.                     // We have the page the browser is requesting
  82.                     Console.WriteLine("Page Requested: " + strPage);
  83.  
  84.                     //strResponse = "<html><body><h1>Hello World!</h1></body></html>";
  85.  
  86.  
  87.                     strFile = GetFile(strPage);
  88.  
  89.                     strResponse = "HTTP/1.1 200 OK\n" +
  90.                                   "Connection: close\n" +
  91.                                   "Date: " + System.DateTime.Now + "\n" +
  92.                                    "Server: Apache/2.2.3 (CentOS)\n" +
  93.                                    "Last-Modified: Tue, 09 Aug 2011 15:11:03 GMT\n" +
  94.                                    "Content-Length: " + Convert.ToString(strFile.Length) + "\n" +
  95.                                    "Content-Type: text/html\n\n" +
  96.                                    "Content-Type: image/jpg\n\n" +
  97.                                    strFile;
  98.  
  99.                     // Convert String back into BYTE array.
  100.                     byte[] msg = System.Text.Encoding.ASCII.GetBytes(strResponse);
  101.  
  102.                     // Send back a response.
  103.                     stream.Write(msg, 0, msg.Length);
  104.                     Console.WriteLine("Sent: {0}", data);
  105.                 }
  106.  
  107.                 // Shutdown and end connection
  108.                 client.Close();
  109.  
  110.  
  111.                 }
  112.                 catch
  113.                 {
  114.  
  115.                 }
  116.             }
  117.         }
  118.         catch (SocketException e)
  119.         {
  120.             Console.WriteLine("SocketException: {0}", e);
  121.         }
  122.         finally
  123.         {
  124.             // Stop listening for new clients.
  125.             server.Stop();
  126.         }
  127.  
  128.  
  129.         Console.WriteLine("\nHit enter to continue...");
  130.         Console.Read();
  131.     }
  132.  
  133.     public static string GetFile(String strFileName)
  134.     {
  135.         string strPath = "";
  136.         string strResult = "";
  137.  
  138.         if (!File.Exists(strFileName))
  139.         {
  140.             // Warning message that file does not exist
  141.             strResult = "404 - File does not exist";
  142.         }
  143.         else
  144.         {
  145.             strResult = File.ReadAllText(strFileName);
  146.         }
  147.  
  148.         return strResult;
  149.     }
  150. }
  151.  
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!!
Apr 8 '16 #1
0 1122

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: MrDom | last post by:
I am wondering if someone can help solve this question I have a table in sql server 2000, I setup it using Enterprise manager. When I generate an SQL Script for this table it scripts as: ...
5
by: milkyway | last post by:
Hello everyone, I have a few values and variables that I want to post to a server (without using a SUBMIT button). Is there a way to post data from within javascript - do sockets or connections...
3
by: ernst | last post by:
Hello, I cannot get ASP.NET running with a simple testfile called "test.aspx". It contains the following lines: <html> <head> <script language="vb" runat="server"> sub page_load()...
2
by: Bruce Russell | last post by:
This may sound stupid but I can't rename the WebForm1.aspx in the solution explorer. The file is located in my local web server at C:\Inetpub\wwwroot\Lab3-VB-Starter\WebForm1.aspx Is there...
1
by: Pål Johansen | last post by:
I made a program that does screen scraping from a website, but when running this progran on a Win 2003 server the httpwebrequest works fine for a while but suddenly never returns from the request...
2
by: Oenone | last post by:
I am developing an assembly that can be used either by a Windows Forms application or from within an ASP.NET web site. When running within the Forms app, I can break into the code while it is...
0
by: abcd | last post by:
I have Page1.asp which is posted to Page2.asp. Page2.asp runs in hidden frame. When web server is running and Page2.asp is running I can pass suceess / fail / any error messages to Page1.asp using...
5
by: Dan Fulbright | last post by:
I'm trying to install PHP 5.2.2 on Windows, but I keep getting errors when running go-pear.bat: mmap cache can't open phar://go-pear.phar/index.php mmap cache can't open...
4
by: Anna97 | last post by:
Hi, I guess I've built my site sort of strangely: The images are in the html (using tables), but the text is all in css positioned using coordinates (hovering over the images). I need my page...
17
by: handique | last post by:
Hi, I have javascript code for rotating images, but the rotation starts only when mouse is placed over the image. But i want to rotate images automatically when the page loads. Can any guide me in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.