& y) {
if (x != y) {
return false;
if (x.address() != y.address()) {
return false;
return x.port() == y.port();
IPEndPoint.cpp
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#ifdef _WIN32
#include <WS2tcpip.h>
#else
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#endif
#include <string>
#include <boost/asio.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/ip/tcp.hpp>
#include "Ipep.h"
#include "IPEndPoint.h"
#define IP_END_POINT_USE_BOOST_INET_PTON
namespace Server {
namespace Net {
IPEndPoint::IPEndPoint(const char* address, int port)
: _AddressFamily(AddressFamily::InterNetwork)
, Port(port) {
if (NULL == address || *address == '\x0') {
*(UInt32*)this->_AddressBytes = 0;
this->_AddressFamily = AddressFamily::InterNetwork;
else {
#ifndef IP_END_POINT_USE_BOOST_INET_PTON
struct sockaddr_in6 in6;
int err = inet_pton(AF_INET6, address, &in6);
if (err > 0) {
this->_AddressFamily = AddressFamily::InterNetworkV6;
memcpy(this->_AddressBytes, &in6.sin6_addr, sizeof(this->_AddressBytes));
else {
*(UInt32*)this->_AddressBytes = inet_addr(address);
this->_AddressFamily = AddressFamily::InterNetwork;
#else
boost::system::error_code ec;
boost::asio::ip::address host;
try {
host = boost::asio::ip::address::from_string(address, ec);
catch (std::exception&) {
ec = boost::asio::error::invalid_argument;
if (ec) {
this->_AddressFamily = AddressFamily::InterNetwork;
*(UInt32*)this->_AddressBytes = IPEndPoint::NoneAddress;
else if (host.is_v6()) {
this->_AddressFamily = AddressFamily::InterNetworkV6;
boost::asio::ip::address_v6::bytes_type buf = host.to_v6().to_bytes();
memcpy(this->_AddressBytes, buf.data(), buf.size());
else {
this->_AddressFamily = AddressFamily::InterNetwork;
boost::asio::ip::address_v4::bytes_type buf = host.to_v4().to_bytes();
memcpy(this->_AddressBytes, buf.data(), buf.size());
#endif
IPEndPoint::IPEndPoint(AddressFamily af, const void* address_bytes, int address_size, int port)
: _AddressFamily(af)
, Port(port) {
int limit_size = 0;
if (af == AddressFamily::InterNetworkV6) {
limit_size = sizeof(struct in6_addr);
else {
af = AddressFamily::InterNetwork;
limit_size = sizeof(struct in_addr);
memset(this->_AddressBytes, 0, limit_size);
if (NULL != address_bytes && address_size > 0) {
memcpy(this->_AddressBytes, address_bytes, std::min(address_size, limit_size));
this->_AddressFamily = af;
std::string IPEndPoint::GetHostName() noexcept {
char szz[256]; // 域名规定不超过64字节(但是几乎大部分实现为64-1字节)
int err = gethostname(szz, 0xff);
if (err <= 0) {
return "localhost";
return szz;
std::string IPEndPoint::ToString() noexcept {
return Ipep::ToIpepAddress(this);
std::string IPEndPoint::ToAddressString(AddressFamily af, const Byte* address_bytes, int address_size) noexcept {
if (NULL == address_bytes || address_size <= 0) {
return "0.0.0.0";
if (af == AddressFamily::InterNetworkV6) {
if (address_size < (int)sizeof(struct in6_addr)) {
return "0.0.0.0";
char sz[INET6_ADDRSTRLEN];
if (!inet_ntop(AF_INET6, (struct in6_addr*)address_bytes, sz, sizeof(sz))) {
return "0.0.0.0";
return sz;
else {
if (address_size < (int)sizeof(struct in_addr)) {
return "0.0.0.0";
char sz[INET_ADDRSTRLEN];
if (!inet_ntop(AF_INET, (struct in_addr*)address_bytes, sz, sizeof(sz))) {
return "0.0.0.0";
return sz; // inet_ntoa(*(struct in_addr*)address);
IPEndPoint类包含应用程序连接到主机上的服务所需的主机和本地或远程端口信息。通过组合服务的主机IP地址和端口号,IPEndPoint类形成到服务的连接点,它主要用来将网络端点表示为IP地址和端口号。IPEndPoint类的常用字段、属性及说明如下表所示。
表 IPEndPoint类的常用字段、属性及说明
字段及属性
MaxPort字段
指定可以分...
我一直在学习一些套接字编程,我创建了一个异步服务器,只监听并返回数据 .但我在这里有一个问题,我在IPEndPoint(IPAddress.any,端口)的服务器上使用IPEndPoint,我也在客户端做同样的事情 . 运行服务器和客户端时,我使用try catch,客户端返回它的异常“请求的地址在其上下文0.0.0.0:port中无效”以下是代码:Server.csPublic void Lis...
IP addresses in C# 在.Net网络库里面最大的优点就是IP地址和端口被成对处理,相比于UNIX中用的方法真是一个巨大的令人欢迎的进步。.NET定义了两个类来处理关于IP地址的问题。 One of the biggest advantages you will notice in the .NET network library is the way IP address/port pairs are handled. It is a fairly straightforward proc.
IPEndPoint(IPAddress.Any, 0)
If creates an IPEndpoint using any available IP address on the local computer, and any available port number.
IPAddress.Any代表本机上的所有IP地址,MSDN上说是“一个 IP 地址,指示服务器应侦听所有网络接口
IPEndPoint继承与EndPoint。IPEndPoint引用的时候参数为IP和端口。IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
.net中 Bind(EndPoint localEP);方法参数是EndPoint,填入IPEndPoint会按照EndPoint参数执行。
EndPoint是IPEndPoint的基类,IP...
// 实名发送
IPEndPoint localIpep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 25555); // 本机IP,指定的端口号 ...
IPEndPoint是ip和端口对的组合,如“127.0.0.1:1000”
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint localEndPoint = new IPEndPoint(ipAddress,11000);
在.Net网络库里面最大的优点就是IP地址和端口被成对处理,相比于UNIX中用的方法真是一个巨大的令人欢迎的进步。.NET定义了两个类来处理关于IP地址的问题。
One of the biggest advantages you will notice in the .NET network library is the way IP address/port pairs are ha...