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
| #include <cstdlib>
#include <iostream>
#include <winsock2.h>
using namespace std;
int main(int argc, char *argv[])
{
int flag;
int j=0;
int t[200];
for(int i=1;i<1024;i++){
WSADATA var_init;
WSAStartup(MAKEWORD(2,2),&var_init);
SOCKET sock;
sock=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
sockaddr_in connection;
connection.sin_family=AF_INET;
connection.sin_addr.s_addr=INADDR_ANY;
connection.sin_port=htonl(i);
flag=bind(sock,(SOCKADDR*) &connection,sizeof(connection));
cout<<i<<" "<<flag<<" "<<GetLastError()<<endl;
if(flag==WSAEADDRINUSE){
t[j]=i;
j++;
}
closesocket(sock);
WSACleanup();
}
for(int i=0;i<j;i++){
cout<<"le port : "<<t[j]<<" est occupé"<<endl;
}
system("PAUSE");
return EXIT_SUCCESS;
} |