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

Réseau C Discussion :

Socket windows et bug


Sujet :

Réseau C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Avatar de alpha_one_x86
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2006
    Messages
    411
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Somme (Picardie)

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

    Informations forums :
    Inscription : Décembre 2006
    Messages : 411
    Par défaut Socket windows et bug
    Bonjour, voila mon code:
    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
     
      //////////Create a socket////////////////////////
     
    //Create a SOCKET object called m_socket.
     
    SOCKET m_socket;
     
     
     
    // Call the socket function and return its value to the m_socket variable.
     
    // For this application, use the Internet address family, streaming sockets, and
     
    // the TCP/IP protocol.
     
    // using AF_INET family, TCP socket type and protocol of the AF_INET - IPv4
     
    m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
     
     
     
    // Check for errors to ensure that the socket is a valid socket.
     
    if (m_socket == INVALID_SOCKET)
     
    {
     
        MessageBox (NULL, TEXT("INVALID_SOCKET"),  TEXT("INVALID_SOCKET"), MB_OK);
     
        WSACleanup();
     
        return 0;
     
    }
     
    else
     
    {
     
        MessageBox (NULL, TEXT("Socket OK"),  TEXT("Socket OK"), MB_OK);
     
    }
     
     
     
    ////////////////bind()//////////////////////////////
     
    // Create a sockaddr_in object and set its values.
     
    sockaddr_in service;
     
     
     
    // AF_INET is the Internet address family.
     
    service.sin_family = AF_INET;
     
    // "127.0.0.1" is the local IP address to which the socket will be bound.
     
    service.sin_addr.s_addr = inet_addr(HOSTNAME);
     
    // 21 is the port number to which the socket will be bound.
     
    service.sin_port = htons(PORTNUM);
     
     
     
    // Call the bind function, passing the created socket and the sockaddr_in structure as parameters.
     
    // Check for general errors.
     
     
     
    if (bind(m_socket, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR)
     
    {
        closesocket(m_socket);
    	MessageBox (NULL, TEXT("SOCKET_ERROR"), TEXT("SOCKET_ERROR"), MB_OK);
        return 0;
     
    }
     
    else
     
    {
    		char temp[256];
    	int num=send(m_socket,temp,255,MSG_DONTROUTE);
    	if(num<0)
    		MessageBox (NULL, TEXT("num"),TEXT("<0"), MB_OK);
    	else
    		MessageBox (NULL, TEXT("Byte writen"),TEXT(">=0"), MB_OK);
    }
    Il m'execute: MessageBox (NULL, TEXT("SOCKET_ERROR"), TEXT("SOCKET_ERROR"), MB_OK);
    Je me suis trés fortement inspiré d'internet, il m'as l'aire correcte mais il ne marche pas, quelqu'un pourrai me débloquer?

  2. #2
    Rédacteur
    Avatar de 3DArchi
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    7 634
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 7 634
    Par défaut
    Bonjour,
    As-tu pensé à appeler WSAStartup avant toute chose ?

  3. #3
    Membre éclairé
    Avatar de alpha_one_x86
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2006
    Messages
    411
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Somme (Picardie)

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

    Informations forums :
    Inscription : Décembre 2006
    Messages : 411
    Par défaut
    Citation Envoyé par 3DArchi Voir le message
    Bonjour,
    As-tu pensé à appeler WSAStartup avant toute chose ?
    Juste avant j'ai:
    WSADATA WSAData;
    WSAStartup(MAKEWORD(2,0), &WSAData);
    Voila mon code complet:
    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
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    // tcp_client.cpp*: définit le point d'entrée pour l'application.
    //
     
    #include "stdafx.h"
    #include "tcp_client.h"
    #include <windows.h> 
    #include <winsock.h>
    #include <commctrl.h>
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
     
    //#pragma comment(lib, "ws2.lib")
    #define PORTNUM         8855          // Port number
    #define HOSTNAME        "192.168.64.245"   // Server name string
     
    #define MAX_LOADSTRING 100
     
    // Variables globales*:
    HINSTANCE			g_hInst;			// instance actuelle
    HWND				g_hWndMenuBar;		// handle de barre de menus
     
    // Pré-déclarations des fonctions incluses dans ce module de code*:
    ATOM			MyRegisterClass(HINSTANCE, LPTSTR);
    BOOL			InitInstance(HINSTANCE, int);
    LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
    INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
     
    int WINAPI WinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPTSTR    lpCmdLine,
                       int       nCmdShow)
    {
    	  int index = 0,                      // Integer index
          iReturn;                        // Return value of recv function
      char szClientA[100];                // ASCII string 
      TCHAR szClientW[100];               // Unicode string
      TCHAR szError[100];                 // Error message string
     
    //start example
     
    WSADATA WSAData;
    WSAStartup(MAKEWORD(2,0), &WSAData);
     
     
      //////////Create a socket////////////////////////
     
    //Create a SOCKET object called m_socket.
     
    SOCKET m_socket;
     
     
     
    // Call the socket function and return its value to the m_socket variable.
     
    // For this application, use the Internet address family, streaming sockets, and
     
    // the TCP/IP protocol.
     
    // using AF_INET family, TCP socket type and protocol of the AF_INET - IPv4
     
    m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
     
     
     
    // Check for errors to ensure that the socket is a valid socket.
     
    if (m_socket == INVALID_SOCKET)
     
    {
     
        MessageBox (NULL, TEXT("INVALID_SOCKET"),  TEXT("INVALID_SOCKET"), MB_OK);
     
        WSACleanup();
     
        return 0;
     
    }
     
    else
     
    {
     
        MessageBox (NULL, TEXT("Socket OK"),  TEXT("Socket OK"), MB_OK);
     
    }
     
     
     
    ////////////////bind()//////////////////////////////
     
    // Create a sockaddr_in object and set its values.
     
    sockaddr_in service;
     
     
     
    // AF_INET is the Internet address family.
     
    service.sin_family = AF_INET;
     
    // "127.0.0.1" is the local IP address to which the socket will be bound.
     
    service.sin_addr.s_addr = inet_addr(HOSTNAME);
     
    // 21 is the port number to which the socket will be bound.
     
    service.sin_port = htons(PORTNUM);
     
     
     
    // Call the bind function, passing the created socket and the sockaddr_in structure as parameters.
     
    // Check for general errors.
     
     
     
    if (bind(m_socket, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR)
     
    {
        closesocket(m_socket);
    	MessageBox (NULL, TEXT("SOCKET_ERROR"), TEXT("SOCKET_ERROR"), MB_OK);
        return 0;
     
    }
     
    else
     
    {
    		char temp[256];
    	int num=send(m_socket,temp,255,MSG_DONTROUTE);
    	if(num<0)
    		MessageBox (NULL, TEXT("num"),TEXT("<0"), MB_OK);
    	else
    		MessageBox (NULL, TEXT("Byte writen"),TEXT(">=0"), MB_OK);
    }
     
      //end example
     
     
      WSACleanup ();
     
    	MSG msg;
     
    	// Effectue l'initialisation de l'application*:
    	if (!InitInstance(hInstance, nCmdShow)) 
    	{
    		return FALSE;
    	}
     
    	HACCEL hAccelTable;
    	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TCP_CLIENT));
     
    	// Boucle de messages principale*:
    	while (GetMessage(&msg, NULL, 0, 0)) 
    	{
    		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
    		{
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    	}
     
    	return (int) msg.wParam;
    }
     
    //
    //  FONCTION*: MyRegisterClass()
    //
    //  BUT*: inscrit la classe de fenêtre.
    //
    //  COMMENTAIRES*:
    //
    ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
    {
    	WNDCLASS wc;
     
    	wc.style         = CS_HREDRAW | CS_VREDRAW;
    	wc.lpfnWndProc   = WndProc;
    	wc.cbClsExtra    = 0;
    	wc.cbWndExtra    = 0;
    	wc.hInstance     = hInstance;
    	wc.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TCP_CLIENT));
    	wc.hCursor       = 0;
    	wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    	wc.lpszMenuName  = 0;
    	wc.lpszClassName = szWindowClass;
     
    	return RegisterClass(&wc);
    }
     
    //
    //   FONCTION*: InitInstance(HINSTANCE, int)
    //
    //   BUT*: enregistre le handle de l'instance et crée une fenêtre principale
    //
    //   COMMENTAIRES*:
    //
    //        Dans cette fonction, nous enregistrons le handle de l'instance dans une variable globale, puis
    //        créons et affichons la fenêtre principale du programme.
    //
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
        HWND hWnd;
        TCHAR szTitle[MAX_LOADSTRING];		// texte de barre de titre
        TCHAR szWindowClass[MAX_LOADSTRING];	// nom de la classe de fenêtre principale
     
        g_hInst = hInstance; // Stocke le handle d'instance dans la variable globale
     
        // SHInitExtraControls doit être appelé une fois lors de l'initialisation de votre application afin d'initialiser
        // l'un des contrôles spécifiques au périphérique, tels que CAPEDIT et SIPPREF.
        SHInitExtraControls();
     
        LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 
        LoadString(hInstance, IDC_TCP_CLIENT, szWindowClass, MAX_LOADSTRING);
     
        //S'il s'exécute déjà, met le focus sur la fenêtre et quitte
        hWnd = FindWindow(szWindowClass, szTitle);	
        if (hWnd) 
        {
            // le focus défini sur la fenêtre enfant au tout premier plan
            // "| 0x00000001" est utilisé pour faire passer toutes les fenêtres possédées au premier plan et
            // les activer.
            SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
            return 0;
        } 
     
        if (!MyRegisterClass(hInstance, szWindowClass))
        {
        	return FALSE;
        }
     
        hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
     
        if (!hWnd)
        {
            return FALSE;
        }
     
        // Lorsque la fenêtre principale est créée à l'aide de CW_USEDEFAULT, la hauteur de la barre de menus (si une barre
        // de menus est créée) n'est pas prise en compte. Nous redimensionnons donc la fenêtre après sa création
        // si elle contient une barre de menus
        if (g_hWndMenuBar)
        {
            RECT rc;
            RECT rcMenuBar;
     
            GetWindowRect(hWnd, &rc);
            GetWindowRect(g_hWndMenuBar, &rcMenuBar);
            rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);
     
            MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
        }
     
        ShowWindow(hWnd, nCmdShow);
        UpdateWindow(hWnd);
     
     
        return TRUE;
    }
     
    //
    //  FONCTION*: WndProc(HWND, UINT, WPARAM, LPARAM)
    //
    //  BUT*:  traite les messages pour la fenêtre principale.
    //
    //  WM_COMMAND	- traite le menu de l'application
    //  WM_PAINT	- dessine la fenêtre principale
    //  WM_DESTROY	- génère un message d'arrêt et retourne
    //
    //
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        int wmId, wmEvent;
        PAINTSTRUCT ps;
        HDC hdc;
     
        static SHACTIVATEINFO s_sai;
     
        switch (message) 
        {
            case WM_COMMAND:
                wmId    = LOWORD(wParam); 
                wmEvent = HIWORD(wParam); 
                // Analyse les sélections de menu*:
                switch (wmId)
                {
                    case IDM_HELP_ABOUT:
                        DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
                        break;
                    case IDM_OK:
                        SendMessage (hWnd, WM_CLOSE, 0, 0);				
                        break;
                    default:
                        return DefWindowProc(hWnd, message, wParam, lParam);
                }
                break;
            case WM_CREATE:
                SHMENUBARINFO mbi;
     
                memset(&mbi, 0, sizeof(SHMENUBARINFO));
                mbi.cbSize     = sizeof(SHMENUBARINFO);
                mbi.hwndParent = hWnd;
                mbi.nToolBarId = IDR_MENU;
                mbi.hInstRes   = g_hInst;
     
                if (!SHCreateMenuBar(&mbi)) 
                {
                    g_hWndMenuBar = NULL;
                }
                else
                {
                    g_hWndMenuBar = mbi.hwndMB;
                }
     
                // Initialise la structure d'informations sur l'activation du shell
                memset(&s_sai, 0, sizeof (s_sai));
                s_sai.cbSize = sizeof (s_sai);
                break;
            case WM_PAINT:
                hdc = BeginPaint(hWnd, &ps);
     
                // TODO*: ajoutez ici le code de dessin...
     
                EndPaint(hWnd, &ps);
                break;
            case WM_DESTROY:
                CommandBar_Destroy(g_hWndMenuBar);
                PostQuitMessage(0);
                break;
     
            case WM_ACTIVATE:
                // Notifie le shell de notre message d'activation
                SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
                break;
            case WM_SETTINGCHANGE:
                SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
                break;
     
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
        }
        return 0;
    }
     
    // Gestionnaire de messages pour la boîte de dialogue À propos de.
    INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)
        {
            case WM_INITDIALOG:
                {
                    // Crée un bouton Terminé et le dimensionne.  
                    SHINITDLGINFO shidi;
                    shidi.dwMask = SHIDIM_FLAGS;
                    shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
                    shidi.hDlg = hDlg;
                    SHInitDialog(&shidi);
                }
                return (INT_PTR)TRUE;
     
            case WM_COMMAND:
                if (LOWORD(wParam) == IDOK)
                {
                    EndDialog(hDlg, LOWORD(wParam));
                    return TRUE;
                }
                break;
     
            case WM_CLOSE:
                EndDialog(hDlg, message);
                return TRUE;
     
    #ifdef _DEVICE_RESOLUTION_AWARE
            case WM_SIZE:
                {
    		DRA::RelayoutDialog(
    			g_hInst, 
    			hDlg, 
    			DRA::GetDisplayMode() != DRA::Portrait ? MAKEINTRESOURCE(IDD_ABOUTBOX_WIDE) : MAKEINTRESOURCE(IDD_ABOUTBOX));
                }
                break;
    #endif
        }
        return (INT_PTR)FALSE;
    }

  4. #4
    Rédacteur
    Avatar de 3DArchi
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    7 634
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 7 634
    Par défaut
    Tu es sur quel OS ?

  5. #5
    Membre éclairé
    Avatar de alpha_one_x86
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2006
    Messages
    411
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Somme (Picardie)

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

    Informations forums :
    Inscription : Décembre 2006
    Messages : 411
    Par défaut
    Windows CE.

  6. #6
    Expert confirmé
    Avatar de Melem
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2006
    Messages
    3 656
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Janvier 2006
    Messages : 3 656
    Par défaut
    Le problème vient de WSAStartup. Que retourne WSAGetLastError() ?

Discussions similaires

  1. Question sur les sockets [WINDOWS]
    Par lektrosonic dans le forum Réseau
    Réponses: 5
    Dernier message: 26/11/2007, 20h13
  2. [Reseau] probleme de socket windows
    Par le novice2 dans le forum Réseau
    Réponses: 4
    Dernier message: 24/07/2007, 19h13
  3. Socket windows/Linux
    Par yodaime dans le forum C++
    Réponses: 7
    Dernier message: 11/04/2006, 16h33
  4. [SOCKET] Windows et linux
    Par Gmrinfo dans le forum C++
    Réponses: 3
    Dernier message: 21/02/2006, 22h44
  5. [socket & windows & accept]
    Par Magique dans le forum Réseau
    Réponses: 7
    Dernier message: 30/04/2004, 23h34

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