Connecting Tech Pros Worldwide Help | Site Map

print IP number from received UDP packet

 
LinkBack Thread Tools Search this Thread
  #1  
Old June 17th, 2007, 10:35 PM
Orendavd@gmail.com
Guest
 
Posts: n/a
Default print IP number from received UDP packet

hi there,
I have a working UDP server in c++, how to I print the IP of the
received packet???


  #2  
Old June 17th, 2007, 10:45 PM
Ian Collins
Guest
 
Posts: n/a
Default Re: print IP number from received UDP packet

Orendavd@gmail.com wrote:
Quote:
hi there,
I have a working UDP server in c++, how to I print the IP of the
received packet???
>
If you know how to access it and its type, the same as any other
variable of that type. If you don't know either of these, you are off
topic and should ask in a platform specific group to find out how.

--
Ian Collins.
  #3  
Old June 18th, 2007, 01:15 AM
vietor liu
Guest
 
Posts: n/a
Default Re: print IP number from received UDP packet


<Orendavd@gmail.com>
??????:1182119469.037903.62270@o61g2000hsh.googleg roups.com...
Quote:
hi there,
I have a working UDP server in c++, how to I print the IP of the
received packet???
>
>
test use socket api ,function recv() parameter

Vietor Liu


  #4  
Old June 18th, 2007, 07:55 AM
James Kanze
Guest
 
Posts: n/a
Default Re: print IP number from received UDP packet

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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.