3 Replies Latest reply: Dec 3, 2012 4:16 PM by etresoft
Loreman78 Level 1 Level 1 (0 points)

I have created a TCP server that lisen for incoming connection on port 42000. The code creates a sockect (startServer) and when a client try to connect to the server then the AcceptCallback is raised. I use AcceptCallback to handle input and output stream. Now... what I can do to close and stop the server (and also stop the listening for incoming connection) ?

 

Here below my code:

 

- (void)startServer

{

        /* Create the server socket as a TCP IPv4 socket and set a callback */

        /* for calls to the socket's lower-level accept() function */

        TCPServer = CFSocketCreate(NULL, PF_INET, SOCK_STREAM, IPPROTO_TCP,

                                   kCFSocketAcceptCallBack, (CFSocketCallBack)AcceptCallBack, NULL);

        if (TCPServer == NULL)

            return;

       

        /* Set the port and address we want to listen on */

        struct sockaddr_in addr;

        memset(&addr, 0, sizeof(addr));

        addr.sin_len = sizeof(addr);

        addr.sin_family = AF_INET;

        addr.sin_port = htons(42000);

        addr.sin_addr.s_addr = htonl(INADDR_ANY);

       

        NSData *address = [ NSData dataWithBytes: &addr length: sizeof(addr) ];

        if (CFSocketSetAddress(TCPServer, (__bridge CFDataRef) address) != kCFSocketSuccess) {

            fprintf(stderr, "CFSocketSetAddress() failed\n");

            CFRelease(TCPServer);

            return;

        }

       

        CFRunLoopSourceRef sourceRef = CFSocketCreateRunLoopSource(kCFAllocatorDefault, TCPServer, 0);

        CFRunLoopAddSource(CFRunLoopGetCurrent(), sourceRef, kCFRunLoopCommonModes);

        CFRelease(sourceRef);

       

        printf("Server -> Socket listening on port %d\n", 42000);

        info.text = @"Waiting for a connection...";

       

}

 

//This routine is static!

void AcceptCallBack(

                    CFSocketRef socket,

                    CFSocketCallBackType type,

                    CFDataRef address,

                    const void *data,

                    void *info)

{

    CFReadStreamRef readStream = NULL;

    CFWriteStreamRef writeStream = NULL;

 

    /* The native socket, used for various operations */

    sock = *(CFSocketNativeHandle *) data;

   

    /* Create the read and write streams for the socket */

    CFStreamCreatePairWithSocket(kCFAllocatorDefault, sock, &readStream, &writeStream);

   

    if (!readStream || !writeStream) {

        close(sock);

        fprintf(stderr, "CFStreamCreatePairWithSocket() failed\n");

        return;

    }

   

    CFReadStreamOpen(readStream);

    CFWriteStreamOpen(writeStream);

 

    inputStream = (__bridge NSInputStream *)readStream;

    outputStream = (__bridge NSOutputStream *)writeStream;

       

}

 

- (void) closeTCPServer

{

    // ????????

}

 

Thank you!


iPhone 4, iOS 6
  • 1. Re: TCP server problem in my iPhone App
    etresoft Level 7 Level 7 (24,270 points)

    There are similar callbacks for handling I/O from the streams. When they close, the socket is closed.

  • 2. Re: TCP server problem in my iPhone App
    Loreman78 Level 1 Level 1 (0 points)

    In a function I have tryed to close the input and output stream but the client can again to connect to the server... the server is not close and I don't understand why... I suppose if the server is close, the client can't connect to the server... or receive a connection error...

     

    I write here the complete callback ... (If you look at the last line of this routine you can see I use a notification to handle a global variables inputstream and output stream)

     

    void AcceptCallBack(

                        CFSocketRef socket,

                        CFSocketCallBackType type,

                        CFDataRef address,

                        const void *data,

                        void *info)

    {

        CFReadStreamRef readStream = NULL;

        CFWriteStreamRef writeStream = NULL;

     

        /* The native socket, used for various operations */

        sock = *(CFSocketNativeHandle *) data;

       

        /* Create the read and write streams for the socket */

        CFStreamCreatePairWithSocket(kCFAllocatorDefault, sock, &readStream, &writeStream);

       

        if (!readStream || !writeStream) {

            close(sock);

            fprintf(stderr, "CFStreamCreatePairWithSocket() failed\n");

            return;

        }

       

        CFReadStreamOpen(readStream);

        CFWriteStreamOpen(writeStream);

     

        inputStream = (__bridge NSInputStream *)readStream;

        outputStream = (__bridge NSOutputStream *)writeStream;

       

        //I use a notification so I can open a input or output stream...         [[NSNotificationCenter defaultCenter] postNotificationName:@"acceptcallbackinfo" object:nil userInfo:nil];

       

    }

     

    //Notification: Now I can create a inputstream or output stream and use stream callback to send and receive the message from the client. To close the server I have tried to close inputStream and outputStream but the client can agai connect to the server..

     

    - (void)acceptcallbackInfo:(NSNotification *)notification

    {

        NSLog(@"Server -> Connection accepted\n");

     

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

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

       

        [inputStream setDelegate:self];

        //[outputStream setDelegate:self];

       

        [inputStream open];

        [outputStream open];

     

    }

     

    - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {

     

    .... other code

     

    }

     

    Please help me if you have some suggestion or code that explai me what can I do... thank you very much!

  • 3. Re: TCP server problem in my iPhone App
    etresoft Level 7 Level 7 (24,270 points)

    Loreman78 wrote:

     

    In a function I have tryed to close the input and output stream but the client can again to connect to the server... the server is not close and I don't understand why... I suppose if the server is close, the client can't connect to the server... or receive a connection error...

    You are talking about two different things here. The listening port just sits there and waits for a connection. When that connection occurs, your accept callback fires to handle the connection. Until you close the listening socket, clients can continue to connect to the server. After accepting the connection, the listening socket and the connected socket have nothing more to do with each other.