Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Cocoa networking woes...

Cocoa networking woes...
Thread Tools
Dedicated MacNNer
Join Date: May 2003
Location: Atlanta, GA
Status: Offline
Reply With Quote
Sep 26, 2003, 03:39 PM
 
Hey all,

I'm having a huge headache w/ Cocoa networking.

I'm writing a client app that connects to a server, and recieves various messages during said connection relating to gamestate, etc.

My problem is this...

I tried SmallSockets, and while it did EVERYTHING I wanted, it failed in once instance (and this seems to be common)...

There is NO "data waiting to be read" notification. The only way I could figure out to accomplish this, was to spawn a separate thread to watch the isReadable variable in an infinite loop and retrieve/format/distribute a message when it came along...

The problem with this is that it spikes CPU usage to 100%.

I couldn't get OmniNetworking to compile (Foundation and Networking both had trouble with an enum for a bool type in one of the headers... Base compiled fine), but it appears to suffer the same troubles?

Perhaps I'm looking at this the wrong way... but what I want to do is the following:

Create a socket, send an init message to the server on connection, wait in the background for data to come in and pass that data off to my controller class. It will also need to be able to send data at regular intervals, but this doesn't seem to be a problem

My question is this, what's the best way to set up such a client? Is there some networking API I'm missing that can set a delegate for an "onReceive" method or something like that?

I'd prefer to stay away from OmniNetworking, and honestly I'm not *particularly* fond of SmallSockets, but I'd like a way to be able to (easily) use my existing data types (NSString's and/or NSData) if possible.

EDIT: the only reason I say I don't like smallsockets is the lack of delegation/waiting... other than that's it seems to be an excellent wrapper.
Alex

G7 Software: home Tetrinet Aqua
-----
"Utopia" 1Ghz TiBook SuperDrive w/ 1Gb RAM.
     
Banned
Join Date: Apr 2002
Location: -
Status: Offline
Reply With Quote
Sep 26, 2003, 04:38 PM
 
use sleep(1), (include <unistd.h>)
     
Dedicated MacNNer
Join Date: May 2003
Location: Atlanta, GA
Status: Offline
Reply With Quote
Sep 27, 2003, 11:03 AM
 
Originally posted by ambush:
use sleep(1), (include <unistd.h>)
Wouldn't sleep(1) wait for 1 second between each poll? The reason I ask is that I'm not sure this is frequent enough...

If I'm misunderstanding the sleep function, could you show me an example of what you're talking about?

Thanks for the info!
Alex

G7 Software: home Tetrinet Aqua
-----
"Utopia" 1Ghz TiBook SuperDrive w/ 1Gb RAM.
     
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
Sep 27, 2003, 07:20 PM
 
I'm using SmallSockets for a project I'm working on, its BufferedSocket class specifically.

Like you, I spun off another thread, and just read from the socket using - (NSMutableData*)readDataUpToString:(NSString*) s; in a while(1) { } loop, basically following structure of the example code in the BufferedSockets sample project. So far, it seems to work a treat, never going above 0% when it's idle. The class itself seems to take care of constantly polling the lower-level BSD socket.

Sorry if this doesn't help at all; I've just woken up and have much grogginess.
[Wevah setPostCount:[Wevah postCount] + 1];
     
Dedicated MacNNer
Join Date: May 2003
Location: Atlanta, GA
Status: Offline
Reply With Quote
Sep 28, 2003, 11:00 AM
 
Originally posted by Wevah:
I'm using SmallSockets for a project I'm working on, its BufferedSocket class specifically.

Like you, I spun off another thread, and just read from the socket using - (NSMutableData*)readDataUpToStringNSString*) s; in a while(1) { } loop, basically following structure of the example code in the BufferedSockets sample project. So far, it seems to work a treat, never going above 0% when it's idle. The class itself seems to take care of constantly polling the lower-level BSD socket.

Sorry if this doesn't help at all; I've just woken up and have much grogginess.
Perhaps that's my problem...

I was doing:

while ([socket isReadable]) {}

Which would force a message to socket each time, and a lookup of readable, and a return before even executing the loop...

I'll try going from the BufferedSockets example and see if that helps things along... from what I can tell OmniNetworking is overkill++ for what I'm doing, and I'd much rather not have to almost double the size of my app bundle just to include the omniframeworks

Thanks!
Alex

G7 Software: home Tetrinet Aqua
-----
"Utopia" 1Ghz TiBook SuperDrive w/ 1Gb RAM.
     
K++
Senior User
Join Date: Jan 2002
Location: NYC
Status: Offline
Reply With Quote
Sep 29, 2003, 04:39 PM
 
Why not use NetSocket instead and use its delegate method socketHasDataint) which sends the size of the content so you know how much your getting.

http://www.blackholemedia.com/code

It has many advantages over SmallSockets besides its being a much more Cocoa-fied socket, it also uses a CFSocket rather than a raw BSD socket and has lass overhead than smallSockets.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:12 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2