Socket -> Ip

CyberP Messages postés 283 Date d'inscription mercredi 8 janvier 2003 Statut Membre Dernière intervention 4 juillet 2007 - 15 avril 2003 à 08:44
NitRic Messages postés 402 Date d'inscription mardi 1 mai 2001 Statut Membre Dernière intervention 15 août 2011 - 20 avril 2003 à 06:15
Comment obtenir l'adresse ip (sous forme de char* : xxx.xxx.xxx.xxx) à partir d'un objet socket (je ne veut pas d'objet SOCK_ADDR mais une ip en char*)

CyberP,
Celui qui dit que toute question a une réponse

1 réponse

NitRic Messages postés 402 Date d'inscription mardi 1 mai 2001 Statut Membre Dernière intervention 15 août 2011
20 avril 2003 à 06:15
Voilà un exemple récupérer sur une faq Winsock, c'est plus rapide que d'écrire le code mais ca revient au même alors voilà:

#include
#include <winsock.h>

int doit(int, char **)
{
char ac[80];
if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) {
cerr << "Error " << WSAGetLastError() <<
" when getting local host name." << endl;
return 1;
}
cout << "Host name is " << ac << "." << endl;

struct hostent *phe = gethostbyname(ac);
if (phe == 0) {
cerr << "Yow! Bad host lookup." << endl;
return 1;
}

for (int i = 0; phe->h_addr_list[i] != 0; ++i) {
struct in_addr addr;
memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
cout << "Address " << i << ": " << inet_ntoa(addr) << endl;
}

return 0;
}

int main(int argc, char *argv[])
{
WSAData wsaData;
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
return 255;
}

int retval = doit(argc, argv);

WSACleanup();

return retval;
}

Adresse de la FAQ

-->> Winsock Programmer's FAQ <<--
-->> http://tangentsoft.net/wskfaq/ <<--

Ne pas oublier de linker ws2_32.lib ;)

~(.:: NitRic ::.)~
0
Rejoignez-nous