안드로이드로 연결하려면 NSInputStream
& NSOutputStream
을 사용해야합니다. 그것은 간단한 작업이 아닙니다.
@property (nonatomic, strong, readwrite) NSInputStream *inputStream;
@property (nonatomic, strong, readwrite) NSOutputStream *outputStream;
그런 다음이 방법을 추가
첫째,
- (void)initNetworkCommunication {
uint portNo = 5555;
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"localhost", portNo, &readStream, &writeStream);
inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
}
당신은 스트림을 시작하려면이 메서드를 호출해야합니다.
그리고이 스트림에서 무엇인가를 보내려면 여기에 예가 있습니다.
- (IBAction)photoClick:(id)sender {
NSData *newData = UIImagePNGRepresentation([UIImage imageNamed:@"s.jpg"]);
int index = 0;
int totalLen = [newData length];
uint8_t buffer[1024];
uint8_t *readBytes = (uint8_t *)[newData bytes];
while (index < totalLen) {
if ([outputStream hasSpaceAvailable]) {
NSLog(@"Sending");
int indexLen = (1024>(totalLen-index))?(totalLen-index):1024;
(void)memcpy(buffer, readBytes, indexLen);
int written = [outputStream write:buffer maxLength:indexLen];
if (written < 0) {
break;
}
index += written;
readBytes += written;
}
}
}
이렇게하면 안됩니다. – Petar