12a18e699SShuo Chen// Copyright 2010, Shuo Chen. All rights reserved. 22a18e699SShuo Chen// http://code.google.com/p/muduo/ 32a18e699SShuo Chen// 42a18e699SShuo Chen// Use of this source code is governed by a BSD-style license 52a18e699SShuo Chen// that can be found in the License file. 62a18e699SShuo Chen 72a18e699SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 82a18e699SShuo Chen// 92a18e699SShuo Chen// This is a public header file, it must only include public header files. 102a18e699SShuo Chen 112a18e699SShuo Chen#ifndef MUDUO_NET_INETADDRESS_H 122a18e699SShuo Chen#define MUDUO_NET_INETADDRESS_H 132a18e699SShuo Chen 142a18e699SShuo Chen#include "datetime/copyable.h" 152a18e699SShuo Chen 162a18e699SShuo Chen#include <string> 172a18e699SShuo Chen 182a18e699SShuo Chen#include <netinet/in.h> 192a18e699SShuo Chen 202a18e699SShuo Chennamespace muduo 212a18e699SShuo Chen{ 222a18e699SShuo Chen 232a18e699SShuo Chen/// 242a18e699SShuo Chen/// Wrapper of sockaddr_in. 252a18e699SShuo Chen/// 262a18e699SShuo Chen/// This is an POD interface class. 272a18e699SShuo Chenclass InetAddress : public muduo::copyable 282a18e699SShuo Chen{ 292a18e699SShuo Chen public: 302a18e699SShuo Chen /// Constructs an endpoint with given port number. 312a18e699SShuo Chen /// Mostly used in TcpServer listening. 322a18e699SShuo Chen explicit InetAddress(uint16_t port); 332a18e699SShuo Chen 342a18e699SShuo Chen /// Constructs an endpoint with given ip and port. 352a18e699SShuo Chen /// @c ip should be "1.2.3.4" 362a18e699SShuo Chen InetAddress(const std::string& ip, uint16_t port); 372a18e699SShuo Chen 382a18e699SShuo Chen /// Constructs an endpoint with given struct @c sockaddr_in 392a18e699SShuo Chen /// Mostly used when accepting new connections 402a18e699SShuo Chen InetAddress(const struct sockaddr_in& addr) 412a18e699SShuo Chen : addr_(addr) 422a18e699SShuo Chen { } 432a18e699SShuo Chen 442a18e699SShuo Chen std::string toHostPort() const; 452a18e699SShuo Chen 462a18e699SShuo Chen // default copy/assignment are Okay 472a18e699SShuo Chen 482a18e699SShuo Chen const struct sockaddr_in& getSockAddrInet() const { return addr_; } 492a18e699SShuo Chen void setSockAddrInet(const struct sockaddr_in& addr) { addr_ = addr; } 502a18e699SShuo Chen 512a18e699SShuo Chen private: 522a18e699SShuo Chen struct sockaddr_in addr_; 532a18e699SShuo Chen}; 542a18e699SShuo Chen 552a18e699SShuo Chen} 562a18e699SShuo Chen 572a18e699SShuo Chen#endif // MUDUO_NET_INETADDRESS_H 58