Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Cocoa-dev Digest, Vol 3, Issue 843



>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];
}

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.