sockaddr.h
上传用户:psq1974
上传日期:2007-01-06
资源大小:1195k
文件大小:3k
- /* Copyright (C) 1998, 1999 State University of New York at Stony Brook
- Author: Andrew V. Shuvalov ( andrew@ecsl.cs.sunysb.edu )
- Software license is located in file "COPYING"
- */
- #ifndef _sockaddr_h_
- #define _sockaddr_h_
- #ifndef _WIN32
- # include <sys/socket.h>
- # include <netinet/in.h>
- # include <netdb.h>
- # include <arpa/inet.h>
- #endif
- #include <ostream.h>
- #include <string>
- #ifdef _WIN32
- # define STD std::
- #else
- # define STD /* */
- #endif
- #ifdef _WIN32
- # define snprintf _snprintf
- #endif
- //! author="Andrew V. Shuvalov"
- /** hides all operations with struct sockaddr_in
- */
- class Sockaddr {
- public:
- sockaddr_in addr;
- public:
- Sockaddr()
- {
- addr.sin_family = 0;
- addr.sin_port = 0;
- }
- Sockaddr( const Sockaddr &a )
- {
- *this = a;
- }
- Sockaddr( const STD string &hostname, const STD string &s_port )
- {
- int port = 0;
- sscanf( s_port.c_str(), "%d", &port );
- construct( hostname, port );
- }
- /** construct from hostname or numeric IP address and optional
- port number */
- Sockaddr( const STD string &hostname, int port = 0 )
- {
- construct( hostname, port );
- }
- void construct( const STD string &hostname, int port = 0 )
- {
- addr.sin_family = AF_INET;
- // detect if site is hostname or ip addr
- if( hostname[0] < '0' || hostname[0] > '9' )
- {
- // hostname
- struct hostent *he = gethostbyname( hostname.c_str() );
- if( he == NULL )
- {
- perror( "gethostbyname" );
- return;
- }
- addr.sin_addr = *(struct in_addr *) he->h_addr;
- }
- else
- {
- // just numbers of IP address
- # ifndef _WIN32
- if( !inet_aton( hostname.c_str(), &addr.sin_addr ))
- {
- perror( "Error converting inet_aton" );
- return;
- }
- # else
- {
- // windoze don't have inet_aton function
- unsigned long int ia = inet_addr( hostname.c_str() );
- if( ia == INADDR_NONE )
- {
- perror( "Error converting inet_aton" );
- return;
- }
- addr.sin_addr.s_addr = ia;
- }
- # endif
- }
- // non-zero port will indicate non-error
- addr.sin_port = htons( port );
- }
- const Sockaddr &operator =( const Sockaddr &a ) {
- if( this != &a ) {
- addr = a.addr;
- }
- return *this;
- }
- bool operator <( const Sockaddr &a ) const {
- if( addr.sin_port == a.addr.sin_port )
- {
- if( addr.sin_addr.s_addr < a.addr.sin_addr.s_addr )
- return true;
- else
- return false;
- }
- else
- {
- if( addr.sin_port < a.addr.sin_port )
- return true;
- else
- return false;
- }
- }
- bool operator ==( const Sockaddr &a ) const {
- if( addr.sin_port == a.addr.sin_port )
- if( addr.sin_addr.s_addr == a.addr.sin_addr.s_addr )
- return true;
- return false;
- }
- STD string get_description() const {
- char buf[100];
- snprintf( buf, 100, "addr 0x%x port %d", ntohl( addr.sin_addr.s_addr ),
- ntohs( addr.sin_port ) );
- return STD string( buf );
- }
- };
- /** prints itself */
- inline ostream &operator <<( ostream &os, const Sockaddr &a )
- {
- char buf[100];
- snprintf( buf, 100, "addr 0x%x port %d", ntohl( a.addr.sin_addr.s_addr ),
- ntohs( a.addr.sin_port ) );
- os << buf;
- return os;
- }
- #endif // _sockaddr_h_