[C++] wyświetlanie mojego adresu IP

0

Witam.

Mam taki kod (miał on wyświelać moje IP), przy próbie kompilacji DevC++ 4.9.9.1 (Windows Xp SP2)kompilator wysypuje mi takie błędy :

[Linker error] undefined reference to WSAStartup@8' [Linker error] undefined reference to WSACleanup@0'
[Linker error] undefined reference to gethostname@8' [Linker error] undefined reference to gethostbyname@4'
[Linker error] undefined reference to inet_ntoa@4' [Linker error] undefined reference to WSACleanup@0'

Nie zabardzo tego rozumiem a biblioteke winsock.h posiadam w domyślnym folderze więc raczej tu nie ma błędu.

#include<iostream>
#include<stdlib.h>
#include <winsock.h>

using namespace std;

int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 0 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
cout<<"error";
return 0;
}

if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 0 )
{
cout<<"error";
WSACleanup( );
return 0;
}

unsigned long max=MAX_COMPUTERNAME_LENGTH + 1;
char bufor[MAX_COMPUTERNAME_LENGTH + 1];

if(gethostname(bufor,max)==0)
{
hostent *MojHost;
MojHost=gethostbyname(bufor);
if(MojHost!=NULL)
{
in_addr adresIP;
memcpy(&adresIP, MojHost->h_addr, sizeof(in_addr));
cout<<"aj pi= "<<inet_ntoa(adresIP);

cout<<endl<<"nazwa kompa= "<<bufor;

cin.get();

WSACleanup( );
}
}
return 0;
}

Proszę o szybką odpowiedź.
Pozdrawiam Patryk[Patorituku]

0

Oto kod jak wyświetlić ip (programik w Builderze ale i po drobnych modyfikacjach powinien działać w DevC++):

//---------------------------------------------------------------------------

#include <vcl.h>
#include <winsock2.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 char buff[100];
  hostent *lphe;
  WSADATA WSAData;

  WSAStartup(MAKEWORD(2, 0), &WSAData);

  gethostname(buff, 100);
  lphe = gethostbyname(buff);
  Label1->Caption = AnsiString(inet_ntoa(*(in_addr*)*lphe->h_addr_list));
  WSACleanup();

}
//---------------------------------------------------------------------------
0

Taaaa....
Jeszcze tak zlinkuj z libwsock32 może...
-lwsock32

0

Tu masz wycinek programu:

AnsiString TForm1::GetLocalIP(void)
{
AnsiString retVal;
WSAData wsaData;

if(!WSAStartup(MAKEWORD(1, 1),&wsaData))
{
char BufHost[80];

if(gethostname(BufHost, sizeof(BufHost)) != SOCKET_ERROR)

{
hostent *phe = gethostbyname(BufHost);
if(phe)
{
in_addr addr;
for(int i = 0; phe->h_addr_list[i] != 0; i++)
{
CopyMemory(&addr, phe->h_addr_list[i], sizeof(in_addr));
if(i > 0) AppendStr(retVal, ".");
AppendStr(retVal, inet_ntoa(addr));
}
}
}
}
WSACleanup();

return retVal;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::IPClick(TObject *Sender)
{
Label1->Caption = GetLocalIP();
}

Pokazuje domyslnie on twoje IP na wszystkich interfejsach.

1 użytkowników online, w tym zalogowanych: 0, gości: 1