Multiple queries to a single connection
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Multiple queries to a single connection

Hybrid View

  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,474

    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,474

    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.

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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center