Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Tools / October 2005

Tip: Looking for answers? Try searching our database.

Socket-Problem: Server for WEB-Client

Thread view: 
dietmar - 07 Oct 2005 19:19 GMT
my server programm sends a response (e.g. HTML-code) to the requested
web-browser-client (firefox or IE).
It works, but only when the request data in the input-stream from the
client-socket are read.

Why??

Dietmar

see the example code:
...
 ServerSocket server = new ServerSocket(80); // any other port(9991);
Socket client=server.accept();

String ip=client.getInetAddress().toString();

 // Read request from client-socket input-stream
 // if the entire read code is marked out as commentar,
 // it doesn't worked.
 InputStream       is  = client.getInputStream();
 InputStreamReader isr = new InputStreamReader(is);
 BufferedReader    br = new BufferedReader(isr);
 StringBuffer request= new StringBuffer();
  while ((r=br.readLine()).length()!=0)
  {
    request.append(r + "\n");
  }

  // Write response to client
  OutputStream os = client.getOutputStream();
  Date date=new Date();
  String s=  "<html> <head><title>Hallo Web</title></head>"+
   "<body><h1>HELLO" + ip + " im WEB</h1>"+"</body>";
  byte b[]=s.getBytes();
  os.write(b);
...

IE-call: http:\\localhost
Roedy Green - 08 Oct 2005 00:10 GMT
>   while ((r=br.readLine()).length()!=0)
>   {
>     request.append(r + "\n");
>   }

just a stylistic aside, I would write that as :

  while ((r=br.readLine()).length()!=0)
  {
   request.append(r);
   request.append( '\n');
  }

To avoid creating an extra StringBuffer every time round the loop.

The other thing is you carefully find the \ns and then hide them
again. I would have expected code more like this that preserves the
lines:
  while ((r=br.readLine()).length()!=0)
  {
  lineArrayList.add( r );
  }

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 08 Oct 2005 00:15 GMT
>   String s=  "<html> <head><title>Hallo Web</title></head>"+
>    "<body><h1>HELLO" + ip + " im WEB</h1>"+"</body>";
>   byte b[]=s.getBytes();

Again some peripheral issues likely not related to your problem.

Your <html> tag is unbalanced.

getBytes is using the SERVER'S platform default encoding.  It a real
app you want to use client's preferred encoding.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 08 Oct 2005 00:17 GMT
>It works, but only when the request data in the input-stream from the
>client-socket are read.

And when you don't read the entire input stream what happens?

1. an exception?  what was it? What was the stack trace?

2. there is no response at all with no message?

3. you get some sort of garbled response?  What was it. See
http://mindprod.com/jgloss/packetsniffer.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.