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