2

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

问题是 recv(CFSocketGetNative(inSocketRef), &length, sizeof(length), 0); call 在 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;
}
4

1 回答 1

1

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

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

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

于 2012-08-23T08:12:41.743 回答