This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am writing simple web server which accepts requests on socket and sends html text response.
I am receiving requests from browser on the specified port. like http://localhost:5555 Now I also want to send small gif files and want to display them on the browser page. The resulting html page will display text combined with gif images at specific positions.
I am using javaImageIO API for the same. However I am not getting the desired page.
Here is my sample code on Server side: I am listening to requests on serverSocket 5555
To answer your immediate question: don't do anything so elaborate as that. The server shouldn't care what the content of the image file is. Just use FileInputStream to read the bytes from the file, and send those bytes unchanged over the connection.
Now, to answer the question you should have asked: why are you writing your own web server, rather than using an existing one? HTTP is actually pretty complicated if you include all the conventions and pseudostandards that have emerged over the years. There are plenty of different webservers to choose from. For a smallish one that's easy to embed in your own programs, try Jetty.