📄 ip_port.h
字号:
#ifndef _IP_PORT_H
#define _IP_PORT_H
#include "trans_ip.h"
class ip_port
{
CString combine; // "192.168.0.90:8102
CString ip; // "192.168.0.90"
USHORT port; // 8102, look port
// 解析IP和端口
void decompose()
{
combine.Remove(_T(' '));
if( combine.IsEmpty() )
return;
port = 0;
ip.Empty();
int idx = combine.Find(_T(":"));
if( idx == -1 )
{
idx = combine.GetLength();
port = 8101;
}
else port = _ttoi((LPCTSTR)combine + idx + 1 );
ip = combine.Left(idx);
}
void compose()
{
combine.Format(_T("%s:%d"), ip, port);
}
public:
friend bool operator==(const ip_port&, const ip_port&);
ip_port(): port(0) {}
ip_port(LPCTSTR i, USHORT p) {set(i,p);}
ip_port(ULONG i, USHORT p){set(i,p);}
ip_port(const ip_port& ref) { *(this) = ref; }
ip_port(LPCTSTR ipport) { combine = ipport; decompose(); }
ip_port& operator=(const ip_port& ref)
{
set(ref.ip, ref.port);
return *this;
}
void set(ULONG i, USHORT p)
{
set(trans_ipUC(i), p);
}
void set(LPCTSTR i, USHORT p)
{
ip = i;
port = p;
compose();
}
ULONG get_ip() const {return trans_ipCU(ip);}
USHORT look_port() const {return port;}
USHORT link_port() const {return trans_port(port); }
CString str_ip() {return ip;}
ip_port& operator=(int i)
{
ASSERT(i==0);
combine.Empty();
ip.Empty();
port = 0;
return *this;
}
operator LPCTSTR() const
{
return combine;
}
operator bool() const
{
return !ip.IsEmpty();
}
bool operator!() const
{
return !(bool)*this;
}
};
inline bool operator==(const ip_port& ip1, const ip_port& ip2)
{
return (ip1.ip == ip2.ip) && (ip1.port == ip2.port);
}
inline bool operator!=(const ip_port& ip1, const ip_port& ip2)
{
return !(ip1 == ip2);
}
#endif // _IP_PORT_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -