client socket disconnect as soon as connected

actually it works well until last night...


this morning I found client will disconnect to server as soon as connected.


I use GCDAsyncSocket public class, the "didConnectToHost" already be called by system, that means it already connected with server. but immerdiately the client will disconnct this connection. After tracing, I found the function "CFReadStreamCallback" and "CFWriteStreamCallback" is called as soon as the connection built, but the type is "kCFStreamEventErrorOccurred", and the error information is "The operation couldn’t be completed. Socket is not connected".


I deleted all the write and read operation on client, and the write operation on server from my own code. to make sure there is not service message between client and server. but the result is same, the client disconnect the connection as soon as it connected.


And if I ignore the "kCFStreamEventErrorOccurred" message, and shield the disconnect operation on function "CFReadStreamCallback" and "CFWriteStreamCallback", everything work well. the message between client and server can communicated normal.


So, who can kindly tell me why? what make the message "kCFStreamEventErrorOccurred" occured?


thanks!

Is your "socket" object going out of scope? In other words, is it a class level variable?

I don't think so... the code is as below:


static void CFReadStreamCallback (CFReadStreamRef stream, CFStreamEventType type, void *pInfo)

{

GCDAsyncSocket *asyncSocket = (__bridge GCDAsyncSocket *)pInfo;


switch(type)

{

case kCFStreamEventHasBytesAvailable:

{

dispatch_async(asyncSocket->socketQueue, ^{ @autoreleasepool {


LogCVerbose(@"CFReadStreamCallback - HasBytesAvailable");


if (asyncSocket->readStream != stream)

return_from_block;


if ((asyncSocket->flags & kStartingReadTLS) && (asyncSocket->flags & kStartingWriteTLS))

{

if (CFReadStreamHasBytesAvailable(asyncSocket->readStream))

{

asyncSocket->flags |= kSecureSocketHasBytesAvailable;

[asyncSocket cf_finishSSLHandshake];

}

}

else

{

asyncSocket->flags |= kSecureSocketHasBytesAvailable;

[asyncSocket doReadData];

}

}});


break;

}

default:

{

NSError *error = (__bridge_transfer NSError *)CFReadStreamCopyError(stream);


if (error == nil && type == kCFStreamEventEndEncountered)

{

error = [asyncSocket connectionClosedError];

}


dispatch_async(asyncSocket->socketQueue, ^{ @autoreleasepool {


LogCVerbose(@"CFReadStreamCallback - Other");


if (asyncSocket->readStream != stream)

return_from_block;


if ((asyncSocket->flags & kStartingReadTLS) && (asyncSocket->flags & kStartingWriteTLS))

{

[asyncSocket cf_abortSSLHandshake:error];

}

else

{

[asyncSocket closeWithError:error]; //here disconnect the connection

}

}});


break;

}

}


}

If you're going to paste code, please select paste it in, then highlight it and select the <> symbol in the toolbar so that it's formatted as code. Much easier to read.


Where do you CREATE the GCDAsyncSocket variable? That's the thing that has to be a class level variable.

becuase it is client, the socket object is a unique object in the program. I think it won't going out of scope.

Then I can't help you.

anyway, thank you

client socket disconnect as soon as connected
 
 
Q