Thread: Using Visual Studio MyTcpListener as an HTTP Server. Cannot get images to post

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    5

    Using Visual Studio MyTcpListener as an HTTP Server. Cannot get images to post

    Hello everyone, I am new to all of this. I have a MyTcpListener program that I am trying to make a webpage appear locally on my PC. my HTML is as follows:
    Code:
    <html>
    
    <head>
    
    <title align=center>Join The Darkside</title>
    
    <font color=red>
    </head>
    
    <body background="disney2.jpg">
    
    
    <h1 align=center> Join The Darkside.</h1>
    
    <h2>A site about the Disney acquisition of Star Wars.</h2>
    
    <h3 align=center>J.J. Abrams, if You screw up Star Wars we will ban Disney!! Seriously, we will want the head of Mickey Mouse and drink beer from his skull!!</h3>
    
    <p>So, lets just face it. Since the 2013 signing of the LucasFilms/LucasArts Empire, (pun intended), there has been a lot of speculation about what is in store for Star Wars Episodes 7-9. J,J, Abrams has been chosen by Disney to direct the new sequels that will span over the years to come.
        perhaps this has been in the making for many years. Lets look back on the past events of Lucasfilms. Pixar; created at the Skywalker Ranch and sold to Disney just in time for Toy Story to be created as well as Finding Nemo which was another big Pixar Hit. Lucas used this money to create The Phantom Menace not long after.
        So, now that Lucas finished his Prequels and made a ........ ton of money, He now sells the entire franchise to Walt Disney. I guess we saw that one coming a long time before. .</p>
    
    <p align=center>So, what do we know. Well for starters, Shooting for the movie has begun in Abu Dahbi. However the cast has yet to be announced. J.J. Abrams has confirmed that George Lucas will be in close proximity with the making of the movie and will give his approval of the script and cast.
        It is stated that Abrams will be starting fresh with a whole new story taking the story to a whole different yet new exciting level. Nevertheless, I am excited that I will be able to take my youngest boys to see the whole new breed of Star Wars. Just as I took my now 18 year old to see the prequels and my mother taking me to see the originals.</p>
    
    
    
    
    
    <p>What is Star Wars coming to? I hope this is not the future of the greatest franchise in existence: <img src=maul.jpg alt="maul"></p>
    
    <p>Star Wars has taken the Theme Parks like GangBusters. They have Star Wars Weekends now that feature stormtroopers all over the parks. Star Wars Dance shows featuring Darth Vader dancing to MC Hammer. (It is a sight to see, click below for a brief video.)
        </p>
    
    <hr>
    
    <h2>So what can we expect from the new improved Disney Star Wars!</h2>
    <ol>
      <li>The same movie storyline opening as all the other movies. (George would never have it any other way!)
      <li>The Empire taking over part of Disney World and Disneyland and a great Star War experience for theme park patrons. (That has already happened.)
      <li>No uses of expanded universe books to appear in the new movies. (Announcement made by J.J. Abrams.)
      <li>Peter Mayhew as Chewbacca. (The only entry on IMBD that is not <i>rumoured</i>.)
      <li>J.J. Abrams to screw something up and disappoint all Star Wars Fans in the world.
    </ol>
    
    <h2>Things some would like to see but sadly wont. In other words, "These aren't the droids your looking for!"</h2>
    <ol>
      <li>Captain Kirk making a guest appearance. (Ummmm, even I don't care to see that!)
      <li>Darth Maul coming back to life as Tigger.
      <li>Jar Jar Binks becoming a new bad ass sith lord as Goofy. (Hey they do look similar.)
      <li>Better Movies than the prequels, and as good as the originals. (The first one maybe, but never as good as the originals.)
      <li>Mickey Mouse becomes The New Emperor. and uses the Epcot Spaceship Earth as the new Death Star.
    </ol>
    
    <h2>The Rumours:</h2>
    <ul>
      <li>It will take place 30 years after the events of Return Of The Jedi, and the Jedi Order will be restored and Luke Skywalker will be the grand master Jedi.
      <li>Palpatine (or the ghost of Palpatine) will somehow rise from the depths of nowhere and return with a new Sith apprentice.
      <li>Mark Hammill, Carrie Fisher and Harrison Ford will revise their roles in cameo appearances. (HUH? An intergalactic Nursing Home?) Latest news says that Harrison will have a huge role. But he is not talking.
    </ul>
    <p><a href="https://www.youtube.com/watch?v=7T0vs9gYydo"</a></p>
    
    <p>Harrison wont talk about Star Wars! <a href=></a>"https://www.youtube.com/watch?v=7T0vs9gYydo"</p>
    <hr>
    
    <h2>What have we learned from Star Wars?</h2>
    <ul>
      <li>Valuable Lessons of survival:
        <ol>
          <li>Traveling through Hyperspace aint like dusting crops, Without precise calculations, you can fly right through a star or bounce too close to a supernova.
          <li>Always "Let the Wookie win."
          <li>Taun Taun carcasses are definitely good frost bite protection in a pinch. It may smell bad, but will keep you warm till your partner can get the shelter built.
        </ol>
      <li>Things We often sit and wonder, when there is nothing whatsoever else to think about:
        <ol>
          <li>Was it in the original plan to make Leia Lukes sister? I mean that kiss sure seemed as if it was a last minute script change. Or maybe the kiss was really a red herring.
          <li>is bulls-eyeing wamprats in a T16 really not impossible? (But what is a T16?)
          <li>
        </ol>
    </ul>
    <p>What is thy bidding My Micky?</p>
    
    <img src=mickey.jpg alt=
    <p>LOL, could it be? The Disney icon becoming the new sith master? </p>
    
    
    <p><a href="https://www.youtube.com/watch?v=Elkw8sLac2ULinks"</a></p>
    
    <p>Here is a fun Disney Star Wars Link <a href=></a>"https://www.youtube.com/watch?v=Elkw8sLac2ULinks"</p>
    
    <p>Proof that Lucas has been involved very closely with Disney for a very long time. Even way before Pixar. So I guess it is of no surprise that George sold out to the mouse.  Well Hey, let us look at the Epcot Starship Earth.... Paint it a darker grey and make a huge dent and you have The Death Star..</p>
    
    </body>
    
    <p>This picture was taken back in 1977 when the first Star Wars was released. Foreshadowing perhaps? <img src=vadermick.jpg alt="vadermick"></p>
    
    
    
    <p align=center> This page sponsored in part by:<img src=sponsor.jpg alt="vadermick"> Well, not really, but I did have a glass of Milk when I started the page...</p>
    
    
    </html>
    My Visual Studio code is as follows:
    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;
        }
    }
    When I put the IP Address into my firefox after building and running the code, the text from the HTML appears but I cannot get my images to work. Can anyone help? All of the jpg's are in my bin folder where the rest of everything is.

    Thanks in Advance!!

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    The biggest problem I see is that you posted a C# program in the C++ forum. Maybe you're not aware, but they are two totally separate, and very different languages.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    5
    Thanks, I just reposted this in the C# Forums.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-28-2012, 11:14 PM
  2. MAKE NoN-GUI SERVER APPLICATION ON VISUAL STUDIO
    By Nabeel Anjum in forum Windows Programming
    Replies: 3
    Last Post: 10-25-2012, 06:35 AM
  3. Replies: 2
    Last Post: 03-18-2006, 08:20 PM
  4. Client server programming in MS Visual Studio 2003
    By Bharat_kumarr in forum Networking/Device Communication
    Replies: 1
    Last Post: 09-04-2004, 10:28 AM
  5. Visual Studio and FTP Server...
    By Grayson_Peddie in forum Networking/Device Communication
    Replies: 0
    Last Post: 09-03-2003, 12:31 PM

Tags for this Thread