1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| #include <iostream>
#include <winsock2.h>
#include <fcntl.h>
#include <sys/fcntl.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
/* Correction : Utilise la version 2,2 ;) */
WSADATA WSAData;
WSAStartup(MAKEWORD(2,2), &WSAData);
SOCKET sock;
SOCKADDR_IN sin;
unsigned long nonblocking = 1;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr("195.175.37.8");
sin.sin_port = htons(80);
sock = socket(PF_INET, SOCK_STREAM, 0);
/* LINUX: */
//fcntl (sock, F_SETFL, O_NONBLOCK);
/* WINDOWS: */
if(ioctlsocket(sock, FIONBIO, &nonblocking)<0)
{
cout << WSAGetLastError() << endl;
cerr << "La socket ne peut être rendu non-bloquante" << endl;
cerr << "On continue quand même avec socket bloquante. Patience." <<endl;
}
connect(sock, (SOCKADDR *)&sin, sizeof(sin));
if(WSAGetLastError() != WSAEWOULDBLOCK)
{ // LINUX : EINPROGRESS
cout<<"Erreur 0: "<<WSAGetLastError()<<endl;
exit(1);
}
else
{
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(sock, &readfds);
struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 500*1000;
int r;
r=select(sock+1, &readfds, NULL, NULL, &tv);
if(r<0)
{
cout << "Erreur 1: "<< WSAGetLastError() << endl;
exit(1);
}
else if(r==0)
{
cout << "Erreur 2: "<< WSAGetLastError() << endl;
exit(1);
}
else
{
cout<<"Le proxy a répondu favorablement. Tout semble ok."<<endl;
}
}
WSACleanup();
return 0;
} |
Partager