Skip navigation.
 
mlRe: Cocoa-dev Digest, Vol 3, Issue 843
FROM : Jason Rusoff
DATE : Fri Jun 30 20:25:10 2006

>I want to build my first network application.  This application will be on
>several computers at my workplace.  I want to send NSData objects between
>these computers.  Each terminal will create NSData objects and send them to
>one central computer for further processing.



Here are some snippets to get started.  Don't complain if they don't compile :-) This gives you the basic stuff you need to handle asynch data downloads.
JR

// Start up the connection

NSString * updateURLStr = @"http://somesite.com";

NSURL* theURL = [NSURL  URLWithString: [updateURLStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
   NSURLRequest *theRequest=[NSURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval: kTimeOutInterval];
                           
                 
   myConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

// These methods are called for the connection delegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    // release the data object and connection
   [self cleanUpConnection];

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
   [self setProgressMax: [response expectedContentLength]];  // set progress bar max....
   myConnection = [connection retain];
   
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // append the new data to my file handle
   if  (myFileHandle != nil)
   {
       @try{
           [myFileHandle writeData:data];
       }
   
       @catch(NSException *exception)
       {
           [self showDiskErr];
       }                
       [self setProgressValue: [myFileHandle offsetInFile]];
   }
}

// We done downloading, finish handling the data

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   [self cleanUpConnection];
// do something with the file that holds the downloaded data

   [[NSWorkspace sharedWorkspace] openFile:mySavedFilePath];
   // Let caller  know that it completed successfully
   [downloadCaller downloadFinished:YES];
   
   
}

// _________________________________________________________________
// We done downloading, finish handling the data

- (void)cleanUpConnection
{
   [myFileHandle closeFile];
   [myFileHandle release];
   myFileHandle = nil;
   [myConnection release];
   myConnection = nil;
}

// _________________________________________________________________

- (IBAction) cancelDownload:(id) sender
{
   [myConnection cancel];    
   [self cleanUpConnection];
   [[NSFileManager defaultManager] removeFileAtPath:mySavedFilePath handler:nil];
   [[self window] close];
   
   [downloadCaller downloadFinished:NO];
}

Related mailsAuthorDate
No related mails found.