Posts

Post marked as solved
8 Replies
6.4k Views
I want to see Apple’s TCP sample code, When I saw Apple’s UDP sample code - UDPEcho. But I does’t found it.Do you know Apple’s TCP sample code like UDPEcho?
Posted
by itang.
Last updated
.
Post marked as solved
1 Replies
475 Views
Hi experts.I created a socket server and when the server accept a connection, I associate the CFSocketNativeHandle with NSInputStream and NSOutputStream.I implement method - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode.I think the connection is closed by the client, if I received event NSStreamEventHasBytesAvailable and the length of data i read is 0.like the following codes:is it right ? if not, how do I know the connection is closed by the client?thanks.- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode { switch (eventCode) { case NSStreamEventOpenCompleted: break; case NSStreamEventHasBytesAvailable: { NSMutableData *data = [NSMutableData data]; uint8_t buffer[1024]; NSInputStream *inputStream = (NSInputStream *)aStream; while (inputStream.hasBytesAvailable) { NSInteger readSize = [inputStream read:buffer maxLength:kBufferSize]; if (readSize > 0) { [data appendBytes:buffer length:readSize]; } else if (-1 == readSize) { NSError *error = aStream.streamError; if (error) { // ... } } } if(0 == data.length) {// the connection is closed by the client } } break; // ... default: break; } }
Posted
by itang.
Last updated
.
Post marked as solved
4 Replies
1.6k Views
When my iPhone 6(iOS 12.2(16E227)) uses the network that is shared by my iMac, the following method testResolution print: ipAddress: 0.0.0.0If not use the network shared by iMac, the following method can resolve domain successfully.- (void)testResolution { NSString *hostName = @"www.apple.com"; CFHostRef host = CFHostCreateWithName(kCFAllocatorDefault, (__bridge CFStringRef)hostName); CFStreamError error; CFHostStartInfoResolution(host, kCFHostAddresses, &error); if (0 == error.error) { CFArrayRef addressArray = CFHostGetAddressing(host, NULL); if (addressArray && CFArrayGetCount(addressArray) > 0) { NSArray *sockaddrArray = (__bridge NSArray *)addressArray; for (NSData *sockaddrData in sockaddrArray) { struct sockaddr_in *sa_in = (struct sockaddr_in *)[sockaddrData bytes]; const char *ipAddress = inet_ntoa(sa_in->sin_addr); printf("ipAddress: %s\n", ipAddress); } } } CFRelease(host); }
Posted
by itang.
Last updated
.