IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Discussion :

problème de socket dans une application win32


Sujet :

Windows

  1. #1
    Membre régulier
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2005
    Messages
    119
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mai 2005
    Messages : 119
    Points : 106
    Points
    106
    Par défaut problème de socket dans une application win32
    Bonjour à tous,

    Voila , après avoir crée un chat , je me suis dit pourquoi pas un petit scanneur de port. Je pensai la besogne facile et en principe elle l'est seulement là je ne voi absolument pas la solution au problème, pour moi tout devrai aller comme sur des roulettes :s . Le problème est que on dirait que la fonction connect n'attend pas d'avoir trouver une connexion ou non, elle passe directement, ce qui fait que tout mes ports sont soit disant fermé mais c'est faux. Je donne le code sans grand espoir que l'ont trouve la solution car comme je l'ai dit il n'y a pas d'erreur dans le code , j'en suis quasi certain. Mais je serai très content si quelqu'un peut m'aider

    Le voici. (j'utilise un thread pour le scan)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    #include <windows.h>
    #include <winsock2.h>
    #include <commctrl.h>
    
    #define IDB_SCAN 1
    #define IDB_STOP 2
    #define IDB_SAVINFILE 3
    
    typedef struct d_lpParam{
    	char szIP[15+1];
    	unsigned int uiStartPort;
    	unsigned int uiEndPort;
    	HWND hwListV;
    }LPPARAMTHREAD;
    
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    void __stdcall GetPorts(char * szPorts,unsigned int * uiStart,unsigned int * uiEnd);
    void Tproc(LPPARAMTHREAD * lpParam);
    
    char szClassName[ ] = "CodeBlocksWindowsApp";
    HINSTANCE GlobalHInstance;
    
    WSADATA wsaData;
    SOCKET sTarget;
    SOCKADDR_IN saiTarget = {0};
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR lpszArgument,
                         int nFunsterStil)
    {
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;
        wincl.style = CS_DBLCLKS;
        wincl.cbSize = sizeof (WNDCLASSEX);
    
    
        wincl.hIcon = LoadIcon (hThisInstance,"A");
        wincl.hIconSm = LoadIcon (hThisInstance,"A");
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;
        wincl.cbClsExtra = 0;
        wincl.cbWndExtra = 0;
        wincl.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
    
    
        if (!RegisterClassEx (&wincl))
            return 0;
    
    	/* Initialisation du WSADATA. */
    	WSAStartup(MAKEWORD(2,0),&wsaData);
    	/* -- fin initialisation -- */
    
    	if(sTarget = socket(AF_INET,SOCK_STREAM,0) == -1)return 1;
    
    	saiTarget.sin_family = AF_INET;
    
    	InitCommonControls();
        GlobalHInstance = hThisInstance;
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "DCKScan",       /* Title Text */
               WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               544,                 /* The programs width */
               375,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        ShowWindow (hwnd, nFunsterStil);
    
        while (GetMessage (&messages, NULL, 0, 0))
        {
            TranslateMessage(&messages);
            DispatchMessage(&messages);
        }
        return messages.wParam;
    }
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	static HWND hwEditLimit,hwEditIP;
    	static HWND hwListVPorts;
    	static HWND hwButtonScan,hwButtonStop,hwSaveInFile;
    	HWND hwLabelLimit;
        switch (message)
        {
        	case WM_CREATE : hwListVPorts = CreateWindow(WC_LISTVIEW,0,LVS_REPORT | WS_VISIBLE | WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_TABSTOP,
    										10,20,200,300,hwnd,NULL,GlobalHInstance,NULL);
    
    						 LV_COLUMN lvcCol;
    
    						 lvcCol.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_FMT;
    						 lvcCol.cx = 100;
    						 lvcCol.pszText = "Ports";
    						 ListView_InsertColumn(hwListVPorts,1,&lvcCol);
    
    						 lvcCol.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_FMT;
    						 lvcCol.cx = 100;
    						 lvcCol.pszText = "Status";
    						 ListView_InsertColumn(hwListVPorts,1,&lvcCol);
    
    						 /* Appliquer les styles etendu à la listview.
    						  * 0x1000+54 = LVM_SETEXTENDEDLISTVIEWSTYLE.
    						  * 0x20 = LVM_EX_FULLROWSELECT.
    						  */
    						 SendMessage(hwListVPorts,0x1000+54,0x20,0x20);
    
    						 hwEditIP = CreateWindowEx(WS_EX_CLIENTEDGE,"edit","127.0.0.1",WS_VISIBLE | WS_CHILD | WS_TABSTOP,
    									230,50,100,20,hwnd,NULL,GlobalHInstance,NULL);
    						 hwEditLimit = CreateWindowEx(WS_EX_CLIENTEDGE,"edit","0,65535",WS_VISIBLE | WS_CHILD | WS_TABSTOP,
    										230,90,100,20,hwnd,NULL,GlobalHInstance,NULL);
    
    						 hwButtonScan = CreateWindow("button","Scan",WS_VISIBLE | WS_CHILD | WS_TABSTOP,
    										230,130,100,20,hwnd,(HMENU)IDB_SCAN,GlobalHInstance,NULL);
    						 hwButtonStop = CreateWindow("button","Stop",WS_VISIBLE | WS_CHILD | WS_TABSTOP,
    										230,150,100,20,hwnd,(HMENU)IDB_STOP,GlobalHInstance,NULL);
    
    						 HFONT PoliceTahoma = CreateFont(13,0,0,0,0,0,0,0,0,0,0,0,0,"tahoma");
    
    						 SendMessage(hwListVPorts,WM_SETFONT,(long)PoliceTahoma,0);
    						 SendMessage(hwEditIP,WM_SETFONT,(long)PoliceTahoma,0);
    						 SendMessage(hwEditLimit,WM_SETFONT,(long)PoliceTahoma,0);
    						 SendMessage(hwButtonScan,WM_SETFONT,(long)PoliceTahoma,0);
    						 SendMessage(hwButtonStop,WM_SETFONT,(long)PoliceTahoma,0);
            break;
            case WM_COMMAND : switch(HIWORD(wParam)){
    								case BN_CLICKED : switch(LOWORD(wParam)){
    														case IDB_SCAN : { char szPorts[11+1]="";
    																		  DWORD dwTid;
    																		  static LPPARAMTHREAD lpParam;
    
    																		  GetWindowText(hwEditLimit,szPorts,11+1);
    
    																		  if(!*szPorts){
    																			MessageBox(hwnd,"Please select ports.","Error :",MB_OK | MB_ICONERROR);
    																			SetFocus(hwEditLimit);
    																		  }
    																		  GetPorts(szPorts,&(lpParam.uiStartPort),&(lpParam.uiEndPort));
    
    																		  GetWindowText(hwEditIP,lpParam.szIP,15+1);
    																		  if(!*(lpParam.szIP)){
    																		  	MessageBox(hwnd,"Please enter a ip.","Error",MB_OK | MB_ICONERROR);
    																		  	SetFocus(hwEditIP);
    																		  }
    
    
    																		  lpParam.hwListV = hwListVPorts;
    
    																		  CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Tproc,(LPPARAMTHREAD*)&lpParam,0,&dwTid);
    
    																		}
    														break;
    														case IDB_STOP :
    														break;
    														case IDB_SAVINFILE :
    														break;
    												  }
    								break;
    						  }
            break;
            case WM_DESTROY:
    			WSACleanup();
                PostQuitMessage (0);
                break;
            default:
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    
    /* TASK : Récupère les ports de début et de fin pour le scan. */
    void __stdcall GetPorts(char * szPorts,unsigned int * uiStart,unsigned int * uiEnd){
    	char szStart[5+1]="";
    	char * ptStart = szStart;
    	char * ptPorts = szPorts;
    
    	do{
    		*ptStart = *ptPorts;
    
    		++ptStart;
    		++ptPorts;
    	}while(*ptPorts != ',' && *ptPorts);
    
    	++ptStart = 0;
    	++ptPorts; /* Passe la virgule. */
    
    	*uiStart = atoi(szStart);
    	*uiEnd = atoi(ptPorts);
    
    }
    
    void Tproc(LPPARAMTHREAD * lpParam){
    
    	int uiX;
    	unsigned short usCount;
    	char szPort[5+1];
    	LV_ITEM lviItem;
    	SOCKET test;
    
    
    	lviItem.mask = LVIF_TEXT;
    
    	saiTarget.sin_addr.s_addr = inet_addr(lpParam->szIP);
    
    	for(uiX = lpParam->uiStartPort;uiX <= lpParam->uiEndPort;++uiX){
    		saiTarget.sin_port = htons(uiX);
    
    		itoa(uiX,szPort,10);
    
    		lviItem.iItem = 65535;
    		lviItem.iSubItem = 0;
    		lviItem.pszText = szPort;
    
    		usCount = ListView_InsertItem(lpParam->hwListV,&lviItem);
    
    		lviItem.iItem = usCount;
    		lviItem.iSubItem = 1;
    		test = connect(sTarget,(SOCKADDR*)&saiTarget,sizeof(saiTarget));
    		if(test == -1)lviItem.pszText = "Close";
    		else lviItem.pszText = "Open";
    
    		ListView_SetItem(lpParam->hwListV,&lviItem);
    
    	}
    	closesocket(sTarget);
    
    }
    ++

    deck_bsd

    /*************************************/
    [http://dckapps.azurewebsites.net/]

  2. #2
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    test = connect(sTarget,(SOCKADDR*)&saiTarget,sizeof(saiTarget));
    connect() ne s'emploie pas ainsi.

    PS: Beurk! Tu as COMBIEN de tabs par indentation?? On n'y voit rien dans ton code!
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  3. #3
    Membre régulier
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2005
    Messages
    119
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mai 2005
    Messages : 119
    Points : 106
    Points
    106
    Par défaut
    Ben si :s ca fait un bail que je l'emploie comme cela :s, et mon chat va nikel :s
    ++

    deck_bsd

    /*************************************/
    [http://dckapps.azurewebsites.net/]

  4. #4
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 519
    Points
    41 519
    Par défaut
    Je me demande ce que j'ai bien pu avoir cru lire pour écrire une connerie pareille. Si je me souviens bien, j'ai du croire sur le coup que tu avais écrit sTarget = connect(...), mais le texte cité prouve lui-même à combien j'avais tort...

    Je n'avais pourtant pas bu ce jour-là...
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  5. #5
    Membre régulier
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2005
    Messages
    119
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mai 2005
    Messages : 119
    Points : 106
    Points
    106
    Par défaut
    Et bien c'est que tu avais soif , mais cela ne résoud pas mon problème . Je relit ce foutu code depuis hier, et sérieusement je sèche :s.
    ++

    deck_bsd

    /*************************************/
    [http://dckapps.azurewebsites.net/]

  6. #6
    Membre régulier
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2005
    Messages
    119
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mai 2005
    Messages : 119
    Points : 106
    Points
    106
    Par défaut
    J'AI TROUVE ... Il fallait la voir, et encore je voi pas pourquoi cela était une faute. Le problème venai en fait du fait que je faisait directement le test de la fonction socket, il faut le faire après celle-ci, mais faut pas me demander pourquoi ... raaaa suis content.
    ++

    deck_bsd

    /*************************************/
    [http://dckapps.azurewebsites.net/]

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. problème d'authentification dans une application windows
    Par meryDev dans le forum Windows Forms
    Réponses: 4
    Dernier message: 15/07/2009, 17h05
  2. Mélanger code et données dans une application Win32 ?
    Par Forthman dans le forum x86 32-bits / 64-bits
    Réponses: 2
    Dernier message: 27/10/2008, 22h28
  3. Problème d'affichage dans une application
    Par altaruk dans le forum Débuter
    Réponses: 3
    Dernier message: 26/01/2008, 18h32
  4. Problème de refresh dans une application modulaire
    Par TigrouMeow dans le forum Windows Forms
    Réponses: 8
    Dernier message: 11/10/2007, 15h06
  5. Réponses: 3
    Dernier message: 18/09/2007, 14h54

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo