• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 609
  • Last Modified:

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.
0
axnst2
Asked:
axnst2
  • 5
  • 3
1 Solution
 
rstaveleyCommented:
YOu need to set nfds
0
 
rstaveleyCommented:
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.
0
 
rstaveleyCommented:
You're not just timing out are you?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
axnst2Author Commented:
What does nfds contain and what's a reasonable value for it?
0
 
axnst2Author Commented:
I might be..I am trying to communicate with it on a factory floor, wirless, 200 ft
0
 
axnst2Author Commented:
That was my guess too...I am setting up a laptop as we speek
0
 
rstaveleyCommented:
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.
0
 
rstaveleyCommented:
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.
0
 
grg99Commented:
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.

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now