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 9271949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:45:14+00:00 2026-06-18T15:45:14+00:00

I have a simple TCP serversocket that will GET a byte array. This GET

  • 0

I have a simple TCP serversocket that will GET a byte array. This GET comes from when entering a website on this server that contains an img src link to a gif image, the requests looks like this:

GET /myHome.htm HTTP/1.1
GET /house.gif HTTP/1.1

Now the byte array is done like this:

byte[] fileByte = Files.readAllBytes(filePath);

To print the website which contains this image I do this:

out.writeBytes(new String(fileByte));

out:

DataOutputStream out= new DataOutputStream(socketClient.getOutputStream());

Now to make the image display I think I have to use something else then

out.writeBytes()

but I do not know for sure. Anybody knows how to make the image display? Right now the image just dont show at all.

  • 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-06-18T15:45:15+00:00Added an answer on June 18, 2026 at 3:45 pm

    First, make sure your GIF file is not corrupted. (Happened to me before, too).

    If that is the case, try this code for sending the GIF file:

    byte[] fileByte = Files.readAllBytes(filePath);
    writer.writeBytes("HTTP/1.1 200 OK\r\n");
    writer.writeBytes("Content-Type: image/gif\r\n");
    writer.writeBytes("Content-Length: "+fileByte.length+"\r\n");
    writer.writeBytes("\r\n");
    writer.write(fileByte, 0, fileByte.length);
    

    And then try to navigate to “house.gif” directly instead of “myHome.htm”. Let me know in the comments what this does.

    Previous answer attempts:

    I think I may have misunderstood your question. Let me try with a different answer:

    You are not sure how to figure out on the server when to return the HTML file myHome.htm and when to return house.gif?

    I think for this you need to simply parse out the requested URL. Just check whether it contains “house.gif” or not. Then, depending on this, you either return the HTML file as you described above, or you send the .gif file, making sure that you use

    writer.write(fileByte, 0, fileByte.length);
    

    to send the binary data and that you set a reply header of

    Content-Type: image/gif
    

    In both cases (for the HTML file and the GIF file), though, you should prepend the data you are sending with correct HTTP response headers. Don’t take the page-title the wrong way, but this site might help: http://net.tutsplus.com/tutorials/other/http-headers-for-dummies/

    And just to make sure: Your server will be receiving TWO independent requests. The first one will ask for the HTML file, the second one will ask for the GIF file. So you send either one or the other. So, there’s no “special way” to send the GIF instead of the HTML file. You use the same clientSocket. But it’s a different connection.

    Previous answer(s):

    I think you might be missing the mime-type of your returned data. Try adding the following HTTP header to your reply:

    Content-Type: image/gif
    

    Actually… Are you sending a correct HTTP reply at all (including headers, specifically Content-Length)? If not, shoot me a comment and I’ll post the code that you need for this.

    If, for some reason, you cannot set the content-type header to let the browser know that you are sending it an image, you might be able to load the binary data on the client with an XMLHttpRequest into a JavaScript function rather than specifying it as the source Url of an img tag. Then you can use JavaScript to encode the binary data into a dataURI (http://en.wikipedia.org/wiki/Data_URI_scheme) with the correct mime type and set that as the source of the image.

    Actually, I just noticed something in your code:

    new String(fileByte)
    

    might interpret the fileBytes as unicode characters rather than binary. Then, when you write this to the writer, it might screw it up as probably not all data in the image are valid unicode. Try replacing the line with this:

    writer.write(fileByte, 0, fileByte.length);
    

    Maybe this is all you need to do to fix it???

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

Sidebar

Related Questions

I have a TCP socket connection where I get byte array of data which
I have a simple TCP server that listens on a port. var net =
So I am creating C++ HTTP emulating via TCP server. I will have simple
I have a simple TCP server that communicates with some devices via GPRS. It
I have built a simple TCP server and need to compare client input with
I have written a simple TCP client and server. The problem lies with the
Right now I've got a simple TCP server/client. I have it set up so
i make a simple TCP client/server in C# and i have the problem. When
I currently have a problem with a Java server thingy. It's a simple TCP
I have a simple console app that calls a WCF service over net.tcp and

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.