Hi,

I have a problem about correctly closing the underlying socket after
opening and reading an input stream from a URL object. The following
code leaves many (i.e. all) sockets open (in the CLOSE_WAIT state, as
netstat reports):

        private void test() {
                for (int i = 0; i < 10000; i++) {
                        try {
                                String urlStr = "http://test-url.com/?q="; + i;
                                URL url = new URL(urlStr);
                                InputStream is = url.openStream();
                                byte[] buffer = new byte[1024];
                                int len = is.read(buffer);
                                String content = "";
                                while (len != -1) {
                                        content += new String(buffer, 0, len);
                                        len = is.read(buffer);
                                }
                                is.close();
                        } catch (Exception e) {
                                Log.w(TAG,e);
                        }
                        Log.i(TAG, "i: " + i);
                }
        }

As a consequence, no more sockets can be opened after a while. The
problem does not seem to occur when running the same code in a
"normal" java program (i.e. not in android). Am I missing something
here? Or is this problem due to a bug in android? How could I read
many URLs without running into this problem? (I want to query a web
service with lots of different parameters, that's why I need to open
that many connections.)

By the way: The problem only occurs, if the urlStr changes in each
loop.

Thanks for any hints,

Michael


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to