How can I wait on a server socket for 10 seconds? - Linux

This is a discussion on How can I wait on a server socket for 10 seconds? - Linux ; Hi, I have the following code which listens on a server socket and accept client connections. How can I change it to just accept client connection for just 10 seconds after the server port is opened? I read about select, ...

+ Reply to Thread
Results 1 to 8 of 8

Thread: How can I wait on a server socket for 10 seconds?

  1. How can I wait on a server socket for 10 seconds?

    Hi,

    I have the following code which listens on a server socket and accept
    client connections. How can I change it to just accept client
    connection for just 10 seconds after the server port is opened? I
    read about select, but the example I found are for select on multiple
    sockets, not just 1.


    serverAddress.sin_family = AF_INET;
    serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
    serverAddress.sin_port = htons(listenPort);

    if (bind(listenSocket,
    (struct sockaddr *) &serverAddress,
    sizeof(serverAddress)) < 0) {
    std::cout << "cannot bind socket";
    return;
    }

    listen(listenSocket, 10);

    while (1) {
    std::cout << "Waiting for TCP connection on port " << listenPort
    << " ...\n";

    clientAddressLength = sizeof(clientAddress);
    connectSocket = accept(listenSocket,
    (struct sockaddr *) &clientAddress,
    &clientAddressLength);

    }


  2. Re: How can I wait on a server socket for 10 seconds?

    Herman.Schultz@gmail.com wrote:
    > I have the following code which listens on a server socket and accept
    > client connections. How can I change it to just accept client
    > connection for just 10 seconds after the server port is opened? I
    > read about select, but the example I found are for select on multiple
    > sockets, not just 1.


    > serverAddress.sin_family = AF_INET;
    > serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
    > serverAddress.sin_port = htons(listenPort);


    > if (bind(listenSocket,
    > (struct sockaddr *) &serverAddress,
    > sizeof(serverAddress)) < 0) {
    > std::cout << "cannot bind socket";
    > return;
    > }


    > listen(listenSocket, 10);


    > while (1) {
    > std::cout << "Waiting for TCP connection on port " << listenPort
    > << " ...\n";


    > clientAddressLength = sizeof(clientAddress);
    > connectSocket = accept(listenSocket,
    > (struct sockaddr *) &clientAddress,
    > &clientAddressLength);
    > }



    You can use select to wait on as many sockets (or file descriptors)
    as you want, if you want to wait for a single one just remove all
    but the one you're interested in from the example. Since you don't
    post the example code I can't show you directly how it's to be done
    there but basically it's just

    fd_set read_set;
    struct timeval timeout;

    FD_ZERO( &read_set ); /* clear the FD set */
    FD_SET( listenSocket, &read_set ); /* add the socket to the set */

    timeout.tv_sec = 10; /* set timeout to 10 s */
    timeout.tv_usec = 0;

    switch ( select( listenSocket + 1, &read_set, NULL, NULL, &timeout ) )
    {
    case 0:
    fprintf( stderr, "timeout occurred\n" );
    break;

    case 1 :
    /* just to make 100% sure;-) */
    assert( FD_ISSET( listenSocket ), &read_set ) );

    connectSocket = accept( listenSocket,
    ( struct sockaddr * ) &clientAddress,
    &clientAddressLength );
    ...
    break;

    default:
    fprintf( stderr, "some error during select\n" );
    break;
    }

    Alternatively, you could create a timer that expires after 10 se-
    conds, raising a SIGALRM signal. In that case accept() will return
    with a value of -1 and errno is set to EINTR. Of course, you must
    remove the timer if a connection comes in before the timer times
    out.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

  3. Re: How can I wait on a server socket for 10 seconds?

    On Jul 5, 11:21 am, j...@toerring.de (Jens Thoms Toerring) wrote:
    > Herman.Schu...@gmail.com wrote:
    > > I have the following code which listens on a server socket and accept
    > > client connections. How can I change it to just accept client
    > > connection for just 10 seconds after the server port is opened? I
    > > read about select, but the example I found are for select on multiple
    > > sockets, not just 1.
    > > serverAddress.sin_family = AF_INET;
    > > serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
    > > serverAddress.sin_port = htons(listenPort);
    > > if (bind(listenSocket,
    > > (struct sockaddr *) &serverAddress,
    > > sizeof(serverAddress)) < 0) {
    > > std::cout << "cannot bind socket";
    > > return;
    > > }
    > > listen(listenSocket, 10);
    > > while (1) {
    > > std::cout << "Waiting for TCP connection on port " << listenPort
    > > << " ...\n";
    > > clientAddressLength = sizeof(clientAddress);
    > > connectSocket = accept(listenSocket,
    > > (struct sockaddr *) &clientAddress,
    > > &clientAddressLength);
    > > }

    >
    > You can use select to wait on as many sockets (or file descriptors)
    > as you want, if you want to wait for a single one just remove all
    > but the one you're interested in from the example. Since you don't
    > post the example code I can't show you directly how it's to be done
    > there but basically it's just
    >
    > fd_set read_set;
    > struct timeval timeout;
    >
    > FD_ZERO( &read_set ); /* clear the FD set */
    > FD_SET( listenSocket, &read_set ); /* add the socket to the set */
    >
    > timeout.tv_sec = 10; /* set timeout to 10 s */
    > timeout.tv_usec = 0;
    >
    > switch ( select( listenSocket + 1, &read_set, NULL, NULL, &timeout ) )
    > {
    > case 0:
    > fprintf( stderr, "timeout occurred\n" );
    > break;
    >
    > case 1 :
    > /* just to make 100% sure;-) */
    > assert( FD_ISSET( listenSocket ), &read_set ) );
    >
    > connectSocket = accept( listenSocket,
    > ( struct sockaddr * ) &clientAddress,
    > &clientAddressLength );
    > ...
    > break;
    >
    > default:
    > fprintf( stderr, "some error during select\n" );
    > break;
    > }
    >
    > Alternatively, you could create a timer that expires after 10 se-
    > conds, raising a SIGALRM signal. In that case accept() will return
    > with a value of -1 and errno is set to EINTR. Of course, you must
    > remove the timer if a connection comes in before the timer times
    > out.
    > Regards, Jens
    > --
    > \ Jens Thoms Toerring ___ j...@toerring.de
    > \__________________________ http://toerring.de


    Thank you very much for your help. How can I get 'assert' call to
    comile?

    I get this compilation error:

    error: 'assert' was not declared in this scope
    Server.tproj/RTSPRequestInterface.cpp:506: error: expected `;' before
    ')' token



  4. Re: How can I wait on a server socket for 10 seconds?

    Herman.Schultz@gmail.com wrote:
    > How can I get 'assert' call to comile?


    > I get this compilation error:


    > error: 'assert' was not declared in this scope
    > Server.tproj/RTSPRequestInterface.cpp:506: error: expected `;' before
    > ')' token


    You need to include (in C) or (for C++). But
    you can also leave it out - if select() returns and you were wai-
    ting for a single file selector then there hardly should be a way
    that it would return for a different file descriptor then the one
    you were waiting for.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

  5. Re: How can I wait on a server socket for 10 seconds?

    On Jul 5, 11:48 am, j...@toerring.de (Jens Thoms Toerring) wrote:
    > Herman.Schu...@gmail.com wrote:
    > > How can I get 'assert' call to comile?
    > > I get this compilation error:
    > > error: 'assert' was not declared in this scope
    > > Server.tproj/RTSPRequestInterface.cpp:506: error: expected `;' before
    > > ')' token

    >
    > You need to include (in C) or (for C++). But
    > you can also leave it out - if select() returns and you were wai-
    > ting for a single file selector then there hardly should be a way
    > that it would return for a different file descriptor then the one
    > you were waiting for.
    > Regards, Jens
    > --
    > \ Jens Thoms Toerring ___ j...@toerring.de
    > \__________________________ http://toerring.de


    Thanks. I have another compiler error:

    error: 'FD_ISSET' was not declared in this scope

    >From here:

    http://scitsc.wlv.ac.uk/cgi-bin/mansec?3C+FD_ISSET

    I have add
    #include
    #include

    to my .cpp file, and yet, it still said FD_ISSET was not declared in
    the script.

    Thank you for any pointers.


  6. Re: How can I wait on a server socket for 10 seconds?

    Herman.Schultz@gmail.com wrote:
    > Thanks. I have another compiler error:


    > error: 'FD_ISSET' was not declared in this scope


    > >From here:

    > http://scitsc.wlv.ac.uk/cgi-bin/mansec?3C+FD_ISSET


    > I have add
    > #include
    > #include


    > to my .cpp file, and yet, it still said FD_ISSET was not declared in
    > the script.


    To be sure you got everthing I would recommend to also include

    #include
    #include

    - at least that's what the Linux man page for select(2) says.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

  7. Re: How can I wait on a server socket for 10 seconds?

    In article <1183659265.521843.223190@n2g2000hse.googlegroups.c om>,
    wrote:

    >Thanks. I have another compiler error:
    >
    > error: 'FD_ISSET' was not declared in this scope


    man socket

    --
    http://www.spinics.net/lists/

  8. Re: How can I wait on a server socket for 10 seconds?

    In article <5f4rhjF3beeihU1@mid.uni-berlin.de>,
    Jens Thoms Toerring wrote:

    >#include
    >#include
    >
    >- at least that's what the Linux man page for select(2) says.


    Mine says:

    #include
    #include

    --
    http://www.spinics.net/lists/

+ Reply to Thread