-
Serving up a Webpage
I am trying to create a chat program where, I run a server program on my computer, and anyone who connects to my private IP address via port 4444 will receive a webpage (chat.html). My problem is that no one on my private network can get my chat.html page when they connect to my IP address although they can connect. The chat.html file is located on my machine in the same folder as the server program. What do I have to do to the chat.html file or my server program that will make the other computer capable of receiving the file?
-
Have you tested opening the file from the server program without sending it.
Can it be opened?
If the server is successful opening the file, but not sending it; how do you send it?
-
I am able to open up the html file, and I haven't been able to send the html file without the other computer have had a client program to connect to it first. I did some research, and it seems that I need to create a servlet that will serve the html file when I connect to it from my browser... My only question is how to create a servlet that will do so (or maybe the file could be downloaded ) and do I need tomcat to test my servlet?
-
I'm lost on what you are trying to do.
Is the client a program you have written or any Web browser?
If not a Web browser; should the file be sent on connection or when requested?
-
Just any web browser. Once connected the user should be presented with a webpage, just like a web server.
-
I've never used Java's built-in classes for HTTP, maybe they can be used.
Otherwise:
When the client is connecting, it will send the line
GET (*) HTTP/1.(**)
ending with \r\n, (*) is the file the client is trying to access: \ for the default page. (**) will be 1, but 0 on Lynx and Mobile Safari (old protocol version).
After that line the client will also send a number of lines (including virtual host), all ending with \r\n, followed by an empty line ending with \r\n.
The the server should send:
HTTP/1.(**) 200 OK\r\n
Content-Length: (***)\r\n
Connection: close
\r\n
(****)
Where (***) is the number of bytes, in decimal (10-base), in the file to send,
and (****) is all bytes in the file to send.
And then close the connection.
But the best, and easiest way, might be to just install a HTTP server on your computer.
-
Thank you for all your help.
I followed what you said and did a little research and found what I needed to write the contents of my 'chat.html' file to any socket that connects to my java server. But a new problem has surfaced. The webpage also has an <iframe> tag that displays another webpage (An empty html file that the server will write to) but my initial webpage cant find. It attempts to look for it at http://192.168.15.3:15975/text.html.
Do I need to place my 'text.html' file some where in on my hardrive so that my initial webpage can find it? I've tried send 'chat.html' and 'text.html' but the two website end up right on top of each other and not inside of the <iframe> tag. Where should I go from here.
If this isn't possible then I might just go with a web server that just sends a client jnlp file and the clients can just use Java web start to run the program. I started with this idea because it was the easiest one todo.
-
I assume "chat.html" does not define a base catalogue.
The file for the iframe (src ?) should be "text.html" (or "./text.html"), but I would suggest renaming the file to just "text" or "text.txt" if it is just plain text (MIME: text/plain). It should be located in the same catalogue as "chat.html".
If you are using to protocol a wrote, the easiest way to avoid an attack where someone tries to access you computes files is to not allow any other file that intended, by having a list of accepted file. But I would suggest to make text.html not an actual file but rather store its content internally in the server program as a StringBuilder and send the StringBuilder's content rather the a file's content.
Why don't just make an applet and attach it the to a HTML, otherwise you most learn how the receive the messages the clients writes (PUT verb in HTTP).