Solved

TInAddr

Posted on 2003-12-03
7
597 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

776 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