Newbie server response question

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

}
 
H

hiwa

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

Mark Space

Bob said:
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?
 
O

Oliver Wong

Bob said:
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;
}
}

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
 
B

Bob

Thanks so much for all the help.
Oliver said:
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.


Does your client terminate its message with a newline?

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top