LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [Java] Running A Basic Web Server (https://www.linuxquestions.org/questions/programming-9/%5Bjava%5D-running-a-basic-web-server-943939/)

varoluscuprens 05-08-2012 02:53 PM

[Java] Running A Basic Web Server
 
Hi There;
There is a java class that implements basic web server.It gets the port number from console. This code is very basic, it returns what the client sends.Here are the codes:

Code:

package p106;

import java.io.*;
import java.net.*;

public class HttpMirror {

        public static void main(String[] args) {

                try
                {
                        //Get the port.
                        int port = Integer.parseInt(args[0]);
                       
                        //Create a socket for listening that port.
                        ServerSocket ss = new ServerSocket(port);
                       
                        for(;;)
                        {
                               
                                /* Wait for connection from the client. Routine will be blocked when
                                * it returns the socket that connected to the client. 
                                * */
                                Socket client = ss.accept();
                               
                                //Set the input and output in order to talk with the client.
                                BufferedReader in = new BufferedReader( new InputStreamReader(client.getInputStream()));
                                PrintWriter out = new PrintWriter(client.getOutputStream());
                               
                                out.print("HTTP/1.0 200 en");
                                out.print("Content-Type text/plain");
                               
                                /* At that point, read HTTP request and send it back to the client.
                                *
                                * */
                               
                                String line;
                                while( (line = in.readLine()) != null)
                                {
                                        if(line.length() == 0)
                                                break;
                                        out.print(line );
                                       
                                }
                                                               
                                out.close();
                                in.close();
                                client.close();
                               
                        } // Tekrar döngüye gir ve bir sonraki bağlantıyı bekle.
                }catch(Exception e)
                {
                        System.err.println(e);
                }

        }

}

My OS is ubuntu 11.10. My browser is Mozilla Firefox. My IDE is Eclipse. First of all, I give “4444” as a port number, by the way this code will accept 4444 as a port number. And press “Run” button in eclipse. When the code runs, I start firefox and I type “localhost:4444” and “127.0.0.1:4444”, but there is no result, there is a blank page in browser.
What is the problem, how can I solve it?How can that class work?
Thanks in advance.

crabboy 05-09-2012 12:06 PM

This program works as written. All it does is return the exact information passed to it. Since you are simply going to http://localhost:4444, there is nothing to return. If you add a breakpoint at the Socket call, you'll see that it is opening the socket and reading the http header and echoing that back to the browser.

What are you expecting from the program?

varoluscuprens 05-12-2012 03:56 PM

I expect program to show requests in which the clients sends. The modificated and corrected version is there:
The problem was solved in: http://forum.ceviz.net/java-jsp-jsf/...tml#post696442

Quote:

String crlf = "\r\n";
out.print("HTTP/1.1 200 OK" + crlf);
out.print("Content-Type text/plain" + crlf);

/*
* At that point, read HTTP request and send it back to the
* client.
*/
out.print(crlf);
String line;
while ((line = in.readLine()) != null) {
if (line.length() == 0)
break;
out.print(line + crlf);
System.out.println(line);
}
Thanks crabboy for your interest.

crabboy 05-12-2012 10:52 PM

Can't read the language, but based on the program it looks like it is creating a proper http header for the browser for which to display the original request.


All times are GMT -5. The time now is 01:57 PM.