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 / General / June 2006

Tip: Looking for answers? Try searching our database.

Newbie server  response question

Thread view: 
Bob - 13 Jun 2006 01:31 GMT
Hi,
I am trying to build a very simple server.  It is supposed to listen on
a port, get a very simple one line command about what to send back, and
then send that back.  I am having trouble with the listening part.  The
code I have below works but is very slow.  More specifically, I have
the following code listening for the command (the string variable
"line"):

BufferedReader in = new BufferedReader(
new InputStreamReader(client.getInputStream()));

  boolean eof2 = false;
           while (!eof2) {
               String line = in.readLine();
               if (line != null){
                   System.out.println(line);
eof2=true;
}
}

When I have the while loop disabled by setting eof2=true, this takes
under 0.3 seconds to run, but when I enable it, it seems to run
correctly (it correctly detects the string that the client is passing
to it) but it takes 10 seconds to run.  Any suggestions?

Thanks,
Bob

Full code below:

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

public class TimeServerWQry52 extends Thread {
   private ServerSocket sock;
String neum="1";
String line="";
   public TimeServerWQry52() {
       super();
       try {
           sock = new ServerSocket(4415);
           System.out.println("TimeServer running ...");
       } catch (IOException e) {
           System.out.println("Error: couldn't create socket.");
           System.exit(1);
       }
   }

   public void run() {
       Socket client = null;

       while (true) {
           if (sock == null)
               return;
           try {
               client = sock.accept();

  BufferedReader in = new BufferedReader(
               new InputStreamReader(client.getInputStream()));

  boolean eof2 = false;
           while (!eof2) {
               String line = in.readLine();
               if (line != null){
                   System.out.println(line);
eof2=true;
}
}

BufferedOutputStream bos = new BufferedOutputStream(
                   client.getOutputStream());
               PrintWriter os = new PrintWriter(bos, false);
               String outLine;

               Date now = new Date();
               os.println(now +  "\n\r Andrew added this");
System.out.println("New connection made");

qrySeriesData3 qryhere=new qrySeriesData3(neum);

for (Iterator i=qryhere.qryResult.iterator();i.hasNext(); ){
String lcResult=(String) i.next();
os.println(lcResult);

}
os.println("Transfer Finished \n\r");

               os.flush();

              os.close();  // this closes the connection

               client.close();
System.out.println("Transfer finished successfully");
           } catch (IOException e) {
               System.out.println("Error: couldn't connect to
client.");
       //        System.exit(1);
           }
       }
   }

   public static void main(String[] arguments) {
       TimeServerWQry52 server = new TimeServerWQry52();
       server.start();
   }

}
hiwa - 13 Jun 2006 02:40 GMT
We got no problem. It's a plain standard minmal C/S program.
---------------------------------------------------------------------------------------------------------
import java.io.*;
import java.net.*;
import java.util.*;

public class TimeServer implements Runnable{
 private ServerSocket sock;
 String neum="1";
 String line="";

 public TimeServer() {
   try {
     sock = new ServerSocket(4415);
     System.out.println("TimeServer running ...");
   } catch (IOException e) {
     System.out.println("Error: couldn't create socket.");
     System.exit(1);
   }
 }

 public void run() {
   Socket client = null;

   while (true) {
     if (sock == null)
       return;
     try {
       client = sock.accept();
       System.out.println("--client connected--");
       BufferedReader in = new BufferedReader(
           new InputStreamReader(client.getInputStream()));

       boolean eof2 = false;
       while (!eof2) {
         String line = in.readLine();
         if (line != null){
           System.out.println(line);
           eof2 = true;
         }
       }

       BufferedOutputStream bos = new BufferedOutputStream(
           client.getOutputStream());
       PrintWriter os = new PrintWriter(bos, false);
       String outLine;

       Date now = new Date();
       os.println(now +  "\n Andrew added this");
       System.out.println("New connection made");

       os.println("Transfer Finished \n\r");
       os.flush();
       os.close();  // this closes the connection

       client.close();
       System.out.println("Transfer finished successfully");
     } catch (IOException e) {
       System.out.println("Error: couldn't connect to client.");
       //        System.exit(1);
     }
   }
 }

 public static void main(String[] arguments) {
   TimeServer server = new TimeServer();
   new Thread(server).start();
 }
}

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

public class TimeClient{

 public static void main(String[] args){
   PrintWriter pw = null;
   BufferedReader br = null;
   String line = null;
   try{
     Socket s = new Socket("127.0.0.1", 4415);
     pw = new PrintWriter(new BufferedWriter
      (new OutputStreamWriter(s.getOutputStream())));
     br = new BufferedReader(new
InputStreamReader(s.getInputStream()));

     pw.println("Hi! I'm a new client Mary.");
     pw.flush();

     while ((line = br.readLine()) != null){
       System.out.println(line);
     }
   }
   catch (Exception e){
     e.printStackTrace();
   }
 }
}
hiwa - 13 Jun 2006 03:02 GMT
A quick addendum: your client program, esp. I/O part,  should have
problem(s).
Mark Space - 13 Jun 2006 06:10 GMT
> under 0.3 seconds to run, but when I enable it, it seems to run
> correctly (it correctly detects the string that the client is passing
> to it) but it takes 10 seconds to run.  Any suggestions?

I'm not sure (didn't read through the full source), but 10 secs sounds
like a network timeout.  Maybe the client should be closing the channel
but isn't?  Maybe that's why your EOF test is fouling you up?
Oliver Wong - 13 Jun 2006 20:19 GMT
> Hi,
> I am trying to build a very simple server.  It is supposed to listen on
[quoted text clipped - 15 lines]
> }
> }

   This code will loop forever if the client closes the connection without
sending anything. I'm guessing your intent is to read a single line from the
client. If so, there's no need for the loop. readLine() is a synchronous
operation.

> When I have the while loop disabled by setting eof2=true, this takes
> under 0.3 seconds to run, but when I enable it, it seems to run
> correctly (it correctly detects the string that the client is passing
> to it) but it takes 10 seconds to run.  Any suggestions?

   Does your client terminate its message with a newline?

   - Oliver
Bob - 13 Jun 2006 23:57 GMT
Thanks so much for all the help.
> > Hi,
> > I am trying to build a very simple server.  It is supposed to listen on
[quoted text clipped - 29 lines]
>
>     - Oliver


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.