0

I have written a TCP Socket application in vc++ 2008. I want 'send' operation to be timed out if it takes more than x seconds. For that I tried using

char *optValue = "5000";
setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, optValue, sizeof(optValue));

It returns success, but 'send' didn't behave as expected. Then tried with

tv.tv_sec = 10; tv.tv_usec = 0; FD_SET(accept_socket, &write_mask); select(socket, (fd_set *)0, &write_mask, (fd_set *)0, &tv);

Still 'send' is not behaving according to the timeout value set. Please check is it the right way to do?

Pavan
  • 1,023
  • 2
  • 12
  • 25
  • 1
    Are you sure that the setsocket argument has to be passed as a string? The function accepts a char pointer, but this is just used as a pointer to "something". – harper Aug 29 '12 at 06:32
  • I changed it to integer, and used char pointer to that. (As told in Answer1 below). Still problem is not solved. – Pavan Aug 30 '12 at 03:36

1 Answers1

1

The setsockopt, while accepting a char * doesn't actually want the values as strings. You should pass the value as a pointer to an integer, typecasted to char *:

int optValue = 5000;
setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, (const char *) &optValue, sizeof(optValue));

PS. Your usage of sizeof does not do what you think it does. It returns the size of the pointer, not what it points to.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621