From: Alex L. <ale...@gm...> - 2011-05-19 13:53:41
|
Hi, I wrote a very simple Java->iPhone application to open a connection and read the data in the response, but I found I only ever got back 26 bytes of data. Here's the simplest code I could put together which exhibits the problem... private boolean finished = false; @Override public void applicationDidFinishLaunching(UIApplication app) { try { final NSURL url = NSURL.URLWithString("http://www.google.com"); final NSMutableURLRequest req = new NSMutableURLRequest(url); NSURLConnectionDelegate delegate = new NSURLConnectionDelegate() { @Override public void connectionDidFailWithError( NSURLConnection connection, NSError error) { super.connectionDidFailWithError(connection, error); System.out.println("Error [" + error + "]"); } @Override public void connectionDidFinishLoading( NSURLConnection connection) { super.connectionDidFinishLoading(connection); finished = true; System.out.println("Finish loading"); } @Override public void connectionDidReceiveData( NSURLConnection connection, NSData data) { super.connectionDidReceiveData(connection, data); System.out.println("Received data [" + new String(data.getBytes()) + "]"); } }; NSURLConnection.connectionWithRequest(req, delegate); // Wait until all the data has been received or we hit a 20s limit. int i = 0; while (!finished && i < 20) { Thread.sleep(1000); i++; } } catch (Exception e) { System.err.println("Exception doing HTTP" + e); } } I would have expected multiple "Received Data[.......]" messages but I only ever get one of 26 characters. I took a look at the NSData java class, specifically the readData method and I can see why it is failing. The do/while loop in there only continues to read data if its buffer has been filled and assumes that if the read() method returns a read count of less than the buffer size that all the data has been read. Using the code above the response contains an initial 26 bytes and consequently stops as the buffer in NSData has not been completely filled. I wrote an equivalent pure java application and it exhibits the same behaviour using the readData code. Rewriting the readData method so it keeps reading until -1 is returned and buffering the code appropriately retrieves all the data which is approximately 9k bytes. Is this a bug or have I done something wrong? Would the code work properly if the application were running on an iPhone rather than in the Emulator? Thanks, Alex |
From: Arno P. <ar...@pu...> - 2011-05-19 17:06:07
|
I'm not sure if you should do that sleep() in your code. The NSURLConnection delegate will most likely be executed in the context of the same thread that created it, but if you put that thread to sleep, problems will occur. Basically you implemented a busy wait which is never a good idea. This in not an XMLVM problem, IMHO. Arno On 5/19/11 6:53 AM, Alex Lewis wrote: > Hi, > I wrote a very simple Java->iPhone application to open a connection > and read the data in the response, but I found I only ever got back 26 > bytes of data. Here's the simplest code I could put together which > exhibits the problem... > > private boolean finished = false; > @Override > public void applicationDidFinishLaunching(UIApplication app) { > try { > final NSURL url = NSURL.URLWithString("http://www.google.com"); > final NSMutableURLRequest req = new NSMutableURLRequest(url); > NSURLConnectionDelegate delegate = new NSURLConnectionDelegate() { > @Override > public void connectionDidFailWithError( > NSURLConnection connection, NSError error) { > super.connectionDidFailWithError(connection, error); > System.out.println("Error [" + error + "]"); > } > @Override > public void connectionDidFinishLoading( > NSURLConnection connection) { > super.connectionDidFinishLoading(connection); > finished = true; > System.out.println("Finish loading"); > } > @Override > public void connectionDidReceiveData( > NSURLConnection connection, NSData data) { > super.connectionDidReceiveData(connection, data); > System.out.println("Received data [" + new String(data.getBytes()) + "]"); > } > }; > NSURLConnection.connectionWithRequest(req, delegate); > // Wait until all the data has been received or we hit a > 20s limit. > int i = 0; > while (!finished && i < 20) { > Thread.sleep(1000); > i++; > } > } > catch (Exception e) { > System.err.println("Exception doing HTTP" + e); > } > } > > I would have expected multiple "Received Data[.......]" messages but I > only ever get one of 26 characters. I took a look at the NSData java > class, specifically the readData method and I can see why it is failing. > The do/while loop in there only continues to read data if its buffer has > been filled and assumes that if the read() method returns a read count > of less than the buffer size that all the data has been read. Using the > code above the response contains an initial 26 bytes and consequently > stops as the buffer in NSData has not been completely filled. I wrote an > equivalent pure java application and it exhibits the same behaviour > using the readData code. Rewriting the readData method so it keeps > reading until -1 is returned and buffering the code appropriately > retrieves all the data which is approximately 9k bytes. > > Is this a bug or have I done something wrong? Would the code work > properly if the application were running on an iPhone rather than in the > Emulator? > > Thanks, > Alex > > > > ------------------------------------------------------------------------------ > What Every C/C++ and Fortran developer Should Know! > Read this article and learn how Intel has extended the reach of its > next-generation tools to help Windows* and Linux* C/C++ and Fortran > developers boost performance applications - including clusters. > http://p.sf.net/sfu/intel-dev2devmay > > > > _______________________________________________ > xmlvm-users mailing list > xml...@li... > https://lists.sourceforge.net/lists/listinfo/xmlvm-users |
From: Paul P. <bay...@gm...> - 2011-05-19 18:14:42
|
I agree with Arno about your use of sleep(). But if you insist, note that sleep() isn't yet implemented in the C backend. It will currently just return immediately, so while you're expecting a 20 second delay, you're getting something closer to 0 seconds. Aside from that, I intend to implement sleep(), wait(), notify(), interrupt(), etc. in the next few weeks in a similar fashion to what I had done for the Obj-C backend. Paul On Thu, May 19, 2011 at 12:05 PM, Arno Puder <ar...@pu...> wrote: > > I'm not sure if you should do that sleep() in your code. The > NSURLConnection delegate will most likely be executed in the context of > the same thread that created it, but if you put that thread to sleep, > problems will occur. Basically you implemented a busy wait which is > never a good idea. This in not an XMLVM problem, IMHO. > > Arno > > > On 5/19/11 6:53 AM, Alex Lewis wrote: > > Hi, > > I wrote a very simple Java->iPhone application to open a connection > > and read the data in the response, but I found I only ever got back 26 > > bytes of data. Here's the simplest code I could put together which > > exhibits the problem... > > > > private boolean finished = false; > > @Override > > public void applicationDidFinishLaunching(UIApplication app) { > > try { > > final NSURL url = NSURL.URLWithString("http://www.google.com"); > > final NSMutableURLRequest req = new NSMutableURLRequest(url); > > NSURLConnectionDelegate delegate = new NSURLConnectionDelegate() { > > @Override > > public void connectionDidFailWithError( > > NSURLConnection connection, NSError error) { > > super.connectionDidFailWithError(connection, error); > > System.out.println("Error [" + error + "]"); > > } > > @Override > > public void connectionDidFinishLoading( > > NSURLConnection connection) { > > super.connectionDidFinishLoading(connection); > > finished = true; > > System.out.println("Finish loading"); > > } > > @Override > > public void connectionDidReceiveData( > > NSURLConnection connection, NSData data) { > > super.connectionDidReceiveData(connection, data); > > System.out.println("Received data [" + new String(data.getBytes()) + > "]"); > > } > > }; > > NSURLConnection.connectionWithRequest(req, delegate); > > // Wait until all the data has been received or we hit a > > 20s limit. > > int i = 0; > > while (!finished && i < 20) { > > Thread.sleep(1000); > > i++; > > } > > } > > catch (Exception e) { > > System.err.println("Exception doing HTTP" + e); > > } > > } > > > > I would have expected multiple "Received Data[.......]" messages but I > > only ever get one of 26 characters. I took a look at the NSData java > > class, specifically the readData method and I can see why it is failing. > > The do/while loop in there only continues to read data if its buffer has > > been filled and assumes that if the read() method returns a read count > > of less than the buffer size that all the data has been read. Using the > > code above the response contains an initial 26 bytes and consequently > > stops as the buffer in NSData has not been completely filled. I wrote an > > equivalent pure java application and it exhibits the same behaviour > > using the readData code. Rewriting the readData method so it keeps > > reading until -1 is returned and buffering the code appropriately > > retrieves all the data which is approximately 9k bytes. > > > > Is this a bug or have I done something wrong? Would the code work > > properly if the application were running on an iPhone rather than in the > > Emulator? > > > > Thanks, > > Alex > > > > > > > > > ------------------------------------------------------------------------------ > > What Every C/C++ and Fortran developer Should Know! > > Read this article and learn how Intel has extended the reach of its > > next-generation tools to help Windows* and Linux* C/C++ and Fortran > > developers boost performance applications - including clusters. > > http://p.sf.net/sfu/intel-dev2devmay > > > > > > > > _______________________________________________ > > xmlvm-users mailing list > > xml...@li... > > https://lists.sourceforge.net/lists/listinfo/xmlvm-users > > > ------------------------------------------------------------------------------ > What Every C/C++ and Fortran developer Should Know! > Read this article and learn how Intel has extended the reach of its > next-generation tools to help Windows* and Linux* C/C++ and Fortran > developers boost performance applications - including clusters. > http://p.sf.net/sfu/intel-dev2devmay > _______________________________________________ > xmlvm-users mailing list > xml...@li... > https://lists.sourceforge.net/lists/listinfo/xmlvm-users > |
From: Alex L. <ale...@gm...> - 2011-05-19 18:38:36
|
Ah yes, take out the sleep and finished flag. Sorry that was mistakenly left in from other tests I had been trying. Without the sleep and finished flag the system output is still (when using the google url)... Received data [<!doctype html><html><head] Finish loading So looking at the implementation of NSURLConnection calling "connectionWithRequest" starts a new thread so the method calls should be asynchronous back to the delegate? So I would expect the "applicationDidFinishLaunching" method to finish quickly but whilst the NSURLConnection thread is running to get the output generated by the delegate. Looking at the code again I see there should be just the one "Received data" message followed by the "Finish Loading" message but there should be a lot more data in NSData's byte array. Thanks, Alex Just in case here's the modified code... @Override public void applicationDidFinishLaunching(UIApplication app) { try { final NSURL url = NSURL.URLWithString("http://www.google.com"); final NSMutableURLRequest req = new NSMutableURLRequest(url); NSURLConnectionDelegate delegate = new NSURLConnectionDelegate() { @Override public void connectionDidFailWithError( NSURLConnection connection, NSError error) { super.connectionDidFailWithError(connection, error); System.out.println("Error [" + error + "]"); } @Override public void connectionDidFinishLoading( NSURLConnection connection) { super.connectionDidFinishLoading(connection); System.out.println("Finish loading"); } @Override public void connectionDidReceiveData( NSURLConnection connection, NSData data) { super.connectionDidReceiveData(connection, data); System.out.println("Received data [" + new String(data.getBytes()) + "]"); } }; NSURLConnection.connectionWithRequest(req, delegate); } catch (Exception e) { System.err.println("Exception doing HTTP" + e); } } On 19 May 2011 18:05, Arno Puder <ar...@pu...> wrote: > > I'm not sure if you should do that sleep() in your code. The > NSURLConnection delegate will most likely be executed in the context of > the same thread that created it, but if you put that thread to sleep, > problems will occur. Basically you implemented a busy wait which is > never a good idea. This in not an XMLVM problem, IMHO. > > Arno > > > On 5/19/11 6:53 AM, Alex Lewis wrote: > > Hi, > > I wrote a very simple Java->iPhone application to open a connection > > and read the data in the response, but I found I only ever got back 26 > > bytes of data. Here's the simplest code I could put together which > > exhibits the problem... > > > > private boolean finished = false; > > @Override > > public void applicationDidFinishLaunching(UIApplication app) { > > try { > > final NSURL url = NSURL.URLWithString("http://www.google.com"); > > final NSMutableURLRequest req = new NSMutableURLRequest(url); > > NSURLConnectionDelegate delegate = new NSURLConnectionDelegate() { > > @Override > > public void connectionDidFailWithError( > > NSURLConnection connection, NSError error) { > > super.connectionDidFailWithError(connection, error); > > System.out.println("Error [" + error + "]"); > > } > > @Override > > public void connectionDidFinishLoading( > > NSURLConnection connection) { > > super.connectionDidFinishLoading(connection); > > finished = true; > > System.out.println("Finish loading"); > > } > > @Override > > public void connectionDidReceiveData( > > NSURLConnection connection, NSData data) { > > super.connectionDidReceiveData(connection, data); > > System.out.println("Received data [" + new String(data.getBytes()) + > "]"); > > } > > }; > > NSURLConnection.connectionWithRequest(req, delegate); > > // Wait until all the data has been received or we hit a > > 20s limit. > > int i = 0; > > while (!finished && i < 20) { > > Thread.sleep(1000); > > i++; > > } > > } > > catch (Exception e) { > > System.err.println("Exception doing HTTP" + e); > > } > > } > > > > I would have expected multiple "Received Data[.......]" messages but I > > only ever get one of 26 characters. I took a look at the NSData java > > class, specifically the readData method and I can see why it is failing. > > The do/while loop in there only continues to read data if its buffer has > > been filled and assumes that if the read() method returns a read count > > of less than the buffer size that all the data has been read. Using the > > code above the response contains an initial 26 bytes and consequently > > stops as the buffer in NSData has not been completely filled. I wrote an > > equivalent pure java application and it exhibits the same behaviour > > using the readData code. Rewriting the readData method so it keeps > > reading until -1 is returned and buffering the code appropriately > > retrieves all the data which is approximately 9k bytes. > > > > Is this a bug or have I done something wrong? Would the code work > > properly if the application were running on an iPhone rather than in the > > Emulator? > > > > Thanks, > > Alex > > > > > > > > > ------------------------------------------------------------------------------ > > What Every C/C++ and Fortran developer Should Know! > > Read this article and learn how Intel has extended the reach of its > > next-generation tools to help Windows* and Linux* C/C++ and Fortran > > developers boost performance applications - including clusters. > > http://p.sf.net/sfu/intel-dev2devmay > > > > > > > > _______________________________________________ > > xmlvm-users mailing list > > xml...@li... > > https://lists.sourceforge.net/lists/listinfo/xmlvm-users > > > ------------------------------------------------------------------------------ > What Every C/C++ and Fortran developer Should Know! > Read this article and learn how Intel has extended the reach of its > next-generation tools to help Windows* and Linux* C/C++ and Fortran > developers boost performance applications - including clusters. > http://p.sf.net/sfu/intel-dev2devmay > _______________________________________________ > xmlvm-users mailing list > xml...@li... > https://lists.sourceforge.net/lists/listinfo/xmlvm-users > |
From: Paul P. <bay...@gm...> - 2011-06-20 14:48:36
|
FYI - I implemented the C version of wait/notify/sleep/interrupt a few days ago, so you may use Thread.sleep(long) now. Thanks, Paul On Thu, May 19, 2011 at 1:38 PM, Alex Lewis <ale...@gm...> wrote: > Ah yes, take out the sleep and finished flag. Sorry that was mistakenly > left in from other tests I had been trying. Without the sleep and finished > flag the system output is still (when using the google url)... > > Received data [<!doctype html><html><head] > Finish loading > > So looking at the implementation of NSURLConnection calling > "connectionWithRequest" starts a new thread so the method calls should be > asynchronous back to the delegate? So I would expect the > "applicationDidFinishLaunching" method to finish quickly but whilst the > NSURLConnection thread is running to get the output generated by the > delegate. Looking at the code again I see there should be just the one > "Received data" message followed by the "Finish Loading" message but there > should be a lot more data in NSData's byte array. > > Thanks, > Alex > > > Just in case here's the modified code... > > @Override > public void applicationDidFinishLaunching(UIApplication app) { > try { > final NSURL url = NSURL.URLWithString("http://www.google.com"); > final NSMutableURLRequest req = new NSMutableURLRequest(url); > > NSURLConnectionDelegate delegate = new NSURLConnectionDelegate() { > @Override > public void connectionDidFailWithError( > NSURLConnection connection, NSError error) { > super.connectionDidFailWithError(connection, error); > System.out.println("Error [" + error + "]"); > } > > @Override > public void connectionDidFinishLoading( > NSURLConnection connection) { > super.connectionDidFinishLoading(connection); > System.out.println("Finish loading"); > } > > @Override > public void connectionDidReceiveData( > NSURLConnection connection, NSData data) { > super.connectionDidReceiveData(connection, data); > System.out.println("Received data [" + new String(data.getBytes()) > + "]"); > } > }; > > NSURLConnection.connectionWithRequest(req, delegate); > } > catch (Exception e) { > System.err.println("Exception doing HTTP" + e); > } > } > > On 19 May 2011 18:05, Arno Puder <ar...@pu...> wrote: > >> >> I'm not sure if you should do that sleep() in your code. The >> NSURLConnection delegate will most likely be executed in the context of >> the same thread that created it, but if you put that thread to sleep, >> problems will occur. Basically you implemented a busy wait which is >> never a good idea. This in not an XMLVM problem, IMHO. >> >> Arno >> >> >> On 5/19/11 6:53 AM, Alex Lewis wrote: >> > Hi, >> > I wrote a very simple Java->iPhone application to open a connection >> > and read the data in the response, but I found I only ever got back 26 >> > bytes of data. Here's the simplest code I could put together which >> > exhibits the problem... >> > >> > private boolean finished = false; >> > @Override >> > public void applicationDidFinishLaunching(UIApplication app) { >> > try { >> > final NSURL url = NSURL.URLWithString("http://www.google.com"); >> > final NSMutableURLRequest req = new NSMutableURLRequest(url); >> > NSURLConnectionDelegate delegate = new NSURLConnectionDelegate() { >> > @Override >> > public void connectionDidFailWithError( >> > NSURLConnection connection, NSError error) { >> > super.connectionDidFailWithError(connection, error); >> > System.out.println("Error [" + error + "]"); >> > } >> > @Override >> > public void connectionDidFinishLoading( >> > NSURLConnection connection) { >> > super.connectionDidFinishLoading(connection); >> > finished = true; >> > System.out.println("Finish loading"); >> > } >> > @Override >> > public void connectionDidReceiveData( >> > NSURLConnection connection, NSData data) { >> > super.connectionDidReceiveData(connection, data); >> > System.out.println("Received data [" + new String(data.getBytes()) + >> "]"); >> > } >> > }; >> > NSURLConnection.connectionWithRequest(req, delegate); >> > // Wait until all the data has been received or we hit a >> > 20s limit. >> > int i = 0; >> > while (!finished && i < 20) { >> > Thread.sleep(1000); >> > i++; >> > } >> > } >> > catch (Exception e) { >> > System.err.println("Exception doing HTTP" + e); >> > } >> > } >> > >> > I would have expected multiple "Received Data[.......]" messages but I >> > only ever get one of 26 characters. I took a look at the NSData java >> > class, specifically the readData method and I can see why it is failing. >> > The do/while loop in there only continues to read data if its buffer has >> > been filled and assumes that if the read() method returns a read count >> > of less than the buffer size that all the data has been read. Using the >> > code above the response contains an initial 26 bytes and consequently >> > stops as the buffer in NSData has not been completely filled. I wrote an >> > equivalent pure java application and it exhibits the same behaviour >> > using the readData code. Rewriting the readData method so it keeps >> > reading until -1 is returned and buffering the code appropriately >> > retrieves all the data which is approximately 9k bytes. >> > >> > Is this a bug or have I done something wrong? Would the code work >> > properly if the application were running on an iPhone rather than in the >> > Emulator? >> > >> > Thanks, >> > Alex >> > >> > >> > >> > >> ------------------------------------------------------------------------------ >> > What Every C/C++ and Fortran developer Should Know! >> > Read this article and learn how Intel has extended the reach of its >> > next-generation tools to help Windows* and Linux* C/C++ and Fortran >> > developers boost performance applications - including clusters. >> > http://p.sf.net/sfu/intel-dev2devmay >> > >> > >> > >> > _______________________________________________ >> > xmlvm-users mailing list >> > xml...@li... >> > https://lists.sourceforge.net/lists/listinfo/xmlvm-users >> >> >> ------------------------------------------------------------------------------ >> What Every C/C++ and Fortran developer Should Know! >> Read this article and learn how Intel has extended the reach of its >> next-generation tools to help Windows* and Linux* C/C++ and Fortran >> developers boost performance applications - including clusters. >> http://p.sf.net/sfu/intel-dev2devmay >> _______________________________________________ >> xmlvm-users mailing list >> xml...@li... >> https://lists.sourceforge.net/lists/listinfo/xmlvm-users >> > > > > ------------------------------------------------------------------------------ > What Every C/C++ and Fortran developer Should Know! > Read this article and learn how Intel has extended the reach of its > next-generation tools to help Windows* and Linux* C/C++ and Fortran > developers boost performance applications - including clusters. > http://p.sf.net/sfu/intel-dev2devmay > _______________________________________________ > xmlvm-users mailing list > xml...@li... > https://lists.sourceforge.net/lists/listinfo/xmlvm-users > > |