This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

PLZ HELP simple java server  RSS feed

 
Sass Sasss
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, help, I want to make simple server, I'm just learning java, how is it possible to do?

==Code==
import java.io.*;
import java.net.*;
public class Teade3{
public static void main(String argumendid[]) throws IOException{
ServerSocket ss=new ServerSocket(80);
while(true){
new Teate3loim(ss.accept());
}
}
}
class Teate3loim extends Thread{
Socket sc;
public Teate3loim(Socket uus_sc){
sc=uus_sc;
start();
}

public void run(){
try{
PrintWriter web=new PrintWriter(sc.getOutputStream(), true);
BufferedReader sisse=new BufferedReader(
new InputStreamReader(sc.getInputStream())
);
web.println("What's ya name? ");
String name=sisse.readLine();
if(name.equals("Mari")){
web.println("We will meet tomorrow");
} else{
web.println("Nobody is here ");
}
sc.close();
}catch(Exception e){
System.out.println("Problem: "+e);
}

}

}
==code==
I want to do the next:
After execution of this code, I'll enter in Address bar http://localhost:80
explorer asks me, what's my name, and how may I enter it to explorer?
Thanks a lot!
[ December 17, 2004: Message edited by: Sass Sasss ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In short, no. The protocol - what one class sends to another and what it gets back - is quite different for a web program.

Each request from the browser is a separate request to the browser. Your browser needs a loop like:

Now all of that is REAL sketchy but the actual code could look quite a lot like that. That would be enough to get you introduced the the protocol, the headers going back & forth. But it would be a LOUSY design for a real web server because it codes way too much logic in the core class and single threads the whole thing.

If you want to dig into this approach, see if you can code to where I said "read headers" and just print out everything you get from the input stream. You can wrap the stream in a buffered reader and readline() and printline() until there is no more to read. Once you see the headers you can start to see how the next bits work.

A parallel bit of research ... do you know how to write HTML forms? You'll need that to send the name from the browser to the server.

Hope that helps!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!