-1

I'm having some issues using Select() to determine when there is data available to read on a socket. I expect the socket I'm connecting to to have no data and thus for select to return a timeout or 0 value. Instead, I get -1 value and the message is "error: no error."

I have code very similar to this example: https://beej.us/guide/bgnet/html/multi/selectman.html

I've read through the select() documentation a couple times here: msdn.microsoft.com/en-us/library/windows/desktop/ms740141(v=vs.85).aspx

Any ideas would be appreciated. Thanks!

Specifics: C++, Win-XP, Microsoft Visual C++ 2010

Sandburg
  • 757
  • 15
  • 28
ProGirlXOXO
  • 2,170
  • 6
  • 25
  • 47
  • 2
    Without seeing your code, we have no way of knowing what it is you're doing wrong. – Brian Roach Feb 27 '12 at 19:34
  • Thanks. Should I put the entire code content in this post or another text hosting site? It's very long and most of the code is entirely irrelevant to this problem. – ProGirlXOXO Feb 27 '12 at 19:41
  • create a minimalistic code which reproduces the problem. this is also the best way for you to figure out the root of the problem. – Karoly Horvath Feb 27 '12 at 19:58
  • Here is a more 'minimal version of my code.' I'm sorry, I could only delete one function. http://pastebin.com/q9pZ33UK – ProGirlXOXO Feb 27 '12 at 20:11
  • @ProGirlXOXO: you can remove a dozen things.. (writing output to file, waiting for keypress, etc...) – Karoly Horvath Feb 27 '12 at 20:15
  • As a general note - it is not a very good decision to use select to see if there is any data. You have to assume there is and read it, and only fallback if read returns EWOULDBLOCK. Otherwise you are just wasting resources. –  Feb 27 '12 at 21:03
  • I need to format the data in a specific way. If i just grab the data constantly, I get an unpredictable chunk of data. If i wait until something is available i can format based on tabs, carriage returns, etc. – ProGirlXOXO Mar 01 '12 at 18:21

1 Answers1

3

You are adding your socket to the fd_set structure before you actually create the socket:

FD_SET(s, &readfds);
// (...)
int iConnected = ConnectToHost(PortNum, IpAddy);
//^ This actually calls: s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);

Therefore, you are adding an invalid socket to the fd_set. Create the socket before doing this:

int iConnected = ConnectToHost(PortNum, IpAddy);
//(...)
FD_SET(s, &readfds);
mfontanini
  • 21,410
  • 4
  • 65
  • 73