簡易範例:
- (void)sentAMessageTo:(NSString *)server_ip port:(int)port message:(NSString *)message
{
NSInputStream *inputStream;
NSOutputStream *outputStream;
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)server_ip, port, &readStream, &writeStream);
inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;
[inputStream open];
[outputStream open];
NSData *data = [[NSData alloc] initWithData:[message dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
[outputStream close];
[inputStream close];
}
此例應該算使用 ARC 方案吧,沒去管記憶體。
沒有留言:
張貼留言