any idea why my select does not work(visual c++ ?)
-
#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?
-
#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?
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 ;)
-
#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?
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!
-
#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?
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++ -
#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?
-
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 ;)