IPhone Network ProgrammingInitial ExperienceChat programThis document describes how to implement instance development.Chat programFor more information, see the content first. First, use Xcode to name a common View-based application project named Network.
Use network communication flow
The simplest way to use sockets to communicate over the network is to use the NSStream class, which is an abstract class that represents a stream. You can use it to read and write data, it can be used in memory, files, or networks. With the NSStream class, you can write data to the server or read data from the server.
On Mac OS X, you can use the NSHost and NSStream objects to establish a connection to the server, for example:
- NSInputStream *iStream;
- NSOutputStream *oStream;
- uint portNo = 500;
- NSURL *website = [NSURL URLWithString:urlStr];
- NSHost *host = [NSHost hostWithName:[website host]];
- [NSStream getStreamsToHost:host
- port:portNo
- inputStream:&iStream
- outputStream:&oStream];
The NSStream class has a method getStreamsToHost: port: inputStream: outputStream:, which creates an input and output stream to the server, but the problem is that iPhone OS does not support getStreamsToHost: port: inputStream: outputStream: therefore, the above Code cannot run in the iPhone application.
To solve this problem, you can add a category to the existing NSStream class and replace the functions provided by the getStreamsToHost: port: inputStream: outputStream: method. Right-click the Classes in Xcode and add the NSStreamAdditions. m file. In the NSStreamAdditions. h file, add the following code:
- #import
- @interface NSStream (MyAdditions)
- + (void)getStreamsToHostNamed:(NSString *)hostName
- port:(NSInteger)port
- inputStream:(NSInputStream **)inputStreamPtr
- outputStream:(NSOutputStream **)outputStreamPtr;
- @end
Add the following code to the NSStreamAdditions file:
- #import "NSStreamAdditions.h"
- @implementation NSStream (MyAdditions)
- + (void)getStreamsToHostNamed:(NSString *)hostName
- port:(NSInteger)port
- inputStream:(NSInputStream **)inputStreamPtr
- outputStream:(NSOutputStream **)outputStreamPtr
- {
- CFReadStreamRef readStream;
- CFWriteStreamRef writeStream;
- assert(hostName != nil);
- assert( (port > 0) && (port < 65536) );
- assert( (inputStreamPtr != NULL) || (outputStreamPtr != NULL) );
- readStream = NULL;
- writeStream = NULL;
- CFStreamCreatePairWithSocketToHost(
- NULL,
- (CFStringRef) hostName,
- port,
- ((inputStreamPtr != nil) ? &readStream : NULL),
- ((outputStreamPtr != nil) ? &writeStream : NULL)
- );
- if (inputStreamPtr != NULL) {
- *inputStreamPtr = [NSMakeCollectable(readStream) autorelease];
- }
- if (outputStreamPtr != NULL) {
- *outputStreamPtr = [NSMakeCollectable(writeStream) autorelease];
- }
- }
- @end
The above Code adds a getStreamsToHostNamed: port: inputStream: outputStream: method to the NSStream class. Now you can use this method in your iPhone application to connect to the server using the TCP protocol.
In the NetworkViewController. m file, insert the following code:
- #import "NetworkViewController.h"
- #import "NSStreamAdditions.h"
- @implementation NetworkViewController
- NSMutableData *data;
- NSInputStream *iStream;
- NSOutputStream *oStream;
Define connectToServerUsingStream: portNo: Method to connect to the server, and then create input and output stream objects:
- -(void) connectToServerUsingStream:(NSString *)urlStr
- portNo: (uint) portNo {
- if (![urlStr isEqualToString:@""]) {
- NSURL *website = [NSURL URLWithString:urlStr];
- if (!website) {
- NSLog(@"%@ is not a valid URL");
- return;
- } else {
- [NSStream getStreamsToHostNamed:urlStr
- port:portNo
- inputStream:&iStream
- outputStream:&oStream];
- [iStream retain];
- [oStream retain];
- [iStream setDelegate:self];
- [oStream setDelegate:self];
-
- [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
- [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
- [oStream open];
- [iStream open];
- }
- }
- }
In a running loop, you can schedule input and output streams to receive events to avoid blocking.
Summary:IPhone Network ProgrammingInitial ExperienceChat programI hope this article will help you with the introduction of instance development!