English 中文(简体)
IOS 的套扣接断
原标题:Socket disconnection in IOS

在 IOS Socket 程序方面我需要一些帮助。 我已经在 IOS 和一个 Java 服务器之间成功实施了 TCP Socket 连接, 但我发现在插座失效后无法重新连接( 我切断了网络并重新连接了网络 ) 。

我用来打开溪流的代码是:

- (void)initCommunication{
@try {
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"XXX.XXX.XXX.XXX", 4454, &readStream, &writeStream);

    inputStream = (__bridge NSInputStream *)readStream;
    outputStream = (__bridge NSOutputStream *)writeStream;

    //Asignamos los delegates
    inputStream.delegate = self;
    outputStream.delegate = self;  

    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [inputStream open];
    [outputStream open];
    connected = true;
    }
}
@catch (NSException *exception) {
    connected = false;
    NSLog(@"Error connecting socket: %@", exception.reason);
    [self checkConnection];
}
}

当我收到NSStreamEventEventError Ocrcrod 或NSSStreamEventEnd对质时, 我试图关闭连接并重新连接。

我关闭它与:

-(void) close{

[inputStream close];
[inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream close];
[outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream setDelegate:nil];
inputStream = nil;
[outputStream setDelegate:nil];
outputStream = nil;

connected = false;
}

问题在于套座不再连接... 我正在考虑使用小套子或可可套子来查看我能否重新连接套座... 您看到任何执行问题吗? 您建议使用小套子或可可套子吗?

提前感谢!

最佳回答

最后,我用CocoAsyncSockets < a href="https://github.com/robbiehanson/CocoaAsyncSocket" rel=“nofollow”来实施,而且与这个插座重新连接非常容易。

首先我初始化了套接字对象 :

socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

要连接我使用 :

NSError *err = nil;
if ([socket connectToHost:@"XXX.XXX.XXX.XXX" onPort:YYYY error:&err])
{
NSLog(@"Connection performed!");
}
else
{
NSLog(@"Unable to connect: %@", err);
}

然后,你可以使用代表 做重新连接 每当它失败了:

- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
NSLog(@"Socket:DidConnectToHost: %@ Port: %hu", host, port);

connected = YES;
    [sock readDataWithTimeout:-1 tag:0];
}

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
{
NSLog(@"SocketDidDisconnect:WithError: %@", err);   
    connected = NO;
    //We will try to reconnect
    [self checkConnection];
}

- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    [self processReceivedData:data];
    [sock readDataWithTimeout:-1 tag:0];
}  

I found very easy to use that library. I hope that helps anyone. Thanks a lot.

问题回答

暂无回答




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签