Advertisement

detecting disconnection properly

Started by March 13, 2003 02:59 AM
5 comments, last by morebeer 21 years, 10 months ago
hi guys (and gals?), i''m writing my own little chat server/client app using winsock (TCP). in the server, i have two threads: a ''listen() thread'' which then starts a ''recv() thread'' for every joining client. when the recv()''ing for a client fails, it is assumed that the client disconnected (WSAECONNABORTED). additionally, a client sends some ''exit'' message before he actually disconnects from the server, so the client can be turned off in a more nice way on the server side. my question is, is this the best/safe approach to detect the disconnection of a client? or are there some other ways? (i''m pretty new to network and socket programming).. i''m a bit confused because a chat program is so much event-driven,anything can happen at any time. i usually prefer ''deterministic'' game loops which simply do their stuff on and on in a repetitive order. thanks in advance
That''s pretty much it. Either the client is nice and sends you a signoff message before gracefully closing its end, or it disconnects abruptly.

-cb
Advertisement
Starting one thread per each client is not a good idea (unless you want to be a CPU hog). IMHO, a single thread is enough for a small chat app. Look through the BEEJ's guide to network programming. There's a good example on creating multiclient apps for chatting in the 'advanced' section. There's also a neat algorithm for determining new connections, data receiving and sending and for handling disconnections. Select() is used.
Here's the guide: http://www.ecst.csuchico.edu/~beej/guide/net/

"I'll be Bach!" (c) Johann Sebastian Schwarzenegger

[edited by - x33 on March 13, 2003 7:26:37 PM]
"I'll be Bach!" (c) Johann Sebastian Schwarzenegger
I''ve been learning up on using sockets with the select() command, so if you get stuck with anything you can contact me either at my website or by email or here.

Later,


Michael Bartman
Michael BartmanLead ProgrammerDark Omen Studios
thanks for all the help! i will look into select(), haven''t used it by now.
you could as well use asynchronous sockets if you''re aiming win32 platforms. They''re doing pretty good.
Darkhaven Beta-test stage coming soon.
Advertisement
What about applications where you maintain an open connection to each session for constantly streaming data to get around firewalls? How would you do something like that?


Looking for an honest video game publisher? Visit www.gamethoughts.com
Shameless plug: Game Thoughts

This topic is closed to new replies.

Advertisement