Question

From an iPad, I am using NSURLRequest to poll a file on an http server on my WLAN. I am reading the file once every second. I using the following code to read the file.

NSURLRequest *request = [NSURLRequest requestWithURL:myURL
                                     cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                     timeoutInterval:30.0];

// Get the data
NSURLResponse *response;
NSError *dataError;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&dataError ];

Running netstat on the server shows the following network activity (where 10.0.1.11 is the ip address of the iPad which is polling the server:

  Proto  Local Address          Foreign Address        State
  TCP    10.0.1.16:80           10.0.1.11:56999        TIME_WAIT
  TCP    10.0.1.16:80           10.0.1.11:57010        TIME_WAIT
  TCP    10.0.1.16:80           10.0.1.11:57011        TIME_WAIT
  TCP    10.0.1.16:80           10.0.1.11:57012        TIME_WAIT
  TCP    10.0.1.16:80           10.0.1.11:57013        TIME_WAIT
  TCP    10.0.1.16:80           10.0.1.11:57014        TIME_WAIT
  TCP    10.0.1.16:80           10.0.1.11:57015        TIME_WAIT
  TCP    10.0.1.16:80           10.0.1.11:57016        TIME_WAIT
  TCP    10.0.1.16:80           10.0.1.11:57017        TIME_WAIT
  TCP    10.0.1.16:80           10.0.1.11:57018        TIME_WAIT
  TCP    10.0.1.16:80           10.0.1.11:57019        TIME_WAIT
  TCP    10.0.1.16:80           10.0.1.11:57020        TIME_WAIT
  TCP    10.0.1.16:80           10.0.1.11:57021        TIME_WAIT
  TCP    10.0.1.16:80           10.0.1.11:57022        ESTABLISHED

Should I be concerned that there are multiple ports being used on the iPad side? Should I somehow be "closing" the NSURLConnection after the read? I am using ARC.

Was it helpful?

Solution

When you close a port, it goes into TIME_WAIT for a little while to make sure that no more traffic will show up from the other end. It can live in that state for anywhere from seconds to minutes, depending on the implementation.

It'll eventually work out, but you're putting a great deal of overhead on the server and the client this way. If a lot of clients do this, you can definitely overwhelm the server.

You should not poll a server with a new connection every second. You should hold open one connection and listen on it for new messages from the sever (this is usually called a "long poll"). Or you can reuse the connection with HTTP/1.1. In any case, if you're going this deep into networking, you almost always will want to switch from NSURLConnection to AFNetworking which has better support for managing these kinds of connections.

OTHER TIPS

Actually NSURLConnection will be closed after the request is over. Are you sending these requests in another thread?

If yes, I believe that what you see in netstat is the open connections to the server. seems that each requests are taking longer that 1second so the second and third and forth connections are being stablished (and so on).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top