Windows socket programing

Posted on 2004-11-05
Last Modified: 2011-09-20
I am writing a client to receive packets from a server that a vendor wrote for an equipment.  For some reason I am not able to get anything back from the server.  I can send it stuff no problem, but I don't get a response back from it.

Client end provided by vendor:

int TCPClient::waitEvents(long millis) {
      timeval timeout;
      timeout.tv_sec = 0;
      timeout.tv_usec = millis*1000;
      fd_set      fd;
      FD_SET(socketHandle, &fd);
      sockaddr_in      from;
      int            len = sizeof(sockaddr_in);
                int         ret = 0;
      int num;
                int err=0;
      if ((num = select(NULL, &fd,NULL , NULL, &timeout)) > 0)  //<---right here I get a 0 returned by select and that's my problem.  In case of an error (socket error...etc) I should get a -1, but I get a zero.

"select" is defined by MS in winsock2.h as follows:

    int nfds,
    fd_set FAR * readfds,
    fd_set FAR * writefds,
    fd_set FAR *exceptfds,
    const struct timeval FAR * timeout

What could my problem be?  Please help me I'm lost.
Question by:axnst2
    LVL 17

    Expert Comment

    YOu need to set nfds
    LVL 17

    Expert Comment

    Mind you, it says at that you don't, because it is ignored. I hadn't readlised that.
    LVL 17

    Expert Comment

    You're not just timing out are you?

    Author Comment

    What does nfds contain and what's a reasonable value for it?

    Author Comment

    I might be..I am trying to communicate with it on a factory floor, wirless, 200 ft

    Author Comment

    That was my guess too...I am setting up a laptop as we speek
    LVL 17

    Expert Comment

    I usually do something like this:

           timeout.tv_sec = millis/1000;
           timeout.tv_usec = (millis%1000)*1000;

    I've not seen if the timeout works with the timeout entirely in the microsecs part.
    LVL 17

    Accepted Solution

    nfds is conventionally the highest-numbered descriptor in any of the three descriptor sets plus 1; but the MS document says that it is being ignored.
    LVL 22

    Expert Comment

    How long are you waiting?   i.e. what is millis?

    also be sure you're not overflowing the integer arithmetic, or the size of usec.
    A few simple if() statements as suspenders wouldnt hurt.

    In my experience the debugging of these kinds of problems goes about a bazillion times faster if you have some sort of network sniffer.  Many times a peek at a packet immediately shows the problem, where otherwise I would have flailed around for weeks trying various things.

    There are several nice free sniffers for Windows and Linux, I strongly  suggest you get one of these.


    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now