Re: create a server listening on a specific port
Re: create a server listening on a specific port
- Subject: Re: create a server listening on a specific port
- From: joao da silva <email@hidden>
- Date: Thu, 24 Mar 2005 23:09:26 -0500
You need something like this:
#define THE_PORT_NUMBER 8770
- (
BOOL) createListeningSocket {
// Here, create the socket from socket calls, then set up an NSFileHandle with that to listen for incoming connections.
fdForListening =
0;
struct sockaddr_in serverAddress;
// In order to use NSFileHandle's acceptConnectionInBackgroundAndNotify method, we need to create a
// file descriptor that is itself a socket, bind that socket, and then set it up for listening. At this
// point, it's ready to be handed off to acceptConnectionInBackgroundAndNotify.
if ((fdForListening = socket(AF_INET, SOCK_STREAM,
0)) >
0) {
memset (&serverAddress,
0,
sizeof (serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl (INADDR_ANY);
// To allow the kernel to choose a random port number pass in 0 for the port.
serverAddress.sin_port = htons (THE_PORT_NUMBER);
bzero(&(serverAddress.sin_zero),
8);
/* zero the rest of the struct */
// let us re-use it if kernel still hanging on to it
int val =
1;
setsockopt (fdForListening, SOL_SOCKET, SO_REUSEADDR, &val,
sizeof val);
if (bind (fdForListening, (
struct sockaddr *)&serverAddress,
sizeof (serverAddress)) <
0) {
close (fdForListening);
fdForListening =
0;
return NO;
}
// Once we're here, we know bind must have returned, so we can start the listen
if (listen (fdForListening,
1) ==
0) {
listeningSocket = [[NSFileHandle alloc] initWithFileDescriptor: fdForListening closeOnDealloc:
YES];
}
}
[[NSNotificationCenter defaultCenter] addObserver:
self selector:
@selector (connectionReceived:) name: NSFileHandleConnectionAcceptedNotification object: listeningSocket];
return YES;
}
// This object is also listening for notifications from its NSFileHandle. When an incoming connection is seen
// by the listeningSocket object, we get the NSFileHandle representing the near end of the connection.
- (
void) connectionReceived:(NSNotification *)aNotification {
[listeningSocket acceptConnectionInBackgroundAndNotify];
NSFileHandle * incomingConnection = [[aNotification userInfo] objectForKey: NSFileHandleNotificationFileHandleItem];
[NSThread detachNewThreadSelector:
@selector (serveConnection:) toTarget:
self withObject: incomingConnection];
}
- (
void) serveConnection: (NSFileHandle*) _connection {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
.....
[pool release];
}
On Mar 23, 2005, at 2:33 AM, Hanspeter Spalinger wrote:
Hi there again,
I try to create a very small and simple server listening on a public port (port number > 49151).
I tried some Cocoa classes like NSNetService, NSSocketPort, NSConnection, but all of them create services already connected to a local port (102x)
How do I create a server listening for connections? I think this should be simple, but dind't found something in the archives. Can someone provide a link for HOWTOs on that topic?
Hanspeter
ps: I dind't find something on the Net. I'm perhaps using wrong keywords, cause I'm not a native english speaker, and I often trie to search simple things on the net, but don't find anything. Does someone know a guide for searching developer stuff, including something like a list/suggestion of keywords often used?
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden