Substituting low level BSD stuff with spiffy Cocoa calls
Substituting low level BSD stuff with spiffy Cocoa calls
- Subject: Substituting low level BSD stuff with spiffy Cocoa calls
- From: Malte <email@hidden>
- Date: Wed, 15 Dec 2004 06:09:54 +0100
Hey *
since this is both, a network and cocoa related problem, i wasn't sure which list to post this to and thus sent it to both, the networking and the cocoa list.
here is my problem:
I'm currently trying to set up a little "networking-testing-cocoa-app". I've plowed through the net finding countless tutorials about
BSD sockets but oddly enough not one "clean objective-c/cocoa"-networking tutorial. :(
So following a tutorial that builds a little chat-application (written by Mike Beam on macdevcenter.com) he, too, uses the unsightly BSD stuff in his otherwise spiffy cocoa/objective-c app. I figured that i'd just "clean up the code" by replacing the BSD stuff with the (apparent) cocoa equivalent but somehow this isn't working too great. After a few days of trying to get a strictly Objective-C/Cocoa solution up and running i have decided to throw the towel into the ring and turn to the lists. I hope one of you has both, the knowledge and patience to help me out :(
Here is the original code by Mike from his tut (Mike, in case you're reading the list: thanks for the tutorials you put together ;))
[snip]
struct sockaddr_in addr;
int sockfd;
// Create a socket
sockfd = socket( AF_INET, SOCK_STREAM, 0 );
// Setup its address structure
bzero( &addr, struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl( INADDR_ANY ); // Bind to any of the system addresses
addr.sin_port = htons( 0 ); // Let the system choose a port for us
// Bind it to an address and port
bind( sockfd, (struct sockaddr *)&addr, struct sockaddr));
// Set it listening for connections
listen( sockfd, 5 );
// Find out the port number so we can pass it to the net service initializer
int namelen = struct sockaddr_in);
getsockname( sockfd, (struct sockaddr *)&addr, &namelen );
// Create NSFileHandle to communicate with socket
listeningSocket = [[NSFileHandle alloc] initWithFileDescriptor:sockfd];
[snip]
what follows is the NSNotificationCenter stuff and so on.
So i figured i could abbreviate the whole ugly thing by doing so:
[snip]
struct sockaddr_in addr;
int sockfd;
NSSocketPort * cocoaStyleSocket = [[NSSocketPort alloc] initWithTCPPort:0];
// Create a socket
sockfd = [cocoaStyleSocket socket];
// Create NSFileHandle to communicate with socket
listeningSocket = [[NSFileHandle alloc] initWithFileDescriptor:sockfd];
[snip]
But after this modification the app doesn't work anymore. There are endless invocations of the "receiveMessage"
method which is being triggered by the NSNotificationCenter when the user initiates a connection by double clicking the
rendezvous contact from his list.
Also no messages are being sent/received anymore --> the chat application isn't working.
Maybe something in the code snippets above is painfully obvious to some of you but i'm such an amateur that i can't
figure it out. :(
By now i guess i could easily write my little app using the BSD sockets, alright, but it has gotten to the point where i want to get
the Obj-C/Cocoa way to work just as a proof of concept so to speak.
I am very grateful for any advice!
Thanks a lot,
-malte the not-so-hot networker
Attachment:
PGP.sig
Description: This is a digitally signed message part
_______________________________________________
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