Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-23-2011, 08:39 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 19
BlueGooGames is on a distinguished road
Smile How to detect disconnected user from CFSocket?

I'm setting up a multiplayer game with CFSockets and CFStreams. Everything is working smoothly and I'm sending data back and forth between server and clients. The problem is I get no notification when a client disconnects.

I got the code from the book Beginning iPhone Games Development and here is a part when a new client connects:

Code:
static void serverAcceptCallback(CFSocketRef socket, CFSocketCallBackType type,
    CFDataRef address, const void *data, void *info) {
  SocketServer *server = (SocketServer*)info;
  
  // We can only process "connection accepted" calls here
  if ( type != kCFSocketAcceptCallBack ) {
    return;
  }
  
  // for AcceptCallBack, the data parameter is a pointer to a CFSocketNativeHandle
  CFSocketNativeHandle handle = *(CFSocketNativeHandle*)data;
  Connection *connection = [[[Connection alloc]
      initWithNativeSocketHandle:handle] autorelease];

  if ( [connection connect] ) {
    [server.delegate newClientConnected:connection];
  }
}
My question is, do I need to ping the user once in a while, and if he does not respond in like 10 seconds, assume he's disconnected?

There are several reasons why he might be disconnected:
- Quit hosted game (from a button in the game)
- Quit app
- Got phonecall
- Network error maybe

So how is a good way to catch a disconnect? It would be neat if there was a function like the one above.

Thanks!
__________________
BlueGooGames Game Dev Blog - n00b game dev with cocos2d.
In a while I'll have a game ready, but it'll take... a while
BlueGooGames is offline   Reply With Quote
Old 02-23-2011, 08:56 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

Have you tried looking in the CFSocket class reference? -- CFSocket class reference

I don't see any sort of callback for when a client gets disconnected, but I do see a method called CFSocketIsValid, which "Returns a Boolean value that indicates whether a CFSocket object is valid and able to send or receive messages."

Seems to me that this could be useful, but you might have to implement the client polling approach you were mentiongin, just not necessarily have to wait X seconds for a response.
smithdale87 is offline   Reply With Quote
Old 02-23-2011, 09:26 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 19
BlueGooGames is on a distinguished road
Default

Yes, I've been studiying that page to no avail. I also see the function CFSocketIsValid, but I'm not sure how to use it.

I think the server is the socket, and that is valid all the time. But the connections with the clients are the ones I need to check. Do you think I'm understanding this enough? Networking is a pretty hefty subject, but it's fun to learn
__________________
BlueGooGames Game Dev Blog - n00b game dev with cocos2d.
In a while I'll have a game ready, but it'll take... a while
BlueGooGames is offline   Reply With Quote
Old 02-25-2011, 05:47 AM   #4 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 19
BlueGooGames is on a distinguished road
Default

I found that I got the connection closed trigger, just that when I sent the program in the background on the client device, the connection is still open. But as soon as I really quit the app, I get read errors, which causes the connection to get detected as closed.

Just in case anyone is doing the same thing
__________________
BlueGooGames Game Dev Blog - n00b game dev with cocos2d.
In a while I'll have a game ready, but it'll take... a while
BlueGooGames is offline   Reply With Quote
Reply

Bookmarks

Tags
cfsocket, connect, disconnect, network

Thread Tools
Display Modes

Posting 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



» Advertisements
» Online Users: 374
18 members and 356 guests
Absentia, akphyo, apatsufas, BinHex, cpsclicker, dre, Error404, Gaz, gmarro, jeroenkeij, Kirkout, mottdog, Music Man, PavelMik, teebee74, whitey99, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:15 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0