1分钟后iphone插座断开连接

时间:2012-08-23 06:36:48

标签: iphone ios sockets ios4 network-programming

我们有以下代码用于连接我们的服务器。这是iPhone App的一部分。

问题是recv(CFSocketGetNative(inSocketRef),& length,sizeof(length),0); 在60秒后,呼叫返回0。我们不是从服务器发送任何东西。我希望它等待数据或断开连接(服务器或客户端启动)。但是,它总是在60秒后返回。

我在这里做错了什么?


 void CallbackHandlerConnectionHandler(CFSocketRef inSocketRef, CFSocketCallBackType inType,
                 CFDataRef inAddress, const void *inData, void *inInfo)
{

ChatServerConnectionHandler *conHandler = (ChatServerConnectionHandler *)inInfo;

if([conHandler respondsToSelector:@selector(dataRecievedFromServer:)])
{
    int res = 0;
    SInt32 length;
    res = recv(CFSocketGetNative(inSocketRef), &length, sizeof(length), MSG_PEEK);


    if(0 >= res || (length < 0))
    {
        //Disconnect the connection, as some has occcured in the sockets..
        [conHandler performSelector:@selector(errorEncountered)];

        NSLog(@"Error occured in server!!");
        return;
    }


    printf ("good data")
}
}

-(BOOL) connectToServer:(NSString *)ipAddress Port:(int)portNumber
{       
connectionState = eConnectionEstablishInProgress;
self.threadStopped = NO;
CFRunLoopSourceRef source;

int sockfd;
struct sockaddr_in serv_addr;

struct hostent *host;
host = gethostbyname([ipAddress cStringUsingEncoding:NSUTF8StringEncoding]);

sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct timeval timeout;

if (sockfd < 0)
{  
    error("ERROR opening socket");
    connectionState = eNoConnection;
    return NO;
}


bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portNumber);
serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(serv_addr.sin_zero),8); 


/* Creates a CFSocket object for a pre-existing native socket */
CFSocketContext socketContext={0,self,NULL,NULL,NULL};
socketRef   = CFSocketCreateWithNative(kCFAllocatorDefault,  
                                       sockfd, 
                                       kCFSocketReadCallBack, 
                                       CallbackHandlerConnectionHandler, 
                                       &socketContext);

source = CFSocketCreateRunLoopSource(NULL, socketRef, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
CFRelease(source);
source = nil;

InstallSignalHandlers();

CFDataRef socketAddress;
socketAddress = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, (UInt8 *)&serv_addr, sizeof(serv_addr), kCFAllocatorNull);
CFSocketError result=CFSocketConnectToAddress(socketRef, socketAddress, 0);
CFRelease(socketAddress);

if(kCFSocketSuccess == result)
{
    //printf("Socket connection established");
    self.serverRunning = YES;
    connectionState = eConnectionEstablised;
    NSLog(@"\nCall Connection accepted callback.");
    if([delegate respondsToSelector:@selector(conectionEstablishedWithTheServer)])
    {
        [delegate performSelector:@selector(conectionEstablishedWithTheServer)];
    }

}
else
{
    //printf("Unable to create socket why???***");
    connectionState = eNoConnection;
    if(socketRef)
        CFRelease(socketRef);
    socketRef = nil;
    return NO;
}

while(FALSE == self.threadStopped)
{
    CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1, FALSE);
}
NSLog(@"\nOut of Server socket connection thread.");

self.serverRunning = NO;


if([delegate respondsToSelector:@selector(conectionFinishedWithServer)])
{
    [delegate performSelector:@selector(conectionFinishedWithServer)];
}

if(socketRef)
{
    if(CFSocketIsValid(socketRef))
        CFSocketInvalidate(socketRef);
}


return YES;
}

1 个答案:

答案 0 :(得分:1)

这看起来像服务器问题 - 或者更确切地说是功能:关闭非活动连接以保留足够的可用资源。

如果您没有编写服务器代码,请尝试联系作者。

你提到的数据包[FIN,ACK]是一个关闭你的套接字的数据包:你应该检查IP地址,但服务器很可能是这个数据包的发起者。