how to setup socket timeout?

I can make a socket listen on a port, but I don't know how to make it timeout if it doesn't recieve any data on that port within, say, 10 seconds.

Does anyone know where I'm going wrong?



SOCKET sock = INVALID_SOCKET;
sock = socket ( AF_INET, SOCK_DGRAM, 0 );

if ( INVALID_SOCKET ==  sock)
{
  AfxMessageBox("Error:  Broadcast DoUdp - INVALID_SOCKET");
}

int iTimeoutLen = 10;
err = setsockopt(sock,                   SOL_SOCKET,
                               SO_LINGER,
                               (CHAR *) &iTimeoutLen,sizeof(int));




Moth
LVL 1
MothAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IexpertCommented:
timeval timeout;
timeout.tv_sec =8; //seconds
timeout.tv_usec = 0; //micro seconds

int nfound = select(FD_SETSIZE, &readmask, (fd_set*) NULL, (fd_set*) NULL, &timeout)

if (nfound < 0)
      printf("Select error [%d]",WSAGetLastError());
else if (nfound==0)
      //deal with timeout here
else
      //read or accept() socket
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MaydipalleCommented:
Hi,
 Create the system structure of type LINGER and set proper values as described below.LINGER as defined as
struct linger {
    u_short    l_onoff;
    u_short    l_linger;
}

To enable SO_LINGER, the application should set l_onoff to a nonzero value, set l_linger to zero or the desired time-out (in seconds), and call setsockopt. To enable SO_DONTLINGER (that is, disable SO_LINGER) l_onoff should be set to zero and setsockopt should be called. Note that enabling SO_LINGER with a nonzero time-out on a nonblocking socket is not recommended.
Enabling SO_LINGER also disables SO_DONTLINGER, and vice versa. Note that if SO_DONTLINGER is DISABLED (that is, SO_LINGER is ENABLED) then no time-out value is specified. In this case, the time-out used is implementation dependent. If a previous time-out has been established for a socket (by enabling SO_LINGER), then this time-out value should be reinstated by the service provider.Supply this structure instead of SO_LINGER in your application

Hope this may help you.Just I got this from some where in the documentation

Madhav
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.