Posted on 2003-12-03
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

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

Expert Comment

ID: 9866574
You could use

char* pszAddr = "";
TInAddr inaddr;

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

// ...

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


Author Comment

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;
LVL 86

Expert Comment

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.

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!


Author Comment

ID: 9866768
compiler doesn't know 'inet_ntoa'
LVL 16

Expert Comment

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

LVL 86

Accepted Solution

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

Oh, it does :o)

You need to

#include <winsock2.h>

Author Comment

ID: 9866841

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

729 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