On Jun 18, 12:39 am, Ian Collins <ian-n...@hotmail.comwrote:
Quote:
|
Orend...@gmail.com wrote:
|
Quote:
Quote:
I have a working UDP server in c++, how to I print the IP of the
received packet???
| |
Quote:
If you know how to access it and its type, the same as any other
variable of that type.
|
I don't know. IP addresses can be a bit strange. The obvious
answer is to define a user type IP, and provide a << operator
for it, but that still sort of begs the question. I've seen IP
addresses in three different formats: char[4], 32 bit int (or
unsigned int), in *network* byte order, and 32 bit int or
unsigned int in native byte order. None of these will give the
traditional a.b.c.d format when printed as a variable of that
type. You need some extra code:
For char[4]:
std::ostream&
operator<<(
std::ostream& dest,
Ip const& obj )
{
for ( int i = 0 ; i < 4 ; ++ i ) {
if ( i != 0 ) {
dest << '.' ;
}
dest << (int)(unsigned char)obj.value[ i ] ;
}
return dest ;
}
For unsigned int, native byte order:
std::ostream&
operator<<(
std::ostream& dest,
Ip const& obj )
{
int shift = 32 ;
while ( shift 0 ) {
shift -= 8 ;
dest << ((obj.value >shift) & 0xFF) ;
if ( shift != 0 ) {
dest << '.' ;
}
}
return dest ;
}
And network byte order:
std::ostream&
operator<<(
std::ostream& dest,
Ip const& obj )
{
unsigned char const*p
= reinterpret_cast< unsigned char const* >( &obj.value ) ;
for ( int i = 0 ; i < 4 ; ++ i ) {
if ( i != 0 ) {
dest << '.' ;
}
dest << (int)( p[ i ] ) ;
}
return dest ;
}
Quote:
If you don't know either of these, you are off topic and
should ask in a platform specific group to find out how.
|
How you get the IP depends on your platform or your library, but
formatting it, once you've got it, requires some work as well.
--
James Kanze (GABI Software, from CAI) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34