Socket Programing For IOS
Socket Programing For IOS
You can use the CFStream API to establish a socket connection. The NSStream class does not
support connecting to a remote host on iOS. CFStream does support this behavior, however, and
once you have created your streams with the CFStream API, you can cast your CFStreams to
NSStreams.
- (void)connection:(NSString*)serviceName forIpAddress:(NSString *)ipAddress
forPort:(NSString *)portNo
{
if(inputStream && outputStream)
[self close];
NSString *urlString = [NSString stringWithFormat:@"http://%@", ipAddress];
NSURL *website = [NSURL URLWithString:urlString];
if (!website) {
NSLog(@"%@ is not a valid URL", website);
}
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)[website host], [portNo
intValue], &readStream, &writeStream);
CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket,
kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket,
kCFBooleanTrue);
inputStream = (NSInputStream *)readStream;
outputStream = (NSOutputStream *)writeStream;
[self open];
}
- (void)open
{
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
}
Once you have cast the CFStreams to NSStreams, set the delegate, schedule the stream on a run
loop, and open the stream as usual. The delegate should begin to receive stream-event messages
(stream:handleEvent:). This method is described below in 'Reading From Socket Connection'
section.
break;
case NSStreamEventErrorOccurred:
event = @"NSStreamEventErrorOccurred";
[self close];
break;
case NSStreamEventEndEncountered:
event = @"NSStreamEventEndEncountered";
[self close];
break;
default:
event = @"Unknown";
break;
}
NSLog(@"event------%@",event);
}
When an NSInputStream object reaches the end of a stream, it sends the delegate a
NSStreamEventEndEncountered event in a stream:handleEvent:message. The delegate should
dispose of the object by doing the mirror-opposite of what it did to prepare the object. Also we
should do When the NSInputStream object experiences errors processing the stream.
- (void)close
{
[inputStream close];
[outputStream close];
[inputStream removeFromRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[outputStream removeFromRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[inputStream setDelegate:nil];
[outputStream setDelegate:nil];
inputStream = nil;
outputStream = nil;
}
- (void)dataSending:(NSString*)data
{
if(outputStream)
{
if(![outputStream hasSpaceAvailable])
return;
NSData *_data=[data dataUsingEncoding:NSUTF8StringEncoding];
int data_len = [_data length];
uint8_t *readBytes = (uint8_t *)[_data bytes];
int byteIndex=0;
unsigned int len=0;
while (TRUE)
{
len = ((data_len - byteIndex >= 40960) ?
40960 : (data_len-byteIndex));
if(len==0)
break;
uint8_t buf[len];
(void)memcpy(buf, readBytes, len);
len = [outputStream write:(const uint8_t *)buf maxLength:len];
byteIndex += len;
readBytes += len;
}
NSLog(@"Sent data----------------------%@",data);
}
}