gpt4 book ai didi

iOS 到 linux 使用套接字连接

转载 作者:太空宇宙 更新时间:2023-11-04 12:49:36 27 4
gpt4 key购买 nike

我想使用套接字将 iphone 连接到我的 linux 机器。我在 linux 上运行服务器程序并将套接字保持在监听模式并尝试从我的 iphone 发送字符串。但无法连接到 linux 机器。我试过 CFStream api。对于连接,我使用端口 3000。我的代码如下:

- (void)viewDidLoad {

[super viewDidLoad];

NSLog(@"view did load");
data = [[NSMutableData alloc] init];
outputStream = [[NSOutputStream alloc] initToMemory];
inputStream = [[NSInputStream alloc] init];
[self initNetworkCommunication];

[self sendString:@"Hello World\n"]

}
- (void)initNetworkCommunication {

CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.1.62",3000, &readStream, &writeStream);

inputStream = (__bridge NSInputStream *)(readStream); // ivar
[inputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];

outputStream = ( __bridge NSOutputStream *)(writeStream); // ivar
[outputStream setDelegate:self];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream open];
}

-(void)sendString:(NSString *)string {

NSLog(@"data string:%@",string);
if(!CFWriteStreamOpen(writeStream)){
NSLog(@"Error, writeStream not open");

//[outputStream close];
}
NSLog(@"Status of outputStream: %i", [outputStream streamStatus]);

NSData *data = [[NSData alloc] initWithData:[string dataUsingEncoding:NSASCIIStringEncoding]];

[outputStream write:[data bytes] maxLength:[data length]];
}
-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {

NSLog(@"thestream:%@",theStream);
NSLog(@"stream event %lu", (unsigned long)streamEvent);
BOOL byteIndex = nil;

switch (streamEvent) {
case NSStreamEventOpenCompleted:
NSLog(@"Stream opened");
break;
case NSStreamEventHasSpaceAvailable: {
uint8_t *readBytes = (uint8_t *)[data mutableBytes];
readBytes += byteIndex; // ivar
int data_len = [data length];
unsigned int len = ((data_len - byteIndex >= 1024) ? 1024 : (data_len - byteIndex));
uint8_t buf [len];
(void)memcpy(buf, readBytes, len);
len = [(NSOutputStream *)theStream write:(const uint8_t *)buf maxLength:len];
NSLog(@"Sending buffer of len: %d", len);
byteIndex += len;
break;
}
case NSStreamEventHasBytesAvailable:
NSLog(@"the stream:%@, inputStream:%@",theStream,inputStream);
if (theStream == inputStream) {
uint8_t buffer[1024];
int len;

while ([inputStream hasBytesAvailable]) {
len = [inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0) {
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];

if (nil != output) {
NSLog(@"server said: %@", output);
}
}
}
[self sendString:@"Another Test"];
}
break;

case NSStreamEventErrorOccurred:
theError = [theStream streamError];
NSString * event = [[NSString alloc]initWithFormat:@"NSStreamEventErrorOccurred %@ ",theError];
NSLog(@"Can not connect to the host!:%@",event);
break;
// case NSStreamEventEndEncountered:
// NSLog(@"Closing stream...");
// [theStream close];
// [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
// //[theStream release];
// theStream = nil;
// break;
// default:

// NSLog(@"Unknown event");

}
}

最佳答案

我得到了解决方案。我犯了一些错误。在 View 中注释两行确实加载并在 initNetworkCommunication 中添加两行解决了问题更正代码如下。

- (void)viewDidLoad {

[super viewDidLoad];

NSLog(@"view did load");
data = [[NSMutableData alloc] init];
// outputStream = [[NSOutputStream alloc] initToMemory]; // Commented this two lines
// inputStream = [[NSInputStream alloc] init];
[self initNetworkCommunication];

[self sendString:@"Hello World\n"]

}
- (void)initNetworkCommunication {



//My program stuck at CFStreamCreatePairWithSocketToHost. I declared this two objects globally but it didn't work then I declared it here now it works
CFReadStreamRef readStream ;
CFWriteStreamRef writeStream ;

CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.1.62",3000, &readStream, &writeStream);

inputStream = (__bridge NSInputStream *)(readStream); // ivar
[inputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];

outputStream = ( __bridge NSOutputStream *)(writeStream); // ivar
[outputStream setDelegate:self];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream open];
}

关于iOS 到 linux 使用套接字连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37675205/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com