Solved

TInAddr

Posted on 2003-12-03
7
572 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now