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
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __WIN32__
#include <winsock2.h>
typedef int socklen_t;
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <netdb.h>
#include <errno.h>
#include <unistd.h>
#endif
const int NUM_PORTS = 4;
unsigned short PORTS[4] = {21, 22, 80, 6667};
const char *const HOST = "localhost";
int main(void)
{
int i;
#ifdef __WIN32__
WSADATA wsa_data;
WSAStartup(MAKEWORD(2, 2), &wsa_data);
#endif
for(i = 0; i < NUM_PORTS; i++)
{
int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == -1)
return 1;
struct sockaddr_in address;
struct hostent *h = gethostbyname(HOST);
address.sin_family = AF_INET;
address.sin_addr = *((struct in_addr*)h->h_addr);
address.sin_port = htons(PORTS[i]);
memset(&address.sin_zero, 0, 8);
if( (h == NULL)
|| (connect(sock, (struct sockaddr*)&address, sizeof(address)) == -1) )
{
#ifndef __WIN32__
shutdown(sock, SHUT_RDWR);
close(sock);
#else
closesocket(sock);
#endif
printf("Port %d fermé\n", PORTS[i]);
continue;
}
else
printf("Port %d ouvert\n", PORTS[i]);
}
return 0;
} |
Partager