LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-08-2012, 02:53 PM   #1
varoluscuprens
LQ Newbie
 
Registered: Jul 2011
Location: Turkey
Distribution: ubuntu 14.04
Posts: 17

Rep: Reputation: Disabled
[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.
 
Old 05-09-2012, 12:06 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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?
 
Old 05-12-2012, 03:56 PM   #3
varoluscuprens
LQ Newbie
 
Registered: Jul 2011
Location: Turkey
Distribution: ubuntu 14.04
Posts: 17

Original Poster
Rep: Reputation: Disabled
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.
 
Old 05-12-2012, 10:52 PM   #4
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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.
 
  


Reply

Tags
implementation, java, server



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Sun To Open Source Java System Web Server and Web Proxy Technologies LXer Syndicated Linux News 0 08-03-2008 07:50 PM
Need help to choose hardware for Web Server running website based on Linux and Java saharabear Linux - Hardware 1 07-02-2008 07:55 AM
Basic web-server set-up oldwordsmith Linux - Newbie 4 05-26-2006 09:09 AM
(java) BASIC and FORM based auth in the same web app mhiggins Programming 0 10-14-2004 10:07 AM
Looking for a very basic HOWTO on setting up a web server CatchFlipsidE Linux - Newbie 5 08-25-2004 01:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration