Sign in to follow this  
whiterook6

How does sending/receiving messages work without multithreading?

Recommended Posts

whiterook6    124
I'm learning winsock programming in preparation for making a simple multiplayer shooter, and I've made a simple type-send-receive-repeat program. However, it isn't multithreaded--it stops and waits for an answer from the simple echo server. I'm not against learning threading, but does the client or the server need to be multithreaded to handle messages? In particular, with my limited experience, I imagine the client would have, in its update loop, a section devoted to grabbing recent messages and handling them. It would also send messages during gameplay without waiting for replies. Is this guess close to accurate? Will me client need multithreading to handle networking? I figure the server will need threading, since I plan on being able to type into a console to change things like cheats, maps, etc., and I wouldn't want it to block waiting for me to type something. Am I on the right track? Is there an article that explains this well?

Share this post


Link to post
Share on other sites
hplus0603    11347
No, you will not need multithreading. There are two options:

1) Set your socket to non-blocking. Then when you call recv(), you will either get data, or it will return an error (would block).
2) Use select(). When select() returns a socket as readable, it is guaranteed that the next call to recv() will return data without blocking.

When sending, you will never block as long as there is space in the outgoing buffer. If you send more data than the uplink can handle, then the buffer will fill up, but you really don't want to do that :-)

Share this post


Link to post
Share on other sites
whiterook6    124
Okay, thanks. Non-blocking vs blocking isn't the same as tcp/udp, right? the hardware handles the tcp-must-arrive-handshaking-whatever, right? Also, what is the difference between asynchronous and synchronous sockets? Are those different than blocking/non-blocking?

Share this post


Link to post
Share on other sites
stonemetal    288
TCP = byte stream abstraction(in order, guarantied delivery) UDP = packet oriented(not guarantied, not order preserving.)

Edit:
This should make it easy to see where non-blocking = asynch comes from.
asynchronous = Thread does not block until communication is complete.
Synchronous = Thread blocks until communication is complete.

Share this post


Link to post
Share on other sites
hplus0603    11347
No, those are different concepts:

TCP/UDP -- the protocol

Blocking/Non-blocking -- how the sockets talk to your app

"Async" sockets -- a Windows-specific variant on non-blocking sockets, not generally recommended (if you want to be Windows specific, use IO completion ports instead)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this