Link to home
Start Free TrialLog in
Avatar of axnst2
axnst2Flag for United States of America

asked on

Windows socket programing

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_ZERO(&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:

#if INCL_WINSOCK_API_PROTOTYPES
WINSOCK_API_LINKAGE
int
WSAAPI
select(
    int nfds,
    fd_set FAR * readfds,
    fd_set FAR * writefds,
    fd_set FAR *exceptfds,
    const struct timeval FAR * timeout
    );
#endif // INCL_WINSOCK_API_PROTOTYPES

What could my problem be?  Please help me I'm lost.
Avatar of rstaveley
rstaveley
Flag of United Kingdom of Great Britain and Northern Ireland image

YOu need to set nfds
Mind you, it says at http://msdn.microsoft.com/library/en-us/winsock/winsock/select_2.asp that you don't, because it is ignored. I hadn't readlised that.
You're not just timing out are you?
Avatar of axnst2

ASKER

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

ASKER

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

ASKER

That was my guess too...I am setting up a laptop as we speek
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.
ASKER CERTIFIED SOLUTION
Avatar of rstaveley
rstaveley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of grg99
grg99

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.