gpt4 book ai didi

objective-c - NSOutputStream 多次写入

转载 作者:搜寻专家 更新时间:2023-10-30 19:50:09 25 4
gpt4 key购买 nike

我正在尝试使用 NSStream 对象打开套接字,然后在套接字上进行读写,但我遇到了问题。

打开socket后不知道怎么写

这是我的做法

1) 首先打开套接字:

 NSURL *website = [NSURL URLWithString:urlStr]; 
if (!website) {
NSLog(@"%@ is not a valid URL");
return;
}
NSHost *host = [NSHost hostWithName:urlStr];
// iStream and oStream are instance variables
[NSStream getStreamsToHost:host port:6667 inputStream:&iStream
outputStream:&oStream];
[iStream retain];
[oStream retain];
[iStream setDelegate:self];
[oStream setDelegate:self];
[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[iStream open];
[oStream open];

2) 设置循环:

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
NSString *io;

if (theStream == iStream) io = @">>";
else io = @"<<";
NSLog(@"stream : %@",theStream);

NSString *event;
switch (streamEvent)
{
case NSStreamEventNone:
event = @"NSStreamEventNone";
break;
case NSStreamEventOpenCompleted:
event = @"NSStreamEventOpenCompleted";
break;
case NSStreamEventHasBytesAvailable:{
event = @"NSStreamEventHasBytesAvailables";
if (theStream == iStream)
{
if(!_data) {
_data = [[NSMutableData data] retain];
}
uint8_t buf[1024];
unsigned int len = 0;
len = [iStream read:buf maxLength:1024];
NSLog(@"Lenght data read : %d", len);
if(len) {
NSData * dataReceived= [[NSString stringWithFormat:@"%s\n", (char *)buf] dataUsingEncoding:NSUTF8StringEncoding];
NSString *s = [[NSString alloc] initWithData:dataReceived encoding:NSUTF8StringEncoding];
NSLog(@"Received _data: \"%@\"\n",s);



} else {
NSLog(@"nothing to read!");
}
}else {
NSLog(@"Not the good stream");
}

break;
}

case NSStreamEventHasSpaceAvailable:{
event = @"NSStreamEventHasSpaceAvailable";
if (theStream == oStream )
{
if(isConnexionCommandSent == NO){
[self sendCommand:@"My connection command"];
isConnexionCommandSent = YES;
}

}

break;
}

case NSStreamEventErrorOccurred:
event = @"NSStreamEventErrorOccurred";
NSError *theError = [theStream streamError];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[theError localizedDescription]
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
break;
case NSStreamEventEndEncountered:
event = @"NSStreamEventEndEncountered";

break;
default:
event = @"** Unknown";
}

NSLog(@"%@ : %@", io, event);
}

3) 然后我有一个在我触摸按钮时调用的函数

- (IBAction)join:(id)sender{
if([oStream hasSpaceAvailable]){
NSLog(@"iStream Status : %d",[iStream streamStatus]);
NSLog(@"oStream Status : %d",[oStream streamStatus]);
[self sendCommand:@"join"];

}else{
NSLog(@"Error command can't be sent");
}
}

-(void) sendCommand:(NSString *) command{
NSLog(@"space : %d",[oStream hasSpaceAvailable]);
if ([oStream hasSpaceAvailable])
{
NSLog(@"Command writen : %s\n",[command cStringUsingEncoding:NSASCIIStringEncoding]);
NSInteger i=[oStream write:(const uint8_t *)[command cStringUsingEncoding:NSASCIIStringEncoding] maxLength:(NSInteger)[command lengthOfBytesUsingEncoding:NSASCIIStringEncoding]];
if (i<0)
{
NSLog(@"erreur lors de l'envoi, status:%i, erreur:%@", [oStream streamStatus], [oStream streamError]);
}
isReadyToSend = NO;
}

else
{
NSLog(@"impossible d'envoyer, status:%i, erreur:%@", [oStream streamStatus], [oStream streamError]);
}
}

但问题是,当调用函数 join 时,一切正常,但服务器什么也没收到......

开启

NSInteger i=[oStream write:(const uint8_t *)[command cStringUsingEncoding:NSASCIIStringEncoding] maxLength:(NSInteger)[command lengthOfBytesUsingEncoding:NSASCIIStringEncoding]];

i > 0,所以我假设写入顺利,但在服务器上没有收到任何东西......我不知道为什么......

你能帮帮我吗?

最佳答案

嘿@Ptitaw 看this post .我相信您可能会在那里找到您的答案和一种更简单的方法来自动连接和访问所有事件(阅读、写作等)希望我能帮上忙:)

关于objective-c - NSOutputStream 多次写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/802096/

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