Jump to content
  • Advertisement
Sign in to follow this  
dave

Small TCP Question

This topic is 4835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, im still working on TCP [smile] My question... If i have a collection of sockets for n number of clients connected to a server i will want to poll the sockets with a ping to make sure the client is still alive. So i have researched and it appears i have to add all the clients sockets to an fd_set structure. Now this structure is passed to a function called select() which pols the collection of sockets on the fd_set structure. Select returns the number of sockets in the collection that are ready, but im confused as to how i identify which sockets it found ready out of the collection, assuming not all are ready. I could build a set containing 1 socket and call select() loads of times, but i would like to know if there is a proper way. Does anyone know how i do this? ace

Share this post


Link to post
Share on other sites
Advertisement
select() modifies the argument sets to include only the "ready" sockets. Thus, you can use FD_ISSET() to test whether a given socket is ready once select() returns.

I'm moving this to the Networking & Multiplayer forum, which seems like a more appropriate place. I also suggest you read the Forum FAQ for that forum, as it points to useful resources and libraries you might want to look at.

Share this post


Link to post
Share on other sites
So what makes a socket not ready. I have one socket open with a client and have put it into a fd_set using the macro. I have run select and it returns 0 and fd_set.fd_count is set to 0.

Whats wrong here?

ace

Share this post


Link to post
Share on other sites
MSDN

"Return Values
The select function returns the total number of socket handles that are ready and contained in the fd_set structures, zero if the time limit expired, or SOCKET_ERROR if an error occurred. If the return value is SOCKET_ERROR, WSAGetLastError can be used to retrieve a specific error code."

Share this post


Link to post
Share on other sites
I just realised that, but thanks anyway.

So what makes a socket write ready? To my knowledge there ist anything pending to me written until u call send, whcih actually does the sending. In the documentation it says that a socket making a connect call is writable, so long as the connect call succeeds. What does this mean. All these sockets in question are attained through accept(). Does this mean they are not writable, do i have to call connect on the sockets attained through accept?

ace

Share this post


Link to post
Share on other sites
Okay. A quick summary:

set listen socket;
create [empty] list/map/vector of sockets.
add listen socket to list/map/vector.
while (1){
set fd_set with socket in list/map/vector.
select(...);
foreach socket in list/map/vector{
if (listen socket ISSET in fd_set){
accept new connection;
add new socket to map/list/vector;
add new client in gamedata;
}else if (connected socket ISSET in fd_set){
// ready to be read.
return_val = recv( stuff );
if (return_val > 0 ){
dispatch stuff to handler;
}else{
client_disconnected!(); // remove from list/map/vector
remove from game data, or otherwise handle it;
}
}
}
}


Or otherwise, you don't need a ping to ensure the client is alive with TCP. recv will return non-positive if for whatever reason the connection is broken.

No, accepted sockets are good to go.

What the confusing documentation means is that send() will block, just like recv(). In certain cases, a socket is not ready to send [if the hardware is busy, or if you filled an OS buffer], and will block; just like a recv() call when nobody sent you anything. select() can also be used in this situation to determine if the socket is ready to send. Usually send() is just called directly though, since the blocking scenarios are far less common than in the recv() case, and sockets are almost always send ready.

Share this post


Link to post
Share on other sites
Quote:
Original post by ace_lovegrove
So what makes a socket write ready?

TCP will only allow so much un-ACK'd data to be outstanding at a time. If the window is completely full the socket won't be writeable.

This is probably a good time to mention that you should always check the return value from send(). It is allowed to send only part of the buffer you give it.

Quote:
Original post by ace_lovegrove
fd_set.fd_count is set to 0

You shouldn't go poking around inside an fd_set directly. The structure is very different between Windows and *nix. Use the macros - FD_ISSET, etc. and you'll be portable.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!