-
January 10th, 2011, 01:17 PM
#1
Multiple queries to a single connection
Hi guys!
Need some help navigating the confusing sea of network I/O. Ive made a single connection object to a server and need to do multiple queries on that connection using its input- and outputstream. The problem is that the server only returns a result for the first query. Either this is the server (which I doubt), or there us something wrong with my code that makes it not catch the result.
I/O has never been my strong point, but Im guessing the problem is just a misformulation or something somewhere in the code. Anyway, Id be really thankful for any help or just general info about network I/O if there is some major stuff that Ive clearly not understood.
Thanks in advance.
Code:
//running this code multiple times causes the result to be empty on all queries except the first.
public void doQuery() {
String res = "";
String query = word + "#" + pos + "#" + num;
query.trim();
try {
OutputStream out = connection.getOutputStream();
PrintWriter outw = new PrintWriter(out, false);
outw.print("GET " + query + " HTTP/1.0\r\n");
outw.print("Accept: text/plain, text/html, text/*\r\n");
outw.print("\r\n");
outw.flush();
InputStream in = connection.getInputStream();
InputStreamReader inr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(inr);
String line = "";
while ((line = br.readLine()) != null) {
res += line;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
results = res;
}
-
January 10th, 2011, 07:00 PM
#2
Re: Multiple queries to a single connection
What class type is 'connection'?
Have you tried closing the connection and reconnecting between method calls?
Have you checked to make sure your second request is getting to the server and without any spurious extra characters?
-
January 11th, 2011, 04:52 AM
#3
Re: Multiple queries to a single connection
Sorry, forgot to specify the type.
connection is of the type java.net.Socket.
I have tried closing and reconnecting, and that works just fine. Its just that I would rather not do it this way because of the number of queries I have to run.
As far as I can see the output to the server is the same for every iteration. Im using the same query multiple times for testing.
After further testing though, it seems like this line of code halts the program:
Code:
br.readLine();
//taken from:
while ((line = br.readLine()) != null) {
res += line;
}
Any ideas why this is happening?
-
January 11th, 2011, 08:49 AM
#4
Re: Multiple queries to a single connection
Do you have the server code? I suspect the server is not responding to subsequent requests and the readLine() method is blocking waiting for a response.
-
January 11th, 2011, 12:15 PM
#5
Re: Multiple queries to a single connection
It's possible that the responses from the server are not terminated with newline or carriage return. They need to be if you want to use BufferedReader.read (see docs). A simple solution is to not wrap the InputStreamReader in a BufferedReader & instead use the InputStreamReader.read methods.
-
January 11th, 2011, 12:19 PM
#6
Re: Multiple queries to a single connection
Another suggestion: Test things manually, outside of your program with telnet or netcat. I prefer netcat, but you'll probably need to download it. I use the netcat that comes with cygwin.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|