By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,374 Members | 1,768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,374 IT Pros & Developers. It's quick & easy.

print IP number from received UDP packet

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

Jun 17 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Or******@gmail.com wrote:
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.
Jun 17 '07 #2

P: n/a

<Or******@gmail.com>
??????:11*********************@o61g2000hsh.googleg roups.com...
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
Jun 18 '07 #3

P: n/a
On Jun 18, 12:39 am, Ian Collins <ian-n...@hotmail.comwrote:
Orend...@gmail.com wrote:
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.
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 ;
}
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:ja*********@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

Jun 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.