• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 680
  • Last Modified:

TInAddr

Hi, i would like to cast an 'TInAddr' to a String or the other way around.

I use C++ Builder 6.0

thanks
0
gregorydeceuninck
Asked:
gregorydeceuninck
  • 3
  • 3
1 Solution
 
jkrCommented:
You could use

char* pszAddr = "127.0.0.1";
TInAddr inaddr;

inaddr.S_un.S_addr = (u_long) inet_addr(pszAddr);

// ...

char* pstr = inet_ntoa(inaddr);
AnsiString as = StrPas(pstr);

0
 
gregorydeceuninckAuthor Commented:
I tried the last line of code 'StrPas' and got the following message:


[C++ Error] MainForm.cpp(69): E2034 Cannot convert 'const TInAddr' to 'const char *'
[C++ Error] MainForm.cpp(69): E2342 Type mismatch in parameter 'Str' (wanted 'const char *', got 'TInAddr')

Am i doing something wrong, do i have to include some header file????

here is the code i tried

void __fastcall TfrmMain::IPSServerStatus(TObject *Sender, DWORD Socket,
      TIpStatusType Event, const TIpConnRec &Connection,
      const TIpSockStatRec &StatRec)
{
        if(Event == stConnect)
        {
         ServerSocket = Socket;
         ServerAddr = Connection.RemoteAddr;
         IPCServer->DefaultPort = this->SSPort;
         AnsiString pstr = StrPas(Connection.RemoteAddr);
         //--make connection the the IP in pstr--
        }
        else if(Event == stDisconnect)
             {
              ServerSocket = 0;
             }
}
0
 
jkrCommented:
>>E2342 Type mismatch in parameter 'Str' (wanted 'const char *', got 'TInAddr')

The line

AnsiString pstr = StrPas(Connection.RemoteAddr);

should read

AnsiString pstr = StrPas(inet_ntoa(Connection.RemoteAddr));

You need to use 'inet_ntoa()' to convert the IP address into a 'char* first.


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gregorydeceuninckAuthor Commented:
compiler doesn't know 'inet_ntoa'
0
 
George TokasCommented:
Hello..
Where the hell is that onstatus???
Anyway assuming that connection and socket are represending the same connection then u can use:
AnsiString pstr = Socket->RemoteAddress;//it is of type ansistring.

gtokas.
0
 
jkrCommented:
>>compiler doesn't know 'inet_ntoa'

Oh, it does :o)

You need to

#include <winsock2.h>
0
 
gregorydeceuninckAuthor Commented:
thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now