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

Boost C++ Discussion :

Récupérer l'IP du poste


Sujet :

Boost C++

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    198
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2009
    Messages : 198
    Points : 101
    Points
    101
    Par défaut Récupérer l'IP du poste
    bonjour,

    Je cherche à récupérer l'adresse IP du PC sous Windows sur lequel s'exécute le programme.
    J'ai trouvé sur Internet deux codes utilisant boos asio mais je n'arrive pas à les faire fonctionner.

    J'utilise Code::Blocks et boost 1.44.

    Premier 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
    #include <iostream>
    #include "boost\asio.hpp"
     
    using namespace std;
     
    int main()
    {
     
        boost::asio::ip::tcp::socket socket(boost::asio::io_service);
        std::string s = socket.remote_endpoint().address().to_string();
     
        cout << "IP : " << s << endl;
        return 0;
    }
    A la compilation j'obtiens une flopée de warnings et l'erreur

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    main.cpp|10|error: request for member 'remote_endpoint' in 'socket', which is of non-class type 'boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >(boost::asio::io_service)'|
    Log complète :
    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
    D:\boost_1_44_0\boost\asio\detail\config.hpp|59|warning: #warning Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately.|
    D:\boost_1_44_0\boost\asio\detail\config.hpp|60|warning: #warning For example, add -D_WIN32_WINNT=0x0501 to the compiler command line.|
    D:\boost_1_44_0\boost\asio\detail\config.hpp|61|warning: #warning Assuming _WIN32_WINNT=0x0501 (i.e. Windows XP target).|
    D:\boost_1_44_0\boost\asio\detail\impl\win_iocp_handle_service.ipp||In constructor 'boost::asio::detail::win_iocp_handle_service::overlapped_wrapper::overlapped_wrapper(boost::system::error_code&)':|
    D:\boost_1_44_0\boost\asio\detail\impl\win_iocp_handle_service.ipp|49|warning: dereferencing type-punned pointer will break strict-aliasing rules|
    D:\boost_1_44_0\boost\asio\ip\impl\address_v6.ipp||In constructor 'boost::asio::ip::address_v6::address_v6()':|
    D:\boost_1_44_0\boost\asio\ip\impl\address_v6.ipp|37|warning: missing braces around initializer for 'in6_addr::<anonymous union>'|
    D:\boost_1_44_0\boost\asio\ip\impl\address_v6.ipp|37|warning: missing braces around initializer for 'u_char [16]'|
    D:\boost_1_44_0\boost\asio\ip\impl\address_v6.ipp|259|warning: missing braces around initializer for 'in6_addr::<anonymous union>'|
    D:\boost_1_44_0\boost\asio\ip\impl\address_v6.ipp|259|warning: missing braces around initializer for 'u_char [16]'|
    D:\boost_1_44_0\boost\asio\ip\detail\impl\endpoint.ipp||In constructor 'boost::asio::ip::detail::endpoint::endpoint(int, short unsigned int)':|
    D:\boost_1_44_0\boost\asio\ip\detail\impl\endpoint.ipp|60|warning: missing braces around initializer for 'in6_addr::<anonymous union>'|
    D:\boost_1_44_0\boost\asio\ip\detail\impl\endpoint.ipp|60|warning: missing braces around initializer for 'u_char [16]'|
    H:\main.cpp||In function 'int main()':|
    H:\main.cpp|10|error: request for member 'remote_endpoint' in 'socket', which is of non-class type 'boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >(boost::asio::io_service)'|
    D:\boost_1_44_0\boost\system\error_code.hpp|214|warning: 'boost::system::posix_category' defined but not used|
    D:\boost_1_44_0\boost\system\error_code.hpp|215|warning: 'boost::system::errno_ecat' defined but not used|
    D:\boost_1_44_0\boost\system\error_code.hpp|216|warning: 'boost::system::native_ecat' defined but not used|
    D:\boost_1_44_0\boost\asio\error.hpp|251|warning: 'boost::asio::error::system_category' defined but not used|
    D:\boost_1_44_0\boost\asio\error.hpp|253|warning: 'boost::asio::error::netdb_category' defined but not used|
    D:\boost_1_44_0\boost\asio\error.hpp|255|warning: 'boost::asio::error::addrinfo_category' defined but not used|
    D:\boost_1_44_0\boost\asio\error.hpp|257|warning: 'boost::asio::error::misc_category' defined but not used|
    D:\boost_1_44_0\boost\asio\error.hpp|259|warning: 'boost::asio::error::ssl_category' defined but not used|
    D:\boost_1_44_0\boost\asio\detail\winsock_init.hpp|78|warning: 'boost::asio::detail::winsock_init_instance' defined but not used|
    ||=== Build finished: 1 errors, 19 warnings ===|
    Deuxième 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
    #include <iostream>
    #include "boost\asio.hpp"
     
    using namespace std;
     
    int main()
    {
     
        using boost::asio::ip::tcp;
     
        boost::asio::io_service io_service;
        tcp::resolver resolver(io_service);
        tcp::resolver::query query(boost::asio::ip::host_name(), "");
        tcp::resolver::iterator iter = resolver.resolve(query);
        tcp::resolver::iterator end; // End marker.
        while (iter != end)
        {
            tcp::endpoint ep = *iter++;
            std::cout << ep << std::endl;
        }
     
        return 0;
    }
    J'obtiens de nombreuses erreurs et cherchant sur Internet j'ai rajouté pour le linker le chemin C:\Windows\system32, mais j'ai quand même ça :
    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
    D:\boost_1_44_0\boost\asio\detail\config.hpp|59|warning: #warning Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately.|
    D:\boost_1_44_0\boost\asio\detail\config.hpp|60|warning: #warning For example, add -D_WIN32_WINNT=0x0501 to the compiler command line.|
    D:\boost_1_44_0\boost\asio\detail\config.hpp|61|warning: #warning Assuming _WIN32_WINNT=0x0501 (i.e. Windows XP target).|
    D:\boost_1_44_0\boost\asio\detail\impl\win_iocp_handle_service.ipp||In constructor 'boost::asio::detail::win_iocp_handle_service::overlapped_wrapper::overlapped_wrapper(boost::system::error_code&)':|
    D:\boost_1_44_0\boost\asio\detail\impl\win_iocp_handle_service.ipp|49|warning: dereferencing type-punned pointer will break strict-aliasing rules|
    D:\boost_1_44_0\boost\asio\ip\impl\address_v6.ipp||In constructor 'boost::asio::ip::address_v6::address_v6()':|
    D:\boost_1_44_0\boost\asio\ip\impl\address_v6.ipp|37|warning: missing braces around initializer for 'in6_addr::<anonymous union>'|
    D:\boost_1_44_0\boost\asio\ip\impl\address_v6.ipp|37|warning: missing braces around initializer for 'u_char [16]'|
    D:\boost_1_44_0\boost\asio\ip\impl\address_v6.ipp|259|warning: missing braces around initializer for 'in6_addr::<anonymous union>'|
    D:\boost_1_44_0\boost\asio\ip\impl\address_v6.ipp|259|warning: missing braces around initializer for 'u_char [16]'|
    D:\boost_1_44_0\boost\asio\ip\detail\impl\endpoint.ipp||In constructor 'boost::asio::ip::detail::endpoint::endpoint(int, short unsigned int)':|
    D:\boost_1_44_0\boost\asio\ip\detail\impl\endpoint.ipp|60|warning: missing braces around initializer for 'in6_addr::<anonymous union>'|
    D:\boost_1_44_0\boost\asio\ip\detail\impl\endpoint.ipp|60|warning: missing braces around initializer for 'u_char [16]'|
    d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\crt2.o:crt1.c|| undefined reference to `SetUnhandledExceptionFilter@4'|
    d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\crt2.o:crt1.c|| undefined reference to `ExitProcess@4'|
    d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\crtbegin.o:cygming-crtbegin.c|| undefined reference to `GetModuleHandleA@4'|
    d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\crtbegin.o:cygming-crtbegin.c|| undefined reference to `GetProcAddress@8'|
    obj\main.o:main.cpp|| undefined reference to `TlsFree@4'|
    obj\main.o:main.cpp|| undefined reference to `TlsFree@4'|
    obj\main.o:main.cpp|| undefined reference to `InterlockedIncrement@4'|
    obj\main.o:main.cpp|| undefined reference to `WSAStartup@8'|
    obj\main.o:main.cpp|| undefined reference to `InterlockedExchange@8'|
    obj\main.o:main.cpp|| undefined reference to `InterlockedDecrement@4'|
    obj\main.o:main.cpp|| undefined reference to `WSASetLastError@4'|
    obj\main.o:main.cpp|| undefined reference to `WSAAddressToStringA@20'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `WSAGetLastError@0'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::generic_category()'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::generic_category()'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `WSASetLastError@4'|
    obj\main.o:main.cpp|| undefined reference to `gethostname@8'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `WSAGetLastError@0'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `ntohs@4'|
    obj\main.o:main.cpp|| undefined reference to `htons@4'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `ntohs@4'|
    obj\main.o:main.cpp|| undefined reference to `ntohl@4'|
    obj\main.o:main.cpp|| undefined reference to `htonl@4'|
    obj\main.o:main.cpp|| undefined reference to `ntohs@4'|
    obj\main.o:main.cpp|| undefined reference to `ntohl@4'|
    obj\main.o:main.cpp|| undefined reference to `htonl@4'|
    obj\main.o:main.cpp|| undefined reference to `htons@4'|
    obj\main.o:main.cpp|| undefined reference to `ntohl@4'|
    obj\main.o:main.cpp|| undefined reference to `htonl@4'|
    obj\main.o:main.cpp|| undefined reference to `ntohs@4'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `WSACleanup@0'|
    ::~winsock_init()]+0xe)||undefined reference to `InterlockedDecrement@4'|
    ::~winsock_init()]+0x1e)||undefined reference to `WSACleanup@0'|
    ))]+0x43)||undefined reference to `EnterCriticalSection@4'|
    ))]+0xac)||undefined reference to `LeaveCriticalSection@4'|
    ))]+0xdf)||undefined reference to `LeaveCriticalSection@4'|
    ))]+0x116)||undefined reference to `EnterCriticalSection@4'|
    ||More errors follow but not being shown.|
    ||Edit the max errors limit in compiler options...|
    ||=== Build finished: 50 errors, 10 warnings ===|
    Normalement je ne devrais rien avoir besoin de plus que les headers de boost, et les codes semblent correctes. Je ne comprends d'où viennent les problèmes.
    Une idée ?

  2. #2
    Membre émérite

    Inscrit en
    Mai 2008
    Messages
    1 014
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 1 014
    Points : 2 252
    Points
    2 252
    Par défaut
    Bonjour,

    Pour le premier code, il ne faut pas être impressionné par boost::Asio et ses types à rallonge (genre boost::asio::ip::tcp::socket). Si tu regardes bien, la construction de la variable socket n'a aucun sens, car tu passes un type en paramètre. Ça serait comme écrire:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    boost::asio::ip::tcp::socket socket(int) // erreur, int est un type;
    boost::asio::ip::tcp::socket socket(float*); // pareil
    boost::asio::ip::tcp::socket socket(std::string); // pareil
    boost::asio::ip::tcp::socket socket(boost::asio::io_service); // pareil
    Pour le deuxième code, j'ai l'impression que tu as oublié de linker avec les lib qui vont bien. Voir par exemple ce post.

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Juillet 2009
    Messages
    198
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2009
    Messages : 198
    Points : 101
    Points
    101
    Par défaut
    Dans l'onglet Linker Settings j'ai ajouté libws2_32.a de ..\CodeBlocks\MinGW\lib.
    Maintenant j'ai cà :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::generic_category()'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::generic_category()'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| undefined reference to `boost::system::system_category()'|
    obj\main.o:main.cpp|| more undefined references to `boost::system::system_category()' follow|
    ||=== Build finished: 12 errors, 0 warnings ===|
    Pour le premier code on trouve sur Internet des bouts de code avec cette construction du socket. Et si l'on ne met que ce code le compilateur accepte.

  4. #4
    zul
    zul est déconnecté
    Membre éclairé Avatar de zul
    Profil pro
    Inscrit en
    Juin 2002
    Messages
    498
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 498
    Points : 699
    Points
    699
    Par défaut
    Il faut linker avec boost_system (et probablement boost_thread aussi).

Discussions similaires

  1. Réponses: 1
    Dernier message: 09/07/2007, 14h13
  2. [AJAX] Impossible de récupérer les valeurs en Post
    Par Tommyl dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 19/06/2007, 20h30
  3. [PEAR][HTML_QuickForm] Comment récupérer valeurs hierselect par POST ?
    Par petchos dans le forum Bibliothèques et frameworks
    Réponses: 6
    Dernier message: 07/06/2007, 14h10
  4. Réponses: 17
    Dernier message: 01/06/2007, 08h16
  5. [XML] comment récupérer un arbre xml posté par http ?
    Par tomperso dans le forum XML/XSL et SOAP
    Réponses: 2
    Dernier message: 02/07/2004, 10h29

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