Solved

TInAddr

Posted on 2003-12-03
7
622 Views
Last Modified: 2012-06-21
Hi, i would like to cast an 'TInAddr' to a String or the other way around.

I use C++ Builder 6.0

thanks
0
Comment
Question by:gregorydeceuninck
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9866574
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
 

Author Comment

by:gregorydeceuninck
ID: 9866649
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
 
LVL 86

Expert Comment

by:jkr
ID: 9866689
>>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
Industry Leaders: 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!

 

Author Comment

by:gregorydeceuninck
ID: 9866768
compiler doesn't know 'inet_ntoa'
0
 
LVL 16

Expert Comment

by:George Tokas
ID: 9866785
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
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 9866805
>>compiler doesn't know 'inet_ntoa'

Oh, it does :o)

You need to

#include <winsock2.h>
0
 

Author Comment

by:gregorydeceuninck
ID: 9866841
thanks
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question