19a1e991dSShuo Chen// Copyright 2010, Shuo Chen. All rights reserved. 29a1e991dSShuo Chen// http://code.google.com/p/muduo/ 39a1e991dSShuo Chen// 49a1e991dSShuo Chen// Use of this source code is governed by a BSD-style license 59a1e991dSShuo Chen// that can be found in the License file. 69a1e991dSShuo Chen 79a1e991dSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 89a1e991dSShuo Chen// 99a1e991dSShuo Chen// This is a public header file, it must only include public header files. 109a1e991dSShuo Chen 119a1e991dSShuo Chen#ifndef MUDUO_NET_INETADDRESS_H 129a1e991dSShuo Chen#define MUDUO_NET_INETADDRESS_H 139a1e991dSShuo Chen 149a1e991dSShuo Chen#include "datetime/copyable.h" 159a1e991dSShuo Chen 169a1e991dSShuo Chen#include <string> 179a1e991dSShuo Chen 189a1e991dSShuo Chen#include <netinet/in.h> 199a1e991dSShuo Chen 209a1e991dSShuo Chennamespace muduo 219a1e991dSShuo Chen{ 229a1e991dSShuo Chen 239a1e991dSShuo Chen/// 249a1e991dSShuo Chen/// Wrapper of sockaddr_in. 259a1e991dSShuo Chen/// 269a1e991dSShuo Chen/// This is an POD interface class. 279a1e991dSShuo Chenclass InetAddress : public muduo::copyable 289a1e991dSShuo Chen{ 299a1e991dSShuo Chen public: 309a1e991dSShuo Chen /// Constructs an endpoint with given port number. 319a1e991dSShuo Chen /// Mostly used in TcpServer listening. 329a1e991dSShuo Chen explicit InetAddress(uint16_t port); 339a1e991dSShuo Chen 349a1e991dSShuo Chen /// Constructs an endpoint with given ip and port. 359a1e991dSShuo Chen /// @c ip should be "1.2.3.4" 369a1e991dSShuo Chen InetAddress(const std::string& ip, uint16_t port); 379a1e991dSShuo Chen 389a1e991dSShuo Chen /// Constructs an endpoint with given struct @c sockaddr_in 399a1e991dSShuo Chen /// Mostly used when accepting new connections 409a1e991dSShuo Chen InetAddress(const struct sockaddr_in& addr) 419a1e991dSShuo Chen : addr_(addr) 429a1e991dSShuo Chen { } 439a1e991dSShuo Chen 449a1e991dSShuo Chen std::string toHostPort() const; 459a1e991dSShuo Chen 469a1e991dSShuo Chen // default copy/assignment are Okay 479a1e991dSShuo Chen 489a1e991dSShuo Chen const struct sockaddr_in& getSockAddrInet() const { return addr_; } 499a1e991dSShuo Chen void setSockAddrInet(const struct sockaddr_in& addr) { addr_ = addr; } 509a1e991dSShuo Chen 519a1e991dSShuo Chen private: 529a1e991dSShuo Chen struct sockaddr_in addr_; 539a1e991dSShuo Chen}; 549a1e991dSShuo Chen 559a1e991dSShuo Chen} 569a1e991dSShuo Chen 579a1e991dSShuo Chen#endif // MUDUO_NET_INETADDRESS_H 58