Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

TCP server problem in my iPhone App

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, (__bridgeCFDataRef) 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

Posted on Dec 3, 2012 2:19 PM

Reply
3 replies

Dec 3, 2012 2:37 PM in response to etresoft

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... [[NSNotificationCenterdefaultCenter] postNotificationName:@"acceptcallbackinfo"object:niluserInfo: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");


[inputStreamscheduleInRunLoop:[NSRunLoopcurrentRunLoop] forMode:NSDefaultRunLoopMode];

[outputStreamscheduleInRunLoop:[NSRunLoopcurrentRunLoop] forMode:NSDefaultRunLoopMode];

[inputStreamsetDelegate:self];

//[outputStream setDelegate:self];

[inputStreamopen];

[outputStreamopen];


}


- (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!

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.

TCP server problem in my iPhone App

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.