Thread: Winsock Select() Function

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    15

    Winsock Select() Function

    I'm trying to make a real basic program that will connect to something read data until there is no more to read, then allow for input, then loop. I can create the socket, connect and read the first round of data and respond just fine, but there seems to be a problem in the second iteration of the loop.

    I'm using the winsock select() function and I cannot seem to figure out the problem. I was hoping somebody might have a snippet of sample code that I might look at in comparison.

    Thanks,

    PetrolMan

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Certain non-blocking selects often don't work in Windows when some third party firewall programs are installed because they turn a non-blocking select into a blocking one! It's fustrating as hell having to write a workaround just for certain PCs.

    You're betting off posting a snippet of your code rather than asking for someone else's.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I revived an old windows specific code using WSAAsyncSelect -- it's bare bones but it's windows code and it's pretty verbose about the connection

    http://zxcvbn.googlecode.com/files/Asyncdaemon.zip
    http://zxcvbn.googlecode.com/files/asyncdaemon_src.zip

    Code:
    [+] Attempting go
    [+] WSASocket returned valid socket
    [+] Bind'ed socket to service - port 5678
    [+] Connection from 127.0.0.1
    [+] Echo: "This is a test"
    [+] Read from 127.0.0.1: g
    [+] Read from 127.0.0.1: o
    [+] Read from 127.0.0.1: o
    [+] Read from 127.0.0.1: d
    [+] Read from 127.0.0.1:  
    [+] Read from 127.0.0.1: q
    [+] Read from 127.0.0.1: u
    [+] Read from 127.0.0.1: i
    [+] Connection 'quit' 127.0.0.1 closed
    [+] Connection 127.0.0.1 closed
    [-] No systems go
    [-] Definitely closed a connection

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    15
    I appreciate the code. As for the firewall issue, how do you recognize this issue and create a workaround? For some protocols such as POP3 blocking sockets isn't much of an issue since there is a call and response setup but IRC, for example, is a free for all and I'm honestly not sure how someone could even attempt a program without nonblocking sockets.

    Again, thanks for the help.

    PetrolMan

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    15
    Code:
    		while (select(0, &sockets, 0, 0, &timeout))
    		{
    			test = recv(mysock, bufs, sizeof(bufs), 0);
    			if (test == 0)
    			{
    				cout << "Connection Closed\n";
    				return 1;
    			}
    			cout << bufs[0];
    		}
    I'm pretty sure my error is in this code. It works on the first iteration (the whole thing is in an infinite for loop) but blocks on the second which suggests to me that the select is returning a value even though the socket isn't ready to read. Perhaps I'm simply using this wrong though. The other possibility is that select is returning some kind of error that I'm simply not checking for but how would I distinguish? BTW, I'm receiving everything in a single char at a time but I figured that would be safest.

    I'm sure I'm just making a stupid and obvious mistake but I can't figure it out.

    Thanks for all of the help.

    PetrolMan
    Last edited by PetrolMan; 03-26-2009 at 07:01 PM. Reason: Sorry for the Multiple Edits

  6. #6
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    The other possibility is that select is returning some kind of error that I'm simply not checking for but how would I distinguish?
    you should check if there is an error using WSAGetLastError() it returns a int that specifies exactly what the error is. haven't used WinSock in a while so i hope that's helpful
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Definitely check recv for -1 socket error, and check WSAGetLastError for WSAEWOULDBLOCK. if you get this message you can just wait for another select notification.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    15
    Progress: I've run the program with error checking and the first iteration goes just fine. On the second go around the select function seems to be the function that is failing. Interestingly enough, the error code is 10022, which for the select function means the timeval structure is wrong. The problem is the code runs fine the first iteration. Any ideas?

    PetrolMan

  9. #9
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I found on this man page on select and fd messages this code

    Code:
       retval = select(1, &rfds, NULL, NULL, &tv);
        /* Don't rely on the value of tv now! */
    fd_zero(3): synchronous I/O multiplexing - Linux man page

    lol, you might have to reset the timeout value or something

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    15

    *Slaps Forehead*

    I made the dumb mistake of not reading the MSDN reference very carefully.

    Upon return, the structures are updated to reflect the subset of these sockets that meet the specified condition. The select function returns the number of sockets meeting the conditions. A set of macros is provided for manipulating an fd_set structure.
    So once select() returns a negative on a socket's readability it is removed from the list. And that is the source of all of my problems. I was trying to select() against an empty list which causes an error.

    I need a Nerf Bat to whack against my head for trying to skim the reference text. Sorry about all of the wasted time for those who have helped. I promise to be more careful in the future.

    PetrolMan

  11. #11
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    I brain fart real loud when coding at night, of course if it wasn't late where your at then by all means get that nerf bat out.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM