开发者

More asynchronous socket questions:

开发者 https://www.devze.com 2023-02-15 11:40 出处:网络
Initial questions here So I\'ve been reading up on asynchronous sockets, and I have a couple more questions. Mostly concrete.

Initial questions here

So I've been reading up on asynchronous sockets, and I have a couple more questions. Mostly concrete.

1: I can use a blocking socket with select() without repercussions, corre开发者_JS百科ct?

2: When I use FD_SET() I'm appending the current fd_set* not changing it, correct?

3: When using FD_CLR(), I can simply pass in the socket ID of the socket I wish to remove, right?

4: When I remove a socket, using FD_CLR(), is there a prefferred way of resetting the Max File Descriptor (nfds)?

5: Say I have all of my connected sockets in a vector, when select() returns, I can just itterate through that vector and check if (FD_ISSET (theVector[loopNum], &readFileSet)) to see if any data needs to be read, correct? And if this returns true, I can simply use the same receiving function I was using on my synchronous sockets to retreive that data?

6: What happens if select() attempts to read from a closed socket? I know it returns -1, but does it set errno or is there some other way I can continue to use select()?

7: Why are you so awesome? =D


I appreciate your time, sorry for the headache, and I hope you can help!


  1. Yes
  2. Unclear? FD_SET inserts a socket into the set. If the socket is already there, nothing changes.
  3. FD_CLR removes a socket from the set, if the socket isn't there nothing's changed
  4. You could keep a parallel set<> of sockets, then get the highest value from there. Or you could just set a bool saying "rescan for nfd before next select" (NOTE: On windows nfd is ignored)
  5. Correct
  6. If select fails, the quick fix is to iterate sockets and select() on each of them one by one to find the bogus one. Optimally your code should not allow select() on a socket you have closed though, if the other end closed it it's perfectly valid to select on.
  7. I need to get you to talk to my wife.


So I've been reading up on asynchronous sockets

Judging by what follows I don't think you have. You appear to have been reading about non-blocking sockets. Not the same thing.

1: I can use a blocking socket with select() without repercussions, correct?

No. Consider the case where a listening socket becomes readable, indicating an impending accept(), but meanwhile the client closes the connection. If you then call accept() you will block until the next incoming connection, preventing you from servicing other sockets.

2: When I use FD_SET() I'm appending the current fd_set* not changing it, correct?

No. You are setting a bit. If it's already set, nothing changes.

3: When using FD_CLR(), I can simply pass in the socket ID of the socket I wish to remove, right?

Correct.

4: When I remove a socket, using FD_CLR(), is there a preferred way of resetting the Max File Descriptor (nfds)?

Not really, just re-scan and re-compute. But you don't really need to reset it actually.

5: Say I have all of my connected sockets in a vector, when select() returns, I can just itterate through that vector and check if (FD_ISSET (theVector[loopNum], &readFileSet)) to see if any data needs to be read, correct?

Correct, but it's more usual just to iterate through the FD set itself.

And if this returns true, I can simply use the same receiving function I was using on my synchronous sockets to retreive that data?

On your blocking sockets, yes.

6: What happens if select() attempts to read from a closed socket?

select() doesn't 'attempt to read from a closed socket. It may attempt to select on a closed socket, in which case it will return -1 with errno == EBADF, as stated in the documentation.

I know it returns -1, but does it set errno or is there some other way I can continue to use select()?

See above.

0

精彩评论

暂无评论...
验证码 换一张
取 消