CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2008
    Posts
    40

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

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Apr 2008
    Posts
    40

    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?

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    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.

  6. #6
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    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.

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Multiple queries to a single connection

    If the "LOOP n" output is printed on the same thread then your code is not blocking waiting for a response so it must either be getting any empty string or br.readline() is returning null.

    Try putting print statements in the read while loop and after the loop to see what values are in res and line and how many times the loop executes.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  8. #8
    Join Date
    Apr 2008
    Posts
    40

    Re: Multiple queries to a single connection

    You where right, I checked up with the people responsible for the server side. There was a problem on their side causing the readLine to block.

    But still with that problem fixed I cant seem to get output for subsequent queries done on the same socket connection. Only the first query on a socket will return anything. So the output looks like this:

    Code:
    Connected
    LOOP: 0
    {SERVER OUTPUT}
    LOOP: 1
    
    LOOP: 2
    The queries look like this (printed to console instead of socket outputstream):

    Code:
    Connected
    LOOP: 0
    GET dog#n#1 HTTP/1.0
    Accept: text/plain, text/html, text/*
    
    LOOP: 1
    GET dog#n#1 HTTP/1.0
    Accept: text/plain, text/html, text/*
    
    LOOP: 2
    GET dog#n#1 HTTP/1.0
    Accept: text/plain, text/html, text/*
    So it is the same exact query over and over again. Does it seem like this is a serverside issue too or am I doing something wrong with the streams?

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
  •  





Click Here to Expand Forum to Full Width

Featured