1e254a845SShuo Chen// Copyright 2010, Shuo Chen. All rights reserved. 2e254a845SShuo Chen// http://code.google.com/p/muduo/ 3e254a845SShuo Chen// 4e254a845SShuo Chen// Use of this source code is governed by a BSD-style license 5e254a845SShuo Chen// that can be found in the License file. 6e254a845SShuo Chen 7e254a845SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 8e254a845SShuo Chen// 9e254a845SShuo Chen// This is a public header file, it must only include public header files. 10e254a845SShuo Chen 11e254a845SShuo Chen#ifndef MUDUO_NET_INETADDRESS_H 12e254a845SShuo Chen#define MUDUO_NET_INETADDRESS_H 13e254a845SShuo Chen 14e254a845SShuo Chen#include "datetime/copyable.h" 15e254a845SShuo Chen 16e254a845SShuo Chen#include <string> 17e254a845SShuo Chen 18e254a845SShuo Chen#include <netinet/in.h> 19e254a845SShuo Chen 20e254a845SShuo Chennamespace muduo 21e254a845SShuo Chen{ 22e254a845SShuo Chen 23e254a845SShuo Chen/// 24e254a845SShuo Chen/// Wrapper of sockaddr_in. 25e254a845SShuo Chen/// 26e254a845SShuo Chen/// This is an POD interface class. 27e254a845SShuo Chenclass InetAddress : public muduo::copyable 28e254a845SShuo Chen{ 29e254a845SShuo Chen public: 30e254a845SShuo Chen /// Constructs an endpoint with given port number. 31e254a845SShuo Chen /// Mostly used in TcpServer listening. 32e254a845SShuo Chen explicit InetAddress(uint16_t port); 33e254a845SShuo Chen 34e254a845SShuo Chen /// Constructs an endpoint with given ip and port. 35e254a845SShuo Chen /// @c ip should be "1.2.3.4" 36e254a845SShuo Chen InetAddress(const std::string& ip, uint16_t port); 37e254a845SShuo Chen 38e254a845SShuo Chen /// Constructs an endpoint with given struct @c sockaddr_in 39e254a845SShuo Chen /// Mostly used when accepting new connections 40e254a845SShuo Chen InetAddress(const struct sockaddr_in& addr) 41e254a845SShuo Chen : addr_(addr) 42e254a845SShuo Chen { } 43e254a845SShuo Chen 44e254a845SShuo Chen std::string toHostPort() const; 45e254a845SShuo Chen 46e254a845SShuo Chen // default copy/assignment are Okay 47e254a845SShuo Chen 48e254a845SShuo Chen const struct sockaddr_in& getSockAddrInet() const { return addr_; } 49e254a845SShuo Chen void setSockAddrInet(const struct sockaddr_in& addr) { addr_ = addr; } 50e254a845SShuo Chen 51e254a845SShuo Chen private: 52e254a845SShuo Chen struct sockaddr_in addr_; 53e254a845SShuo Chen}; 54e254a845SShuo Chen 55e254a845SShuo Chen} 56e254a845SShuo Chen 57e254a845SShuo Chen#endif // MUDUO_NET_INETADDRESS_H 58