这是我的代码:
- (void)connecToServerUsingCFStream:(NSString *)urlStr portNo: (uint) portNo{
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)urlStr, portNo, &_readStream, &_writeStream);
if (_readStream&&_writeStream) {
CFReadStreamSetProperty(_readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
CFWriteStreamSetProperty(_writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
iStream = (NSInputStream *)_readStream;
[iStream retain];
[iStream setDelegate:self];
[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[iStream open];
oStream = (NSOutputStream *)_writeStream;
[oStream retain];
[oStream setDelegate:self];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[oStream open];
}
} 我称之为此方法,但无法连接到主机,我的目的是连接到主机,然后发送数据包。
- (void) sendLoginPacket{
Byte frame_header = 0x68;
unsigned short len = 8;
unsigned short control = 128;
unsigned char adress = 100;
unsigned char ANF = 1;
unsigned char Fn = 1;
Byte byte[8];
byte[0] = frame_header;
byte[1] = len&0xff;
byte[2] = (len>>8)&0xff;
byte[3] = control&0xff;
byte[4] = (control>>8)&0xff;
byte[5] = adress;
byte[6] = ANF;
byte[7] = Fn;
[oStream write:byte maxLength:sizeof(byte)];}
当我调用此方法时,主机可以连接,但是可以获取数据包,如果我再次调用此方法,主机可以接收数据包吗?