Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6038879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:17:12+00:00 2026-05-23T06:17:12+00:00

I am writing a super tiny web server for educational purposes. For the following

  • 0

I am writing a super tiny web server for educational purposes.

For the following code, if I request a html page containing an image, I cannot see the image in the browser. What am I doing wrong?

static void Main(string[] args)
{
    TcpListener listener = new TcpListener(9999);
    listener.Start();
    while (true)
    { 
        TcpClient client = listener.AcceptTcpClient();
        string request = GetRequest(client.GetStream(),
            client.ReceiveBufferSize);
        WriteOutput(request, client.GetStream());
        client.Close();
    }
}

static void WriteOutput(string request, NetworkStream output)
{
    try
    {
        string[] reqs = request.Split(' ');
        WriteOutputHelper(output, reqs[1].Substring(1));
    }
    catch (Exception)
    {
        WriteOutputHelper(output, "404.html");
    }
}

private static void WriteOutputHelper(NetworkStream output, string file)
{
    byte[] statusLine = (new System.Text.ASCIIEncoding()).
        GetBytes(GetStatusLine(file) + "\r\n\r\n");
    output.Write(statusLine, 0, statusLine.Length);
    byte[] ContentType = 
        (new System.Text.ASCIIEncoding()).GetBytes(GetContentType(file) + 
            "\r\n\r\n");
    output.Write(ContentType, 0, ContentType.Length);
    byte[] response = System.IO.File.ReadAllBytes("C:\\" + file);
    output.Write(response, 0, response.Length);
    output.Flush();
}

static string GetContentType(string fileName)
{  
    string i = "<META http-equiv=\"Content-Type\" content=\"";
    if ((fileName.IndexOf(".htm") > -1) || (fileName.IndexOf(".html") > -1))
        i = i + "text/html";
    else if (fileName.IndexOf(".jpg") > -1)
        i = i + "image/jpeg";
    i = i + ";\">";
    return i;
}

static string GetStatusLine(string fileName)
{
    string i = "HTTP/1.0 ";
    if (fileName.IndexOf("404") > -1)
        return i + "404 Not Found";
    else if (fileName.IndexOf("jpg") > -1)
        return i + "302 Found";
    return i + "200 OK";
}

static string GetRequest(NetworkStream reqStream,int bufSize)
{
    byte[] bytesFrom = new byte[10025];
    reqStream.Read(bytesFrom, 0, bufSize);
    string request = System.Text.Encoding.ASCII.GetString(bytesFrom);
    return request;
}

Edited:

    static void imageTest(NetworkStream output)
    {
        byte[] fileContent = System.IO.File.ReadAllBytes("C:\\sachin.jpg");
        string statusLine = "HTTP/1.0 200 OK" + System.Environment.NewLine;
        string contentType = "Content-type: image/jpeg" + System.Environment.NewLine;
        string contentLength = "Content-length: " + fileContent.Length + System.Environment.NewLine;
        System.Text.UnicodeEncoding coding = new UnicodeEncoding();
        byte[] headers = coding.GetBytes(statusLine + contentType + contentLength);
        output.Write(headers, 0, headers.Length);
        output.Write(fileContent, 0, fileContent.Length);
        output.Flush();
    }

For the code above, I am getting this error in fiddler.

The Server did not return properly formatted HTTP Headers. HTTP headers
should be terminated with CRLFCRLF. These were terminated with LFLF.

I am using Unicode encoding because I want to convert string to bytes and I only know to use encoding.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:17:12+00:00Added an answer on May 23, 2026 at 6:17 am

    The response for a JPG must be just the HTTP header and then the contents of the JPEG, not any HTML around it.

    Something like

    HTTP/1.0 200 OK
    Content-type: image/jpeg
    Content-length: XXXXX
    
    RAWJPEGDATA
    

    Fill in XXXXX with the number of bytes in the Jpeg, and just output the raw JPEG data directly, without any encoding.

    Use Fiddler or Firebug to help debug — they show the exact requests/responses being sent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing the code for the user authentication portion of a web site (including account
Writing documentation in html requires some code examples. What to do with characters that
pre tags are super-useful for code blocks in HTML and for debugging output while
I am writing a simple code, as below,,,, - (void)viewDidLoad { [super viewDidLoad]; restaurant_name
Writing a script to render arbitrary HTML as an image, and using PyQt. I'm
Writing a JSP page, what exactly does the <c:out> do? I've noticed that the
i'm writing a program which do something like that: - (void)viewDidLoad { [super viewDidLoad];
I need a super light weight persistence layer for a java app I'm writing.
I started writing a super-simple text adventure game a few months ago, and I
I'm writing a Login page in wicket by java and want to write it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.