POCO库中文编程参考指南(3)Poco::Net::Socket

简介: POCO库中文编程参考指南(3)Poco::Net::Socket 作者:柳大·Poechant 博客:Blog.CSDN.net/Poechant 邮箱:zhongchao.ustc#gmail.

POCO库中文编程参考指南(3)Poco::Net::Socket

  • 作者:柳大·Poechant
  • 博客:Blog.CSDN.net/Poechant
  • 邮箱:zhongchao.ustc#gmail.com (# -> @)
  • 日期:April 14th, 2012

1 SelectMode

enum SelectMode
    /// The mode argument to poll() and select().
{
    SELECT_READ  = 1,
    SELECT_WRITE = 2,
    SELECT_ERROR = 4
};

2 SocketList

typedef std::vector<Socket> SocketList;

3 构造函数

未初始化的 socket:

Socket();

拷贝构造函数

Socket(const Socket& socket);

4 重载运算符

赋值运算符:

Socket& operator = (const Socket& socket);

比较运算符:

bool operator == (const Socket& socket) const;
bool operator != (const Socket& socket) const;
bool operator <  (const Socket& socket) const;
bool operator <= (const Socket& socket) const;
bool operator >  (const Socket& socket) const;
bool operator >= (const Socket& socket) const;

5 常用 socket 操作

返回该 socket 的可读数据的字节数,该操作不引起 socket 阻塞:

int available() const;

关闭 socket:

void close();

poll:

bool poll(const Poco::Timespan& timeout, int mode) const;
    /// Determines the status of the socket, using a 
    /// call to select().
    /// 
    /// The mode argument is constructed by combining the values
    /// of the SelectMode enumeration.
    ///
    /// Returns true if the next operation corresponding to
    /// mode will not block, false otherwise.

SocketImpl* impl() const;
    /// Returns the SocketImpl for this socket.

检查这个 socket 的连接是否是安全的(使用 SSL 或 TLS):

bool secure() const;

6 缓冲区

发送数据的缓冲区:

void setSendBufferSize(int size);
int getSendBufferSize() const;

接收数据的缓冲区:

void setReceiveBufferSize(int size);
int getReceiveBufferSize() const;

7 超时时间

发送数据的超时时间:

void setSendTimeout(const Poco::Timespan& timeout);
Poco::Timespan getSendTimeout() const;

接收数据的超时时间:

void setReceiveTimeout(const Poco::Timespan& timeout);
Poco::Timespan getReceiveTimeout() const;

8 其他接口

void setOption(int level, int option, int value);
void setOption(int level, int option, unsigned value);
void setOption(int level, int option, unsigned char value);
void setOption(int level, int option, const Poco::Timespan& value);
void setOption(int level, int option, const IPAddress& value);

void getOption(int level, int option, int& value) const;
void getOption(int level, int option, unsigned& value) const;
void getOption(int level, int option, unsigned char& value) const;
void getOption(int level, int option, Poco::Timespan& value) const;
void getOption(int level, int option, IPAddress& value) const;

void setLinger(bool on, int seconds);
void getLinger(bool& on, int& seconds) const;

void setNoDelay(bool flag);
bool getNoDelay() const;

void setKeepAlive(bool flag);
bool getKeepAlive() const;

void setReuseAddress(bool flag);
bool getReuseAddress() const;

void setReusePort(bool flag);
bool getReusePort() const;

void setOOBInline(bool flag);
bool getOOBInline() const;

void setBlocking(bool flag);
bool getBlocking() const;

获取 socket 的 IP 和端口:

SocketAddress address() const;

获取 peer socket 的 IP 地址和端口:

SocketAddress peerAddress() const;

9 静态函数

select:

static int select(SocketList& readList,
                  SocketList& writeList, 
                  SocketList& exceptList, 
                  const Poco::Timespan& timeout);

    /// Determines the status of one or more sockets, 
    /// using a call to select().
    ///
    /// ReadList contains the list of sockets which should be
    /// checked for readability.
    ///
    /// WriteList contains the list of sockets which should be
    /// checked for writeability.
    ///
    /// ExceptList contains a list of sockets which should be
    /// checked for a pending error.
    ///
    /// Returns the number of sockets ready.
    ///
    /// After return, 
    ///   * readList contains those sockets ready for reading,
    ///   * writeList contains those sockets ready for writing,
    ///   * exceptList contains those sockets with a pending error.
    ///
    /// If the total number of sockets passed in readList, writeList and
    /// exceptList is zero, select() will return immediately and the
    /// return value will be 0.
    ///
    /// If one of the sockets passed to select() is closed while
    /// select() runs, select will return immediately. However,
    /// the closed socket will not be included in any list.
    /// In this case, the return value may be greater than the sum
    /// of all sockets in all list.

检查是否支持 IPv4 或 IPv6:

static bool supportsIPv4();
static bool supportsIPv6();

-

from:Blog.CSDN.net/Poechant

目录
相关文章
|
10天前
|
C#
一个库帮你轻松的创建漂亮的.NET控制台应用程序
一个库帮你轻松的创建漂亮的.NET控制台应用程序
|
3天前
|
存储 算法 网络协议
【探索Linux】P.26(网络编程套接字基本概念—— socket编程接口 | socket编程接口相关函数详细介绍 )
【探索Linux】P.26(网络编程套接字基本概念—— socket编程接口 | socket编程接口相关函数详细介绍 )
10 0
|
13天前
|
中间件 Go API
Golang深入浅出之-Go语言标准库net/http:构建Web服务器
【4月更文挑战第25天】Go语言的`net/http`包是构建高性能Web服务器的核心,提供创建服务器和发起请求的功能。本文讨论了使用中的常见问题和解决方案,包括:使用第三方路由库改进路由设计、引入中间件处理通用逻辑、设置合适的超时和连接管理以防止资源泄露。通过基础服务器和中间件的代码示例,展示了如何有效运用`net/http`包。掌握这些最佳实践,有助于开发出高效、易维护的Web服务。
27 1
|
14天前
|
Go 开发者
Golang深入浅出之-HTTP客户端编程:使用net/http包发起请求
【4月更文挑战第24天】Go语言的`net/http`包在HTTP客户端编程中扮演重要角色,但使用时需注意几个常见问题:1) 检查HTTP状态码以确保请求成功;2) 记得关闭响应体以防止资源泄漏;3) 设置超时限制,避免长时间等待;4) 根据需求处理重定向。理解这些细节能提升HTTP客户端编程的效率和质量。
18 1
|
17天前
|
网络协议 Linux Go
Go语言TCP Socket编程(下)
Go语言TCP Socket编程
|
17天前
|
网络协议 Ubuntu Unix
Go语言TCP Socket编程(上)
Go语言TCP Socket编程
|
17天前
|
存储 网络协议 关系型数据库
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
|
25天前
|
网络协议 Unix Linux
Linux 下 socket 编程介绍
Linux 下 socket 编程介绍
|
4月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
46 0
|
16天前
|
开发框架 前端开发 JavaScript
JavaScript云LIS系统源码ASP.NET CORE 3.1 MVC + SQLserver + Redis医院实验室信息系统源码 医院云LIS系统源码
实验室信息系统(Laboratory Information System,缩写LIS)是一类用来处理实验室过程信息的软件,云LIS系统围绕临床,云LIS系统将与云HIS系统建立起高度的业务整合,以体现“以病人为中心”的设计理念,优化就诊流程,方便患者就医。
22 0