Skip navigation

TCP server problem in my iPhone App

618 Views 3 Replies Latest reply: Dec 3, 2012 4:16 PM by etresoft RSS
Loreman78 Calculating status...
Currently Being Moderated
Dec 3, 2012 2:19 PM

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
  • etresoft Level 7 Level 7 (23,900 points)
    Currently Being Moderated
    Dec 3, 2012 2:22 PM (in response to Loreman78)

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

  • etresoft Level 7 Level 7 (23,900 points)
    Currently Being Moderated
    Dec 3, 2012 4:16 PM (in response to Loreman78)

    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.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (1)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.