473,669 Members | 2,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 1144

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

Similar topics

6
14601
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: CREATE TABLE . ( IDENTITY (1, 1) NOT NULL , (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
5
2467
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 have to be open for this to work? Any help, hints or advice is appreciated :-) Kindest Regards.
3
1219
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() meinLabel.text = "Hallo ASP.net Server funktioniert" end sub
2
2955
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 some configuration problem in my IIS setup? Thanks,
1
1482
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 (and doesn't time out either) and I have to restart the program. It works fine on a winxp machine. ********************************************************** Sub Download_WebPage() Dim strPost As String Dim Lines As String Dim DownloadOk As...
2
4898
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 running and use edit-and-continue to modify the sourcecode and then immediately execute the modifications. When running within the web site, the source-code is all read-only when I break into it. The little padlock icon appears in the tab for each...
0
1199
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 javascript. Now if Web server is down then Pag1.asp progress bar is running continuously and have no idea whats going on whethere there is logic error or error due to web server down. How to handle this smartly and propogate an error to...
5
4516
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 phar://go-pear.phar/PEAR/Start/CLI.php mmap cache can't open phar://go-pear.phar/PEAR/Start.php There are 20 of these lines. After continuing through the script, it dies with this error:
4
5080
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 to be centered. However, after I center it, the html images move when the browser is resized. My text is sticking to it's coordinates. How do I get the images to stay where they are, as well. Any help is much appreciated. Thank you,
17
4072
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 this regard. the javscript for rotating images is : mg src="'+mImages.src+'" onmouseover="this.src=\''+mImagesO.src+'\'" onmouseout="this.src=\''+mImages.src+'\'" border=0 id="img'+this.a3+'" style="cursor:pointer I want to replace...
0
8466
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8896
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8810
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8590
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8659
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6211
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5683
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4387
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2798
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.