 |
 |
Cocoa networking woes...
|
 |
|
 |
|
Dedicated MacNNer
Join Date: May 2003
Location: Atlanta, GA
Status:
Offline
|
|
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
|
|
use sleep(1), (include <unistd.h>)
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: May 2003
Location: Atlanta, GA
Status:
Offline
|
|
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
|
|
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
|
|
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*)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.
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.
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jan 2002
Location: NYC
Status:
Offline
|
|
Why not use NetSocket instead and use its delegate method socketHasData  int) 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.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|