Herzlich Willkommen, lieber Gast!
  Sie befinden sich hier:

  Forum » VC++ / MFC » Socket Client - immer Fehler bei Socket Erstellung

Forum | Hilfe | Team | Links | Impressum | > Suche < | Mitglieder | Registrieren | Einloggen
  Quicklinks: MSDN-Online || STL || clib Reference Grundlagen || Literatur || E-Books || Zubehör || > F.A.Q. < || Downloads   

Autor Thread - Seiten: > 1 <
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!
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
001
21.09.2004, 21:08 Uhr
FloSoft
Medialer Over-Flow
(Administrator)


hi, unter windows musst du WSAStartup/WSACleanup aufrufen.
--
class God : public ChuckNorris { };
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
002
22.09.2004, 10:45 Uhr
Airdamn



dat is aber kein MFC...
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
003
22.09.2004, 11:04 Uhr
~Sunlight
Gast


Danke, WSAStartup/WSACleanup fehlte.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
004
22.09.2004, 14:35 Uhr
~Sunligth
Gast


hab ich nun wie gesagt zum laufen gebracht, aber leider scheint der Timeout von Windows dafür dann doch etwas groß eingestellt zu sein.
Wie kann ich den Timeout einstellen?
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
Seiten: > 1 <     [ VC++ / MFC ]  


ThWBoard 2.73 FloSoft-Edition
© by Paul Baecher & Felix Gonschorek (www.thwboard.de)

Anpassungen des Forums
© by Flo-Soft (www.flo-soft.de)

Sie sind Besucher: