B
Bob
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();
}
}
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();
}
}