Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. any idea why my select does not work(visual c++ ?)

any idea why my select does not work(visual c++ ?)

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mugiwara
    wrote on last edited by
    #1

    #include "stdafx.h"
    #include <conio.h>
    #include <stdio.h>
    #include <Winsock2.h>
    #pragma comment(lib,"Ws2_32.lib")

    int _tmain(int argc, _TCHAR* argv[])
    {
    // Sleep for 2 sec
    struct timeval tv;
    tv.tv_sec = 2;
    tv.tv_usec = 0;

    printf("Time not elapsed");

    select(NULL, NULL, NULL, NULL, &tv);

    printf("\nTime elapsed");

    return 0;
    }

    There should be a kind of delay (2 seconds), but it does not work, i cant see any delay. what happening?

    H S C M 4 Replies Last reply
    0
    • M Mugiwara

      #include "stdafx.h"
      #include <conio.h>
      #include <stdio.h>
      #include <Winsock2.h>
      #pragma comment(lib,"Ws2_32.lib")

      int _tmain(int argc, _TCHAR* argv[])
      {
      // Sleep for 2 sec
      struct timeval tv;
      tv.tv_sec = 2;
      tv.tv_usec = 0;

      printf("Time not elapsed");

      select(NULL, NULL, NULL, NULL, &tv);

      printf("\nTime elapsed");

      return 0;
      }

      There should be a kind of delay (2 seconds), but it does not work, i cant see any delay. what happening?

      H Offline
      H Offline
      hanq_38910130
      wrote on last edited by
      #2

      1. You didn't call "WSAStartup" and "WSACleanup " 2. You didn't set any socket Besides, you should always check the return value for errorcode ;)

      M 1 Reply Last reply
      0
      • M Mugiwara

        #include "stdafx.h"
        #include <conio.h>
        #include <stdio.h>
        #include <Winsock2.h>
        #pragma comment(lib,"Ws2_32.lib")

        int _tmain(int argc, _TCHAR* argv[])
        {
        // Sleep for 2 sec
        struct timeval tv;
        tv.tv_sec = 2;
        tv.tv_usec = 0;

        printf("Time not elapsed");

        select(NULL, NULL, NULL, NULL, &tv);

        printf("\nTime elapsed");

        return 0;
        }

        There should be a kind of delay (2 seconds), but it does not work, i cant see any delay. what happening?

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        You aren't giving the select call any file descriptors to wait for, thus there are no blocking calls to be made, so there is no wait. The timeval is the MAXIMUM time the select will wait - if there's nothing for it to do, it won't wait.

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

        1 Reply Last reply
        0
        • M Mugiwara

          #include "stdafx.h"
          #include <conio.h>
          #include <stdio.h>
          #include <Winsock2.h>
          #pragma comment(lib,"Ws2_32.lib")

          int _tmain(int argc, _TCHAR* argv[])
          {
          // Sleep for 2 sec
          struct timeval tv;
          tv.tv_sec = 2;
          tv.tv_usec = 0;

          printf("Time not elapsed");

          select(NULL, NULL, NULL, NULL, &tv);

          printf("\nTime elapsed");

          return 0;
          }

          There should be a kind of delay (2 seconds), but it does not work, i cant see any delay. what happening?

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          You know there's a Sleep function, right :rolleyes: ? Because if the only thing you want to do is have a delay of 2 seconds, select is certainly not the best way to do it :)

          Cédric Moonen Software developer
          Charting control [v3.0] OpenGL game tutorial in C++

          1 Reply Last reply
          0
          • M Mugiwara

            #include "stdafx.h"
            #include <conio.h>
            #include <stdio.h>
            #include <Winsock2.h>
            #pragma comment(lib,"Ws2_32.lib")

            int _tmain(int argc, _TCHAR* argv[])
            {
            // Sleep for 2 sec
            struct timeval tv;
            tv.tv_sec = 2;
            tv.tv_usec = 0;

            printf("Time not elapsed");

            select(NULL, NULL, NULL, NULL, &tv);

            printf("\nTime elapsed");

            return 0;
            }

            There should be a kind of delay (2 seconds), but it does not work, i cant see any delay. what happening?

            M Offline
            M Offline
            Moak
            wrote on last edited by
            #5

            I second Cédric, Sleep()[^] would be the simpler way for a two seconds delay.

            Webchat in Europe :java: Now with 26% more Twitter

            1 Reply Last reply
            0
            • H hanq_38910130

              1. You didn't call "WSAStartup" and "WSACleanup " 2. You didn't set any socket Besides, you should always check the return value for errorcode ;)

              M Offline
              M Offline
              Mugiwara
              wrote on last edited by
              #6

              Thank you, that was it.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups