000
21.09.2004, 19:26 Uhr
~Sunlight
Gast
|
Kompiliert erfolgreich, aber irgendwie will er nie eine socketverbindung aufbauen. zumindest fliegt immer bei der Abfrage nach erfolg mit Wert -1 raus. Warum?
Der Code stammt ursprünglich von meinem Linuxprogramm, mußte es etwas abwandeln, aber bisher halt nicht erfolgreich. Nun weiß ich nicht so recht weiter.
Hier der Source:
C++: |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <winsock2.h> /*#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h>*/ //#include <unistd.h> #include <process.h> #include <iostream.h> #include <conio.h>
#define startPort 0 #define endPort 4096
int main(int argc, char *argv[]) { struct sockaddr_in addr; struct servent *serv; int s, i; unsigned long begin, end, curr;
//check correct define of Ports to scan! if (startPort > endPort) { printf("sorry, wrong define of startPort and endPort\n"); return 0; } //wrong usage? --> display USAGE message if (argc < 3) { printf("IP Scanner\n"); printf("==========\n"); printf("scans all IPs from port 0 to 4096 and shows those are open\n"); printf("----------------------------------------------------------\n\n"); fprintf(stderr, "usage: %s <begin> <end>\n", argv[0]); return 1; }
begin = ntohl(inet_addr(argv[1])); end = ntohl(inet_addr(argv[2]));
//set if (begin > end) { curr = end; end = begin; begin = curr; }
//do for all IPs in Range //curr++ -> next IP :) for (curr = begin; curr <= end; curr++) { addr.sin_addr.s_addr = htonl(curr); printf("%s:\n", inet_ntoa(addr.sin_addr));
//i = Port to scan for (i = startPort; i < endPort; i++) { SOCKET socketClient; // SOCKET-Variablen für Client socketClient = socket(PF_INET, SOCK_STREAM, 0); if (socketClient == INVALID_SOCKET) { printf("Error Socket Erstellung: %i\n", socketClient); WSACleanup(); return -1; }
addr.sin_addr.s_addr = htonl(curr); addr.sin_port = htons((short)i); addr.sin_family = AF_INET;
printf("%i\r", i); fflush(stdout);
//connect and action... if (!connect(socketClient, (struct sockaddr*)&addr, sizeof(addr))) { serv = getservbyport(addr.sin_port, "tcp"); if (serv) printf("%i (%s) open\n", i, serv->s_name); else printf("%i (unknown) open\n", i); } //close socket! closesocket(socketClient); }
puts("-------------------------------"); }
return 0; }
|
Hilfe! |