Se vuoi per maggior chiarezza ti evidenzio le parti incriminate del file...
#include <stdio.h>
#include <winsock.h>
#define PORT 80
<font color="limegreen"><b>void client(char *);</font id="limegreen"></b>
int InitializeWinsock(WORD );
int main()
{
char string1[30], string2[10];
int n=0, c=0;
for(n=0;n<1000;n++){ //i tentativi si effettuano fino a
www.vecchio1000.it
strcpy(string1, "www.vecchio");
sprintf(string2, "%i", n);
strcat(string1, string2);
strcat(string1, ".it");
//pero' forse devi convertire la stringa in ip non lo so come si fa mi dispiace
if (!InitializeWinsock (MAKEWORD(1,1) ) ) return 1;
<font color="limegreen"><b>if(!client(string1))continue;</b></font id="limegreen">
else {WSACleanup();c=1;break;} //se ti connetti metti c=1 ed esci da ciclo
}
if(!c)printf("Non sono riuscito a collegarmi a nessun sito\n");
else printf("Mi sono collegato al sito %s\n", string1);
return 0;
<font color="limegreen"><b>}</b></font id="limegreen">
int client(char * server_IP)
{
SOCKET fd; // "file" descriptor for the network socket
SOCKADDR_IN saddr;
<font color="limegreen"><b>char buf[MAX_STRING];</font id="limegreen"></b>
if ((fd=socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET)
printf("Errore durante la creazione del socket\n");
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = inet_addr(server_IP);
saddr.sin_port = htons(PORT);
// request connection to server socket (remote machine)
if (connect(fd, (LPSOCKADDR) &saddr, sizeof(saddr)) == SOCKET_ERROR)
return 0; //se non si connette ritorna 0
if (closesocket(fd) == SOCKET_ERROR)
printf("Errore durante la chiusura del socket\n");
return 1;
}
/**********************************************
* Windows Sockets Initialization *
**********************************************/
int InitializeWinsock(WORD wVersionRequested)
{
WSADATA wsaData;
int err;
err = WSAStartup(wVersionRequested, &wsaData);
// ritorna errore se, ad esempio, l'applicazione supporta al massimo
// la versione 1.1 e la DLL supporta da 2.0 in su (le versioni non si sovrappongono)
if (err!=0) return 0; // Tell the user that we couldn't find a usable winsock.dll
// WSAStartup returns in wHighVersion (struct wsaData) the highest version it supports
// and in wVersion the minimum of its high version and wVersionRequested.
// wVersion is the The version of the Windows Sockets specification
// that the Windows Sockets DLL expects the caller to use.
// Se WSAStartup ritorna un risultato accettabile, l'applicazione deve ancora
// verificare che il risultato sia compatibile con la sua richiesta. Ad esempio,
// con wVersionRequested=1.1 e DLL version 1.0, wVersion=1.0. Se l'applicazione
// vuole assolutamente usare la DLL 1.1, deve ancora verificare di non trovarsi
// in questo caso
// Tell the user that we couldn't find a usable winsock.dll.
if (LOBYTE(wsaData.wVersion )!=1 || HIBYTE(wsaData.wVersion)!=1) return 0;
//Everything is fine: proceed
return 1;
}