支持SSL的封装类--CAsyncSocketEx

Windows上的异步通信程序库CAsyncSocketEx Windows上的异步通信程序库CAsyncSocketEx       CAsyncSocketEx是一个叫Tim Kosse的2003年发布到codeproject.com上的一个小程序库(http://www.codeproject.com/KB/IP/casyncsocketex.aspx),说是MFC的CAsyncSocket的替代品和改进品。   作为异步通信,CAsync 阅读详情

Introduction

CAsyncSocketEx is an MFC-less replacement for CAsyncSocket which does also offer a flexible layer code. This class was written because CAsyncSocket is not the fastest WinSock wrapper and it's very hard to add new functionality to CAsyncSocket derived classes. This class supports most of the features of CAsyncSocket and can be extended quite easily. With the layer system, you can implement transparent layers like proxy or SSL layers easily.

One benefit is, that CAsyncSocketEx works without MFC, but if MFC is enabled (_AFX is defined), CAsyncSocketEx offers some functions taking a CString as argument, so that CAsyncSocketEx is compatible with CAsyncSocket.

CAsyncProxySocketLayer is a layer class for CAsyncSocketEx. With it's help, you can connect through proxy servers. This layer is almost transparent, so you won't have to modify much code to use it. Other layers can be written quite easily as well, just derive new layer classes from CAsyncSocketExLayer.

Another layer class is CAsyncSslSocketLayer, which can be used to establish SSL connections to servers.

Using the code

To use CAsyncSocketEx, just replace all occurrences of CAsyncSocket in your code with CAsyncSocketEx. If you did not enhance CAsyncSocket yourself in any way, you won't have to change anything else in your code. However, problems can occur if you're using IOCtl or SetSockOpt in your code, CAsyncSocketEx and the layer classes do not recognize if you change any options using IOCtl or SetSockOpt.

CAsyncSocketEx provides some useful new functions:

// Triggers an event on the socket
// Any combination of FD_READ, FD_WRITE, FD_CLOSE, 
//FD_ACCEPT, FD_CONNECT and FD_FORCEREAD is valid for lEvent.
BOOL TriggerEvent(long lEvent);

With TriggerEvent you can explicitly trigger the notification messages handled in OnReceive, OnSend, ...

TriggerEvent(FD_SEND); for example posts a message to the internal helper window and returns. Later, if this message is processed, OnSend will be called.

There's something special about FD_READ: OnRecieve will only be called if there is really any data available. To trigger a call to OnReceive regardless of data waiting or not, use FD_FORCEREAD instead.

The other new functions are for the new layer system.

Using the layer system

Using the layer system is really simple. In most cases, it is sufficient to create a new instance of a layer class and attach it to a CAsyncSocketEx instance.

//Attaches a new layer to the socket.
BOOL AddLayer(CAsyncSocketExLayer *pLayer);

This adds a new layer derived from CAsyncSocketExLayer to the socket. You can add more than one layer. The layer last added to the layer list, works directly on the socket and the other layers operate on the previous one. All calls you make to CAsyncSocketEx are first handled by the layer first added to the list. Example: If you want to establish an SSL connection to a server over a proxy server, first add the SSL layer (see the CAsyncSslSocketLayer article), then add the proxy layer.

//Resets layer chain.
void RemoveAllLayers()

This function detached all layers from the socket. But it does not destroy the layer class instances, you are still responsible for destroying the layer instances. In theory, it would be possible to add a layer previously detached from a socket to a new socket, but this is not recommended. You should create and attach a new instance of the layer for each established connection.

//Called by the layers to notify application of some events
virtual int OnLayerCallback(const CAsyncSocketExLayer *pLayer, 
                            int nType, int nParam1, int nParam2);

This function is called by the layers to notify the program about more or less important events of the layer. The first parameter identifies the layer which did send the notification message. The second parameter is the type of the notification. Valid notification types are:

  • LAYERCALLBACK_STATECHANGE

    This notification is sent if the state of the layer has changed. nParam1 contains the new state, nParam2 contains the old state. Valid states are:

    • 0 - Not a valid socket
    • 1 - Unconnected
    • 2 - Connecting
    • 3 - Listening
    • 4 - Connected
    • 5 - Closed
    • 6 - Aborted
  • LAYERCALLBACK_LAYERSPECIFIC

    This notification is sent on events specific to a each layer. See the layer's description for the meaning and the parameters of this notification message.

CAsyncProxySocketLayer

With this layer you can connect through proxy servers. This class supports SOCKS4/5 as well as HTTP/1.1 (using CONNECT method) proxies. For more information about SOCKS4/5 go to http://www.socks.nec.com/socksprot.html, for more information about HTTP 1.1 go to http://www.rfc-editor.org/ and search for RFC2616.

You don't have to change much, in your already existing code, to use CAsyncProxySocketLayer. To use it, create an instance of CAsyncProxySocketLayer, call SetProxy and attach it to a CAsyncSocketEx instance. You have to process OnLayerCallback in your CAsyncSocketEx instance as it will receive all layer notifications.

The following notifications are sent (of type LAYERCALLBACK_LAYERSPECIFIC):

  • Error codes:
    PROXYERROR_NOERROR 0 
    PROXYERROR_NOCONN 1Can't connect to proxy server, use GetLastError for more information
    PROXYERROR_REQUESTFAILED 2Request failed, can't send data
    PROXYERROR_AUTHREQUIRED 3 Authentication required
    PROXYERROR_AUTHTYPEUNKNOWN 4 Authtype unknown or not supported
    PROXYERROR_AUTHFAILED 5 Authentication failed
    PROXYERROR_AUTHNOLOGON 6  
    PROXYERROR_CANTRESOLVEHOST 7 
  • Status messages:
    PROXYSTATUS_LISTENSOCKETCREATED 8Called when a listen socket was created successfully. Unlike the normal listen function, a socksified socket has to connect to the proxy to negotiate the details with the server on which the listen socket will be created. The two parameters will contain the IP and port of the listen socket on the server.

If you want to use CAsyncProxySocketLayer to create a listen socket, you have to use this overloaded function:

BOOL PrepareListen(unsigned long serverIp);

serverIP is the IP of the server you are already connected through the SOCKS proxy. You can't use listen sockets over a SOCKS proxy without a primary connection. Listen sockets are only supported by SOCKS proxies, this won't work with HTTP proxies.

When the listen socket is created successfully, the PROXYSTATUS_LISTENSOCKETCREATED notification is sent. The parameters will tell you the IP and the port of the listen socket. After it, you have to handle the OnAccept message and accept the connection.

Be careful when calling Accept: rConnected socket will NOT be filled! Instead use the instance which created the listen socket, it will handle the data connection.

If you want to accept more than one connection, you have to create a listing socket for each of them!

Description of important functions and their parameters:
void SetProxy(int nProxyType);
void SetProxy(int nProxyType, CString ProxyHost, int nProxyPort);
void SetProxy(int nProxyType, CString ProxyHost, int nProxyPort,
                            CString ProxyUser, CString ProxyPass);

Call one of this functions to set the proxy type.

Parameters:

  • nProxyType specifies the Proxy Type.
  • ProxyHost and nProxyPort specify the address of the proxy
  • ProxyUser and ProxyPass are only available for SOCKS5 proxies.

Supported proxy types:

  • PROXYTYPE_NOPROXY
  • PROXYTYPE_SOCKS4
  • PROXYTYPE_SOCKS4A
  • PROXYTYPE_SOCKS5
  • PROXYTYPE_HTTP11

There are also some other functions:

int GetProxyType();

Returns the used proxy.

const int GetLastProxyError() const;

Returns the last proxy error, see above for a list of available error codes. 

CAsyncSocketEx 很不错的一个类,用法和CAsyncSocket 一样,增加了proxy等功能的支持 立即下载

相关推荐

【React Native】源码分析之Native UI的封装和管理

转载请注明出处:http://blog.youkuaiyun.com/u013531824/article/details/54020287,谢谢。  ReactNative作为使用React开发Native应用的新框架,随着时间的增加,无论是社区还是个人对她的兴趣与日递增。此文目的是希望和大家一起欣赏一下ReactNative的部分源码。阅读源码好处多多,让攻城狮更溜的开发ReactNative应用的同时,也

Tiny的专栏 9779

网络编程(57)—— Windows下使用CAsyncSocket搭建回声服务端和客户端

一、 引言         CAsyncSocket是MFC中对WSAAsyncSelect异步非阻塞通知IO的一个封装类。我们在《Windows下使用WSAAsyncSelect实现窗口处理socket消息》一文中讨论过WSAAsyncSelect的用法,知道它绑定一个窗口到一个socket,并注册了我们自定义的消息和需要监视的IO事件类型(FD_ACCEPT、FD_READ、FD_WRITE

Hyman的博客 2469

Vivado实战:如何用BUFGMUX_CTRL实现FPGA双时钟热切换(附时序约束模板)

本文详细介绍了如何使用Xilinx FPGA中的BUFGMUX_CTRL原语实现双时钟热切换的工业级解决方案。通过分析BUFGMUX_CTRL的核心机制、PCB布局约束和电源噪声抑制方法,提供Vivado实现全流程和时序约束模板,帮助工程师在工业自动化、车载电子等场景中提升系统可靠性。

weixin_29186249的博客 577

CAsyncSocketEx浅析

CAsyncSocketEx是Tim Kosse发布到codeproject.com一个开源库。利用消息驱动,通过CAsyncSocketEx类封装win32的网络接口。 以下通过一个简化的CAsyncSocketEx对其实现进行初步解析。 通过CAsyncSocketEx的create和消息处理函数WindowProc可以很清晰的整理出整个库的脉络。   BOOL CAsyncSock

pokeyode的专栏 1295

Halcon阈值算子 -- 局部阈值之dyn_threshold

Halcon阈值算子 – dyn_threshold HALCON有很多阈值算子,接下来将一一介绍。本文先介绍dyn_threshold。 以下介绍主要参考HALCON帮助文档,并加上自己的体会。如有纰漏,欢迎留言指出 理论介绍 dyn_threshold算子的函数签名如下: dyn_threshold(OrigImage, ThresholdImage : RegionDynThresh : Offset, LightDark : ) 输入参数有: OrigImage:待处理图像 ThresholdIm

m0_47015717的博客 5385

Windows 10/11下Drive Composer Pro 2.9.0完整安装与连接ACS880变频器实战教程

本文详细介绍了在Windows 10/11系统上安装Drive Composer Pro 2.9.0并连接ACS880变频器的完整教程。从系统环境准备、软件安装到许可证配置,再到USB、蓝牙和以太网三种连接方式的实战操作,帮助工业自动化工程师快速掌握ABB变频器的调试技巧。

weixin_32956805的博客 528

CAsyncSocket的封装使用

步骤一:从CAsyncSocket派生出CAsyncSocketEx class CAsyncSocketEx: public CAsyncSocket 添加函数 SetResponseWnd(CWnd*pWnd);//pWnd是CAsyncSocket中函数具体响应的类对象指针 在CAsyncSocketEx中重写基类虚函数  virtual void OnClose(int

csw_100的专栏 1191

从STM32F411到华大HC32F460:一个真实项目移植的踩坑与填坑全记录

本文详细记录了从STM32F411移植到华大HC32F460芯片的全过程,包括开发环境搭建、时钟树配置、中断处理、外设驱动、看门狗陷阱、Flash操作和低功耗设计等关键坑点及解决方案。特别针对HC32F460的200MHz主频、双PLL架构和HSRUN模式等特性进行了深入分析,为工程师提供实用的移植指南,帮助节省大量调试时间。

weixin_33691817的博客 419

【MFC编程】使用CAsyncSocket实现TFTP客户端

【华科网安计网实验】从零开始教你使用MFC编写TFTP客户端。本文首先将介绍如何在VS2022中创建MFC程序,其次介绍CAsyncSocket的使用方法,最后是TFTP客户端的实现过程。

m0_60964043的博客 1713

Python学习(一)

快速入门 1.Linux系统中是可以直接使用Python进行编程,具体如下: 进入Python:在linux的Terminal中输入 Python ,进入Python的命令行; 退出Python:在linux的Terminal中输入quit() 或 exit() 或 ctrl +d ,再回车,退出Python的命令行。 2.python的源程序就是一个特殊格式的文本文件,可以使用任意文本编辑软件做Python的开发。 3.Python程序的文件扩展名通常都是 .py。 4.执行Python程序的方式:在

SummerXRT的博客 1281

电路布局中近场耦合问题

开关电源EMC设计中,传导骚扰超标往往源于空间耦合而非参数问题。某案例中,DC-DC模块的非屏蔽电感磁场泄漏耦合到邻近X电容,导致0.5MHz频点超标。通过将X电容移至PCB底层、优化布局保持15mm间距后,问题完全解决。实践证明:EMC设计需注重空间隔离,优先采用屏蔽电感,滤波元件应远离高频磁场区,布局优化比参数调整更有效。这提示工程师需将电磁兼容视为空间问题而不仅是电气问题。

硬核科技 375

支持SSL的封装类--CAsyncSslSocketLayer

IntroductionCAsyncSslSocketLayer is a layer class for CAsyncSocketEx which allows you to establish SSL secured connections to servers.For information about CAsyncSocketEx and the layer system in g

蓝剑专栏 1577

随机优化 (Stochastic Optimization) 算法详解及案例分析

随机优化是一种用于处理包含随机变量的优化问题的方法。其目标是通过考虑随机变量的概率分布,找到一个在期望意义下最优的解。

闲人编程的博客 4183

读懂源码系列-FileZilla Server 设计原则分析-socket 事件处理流程(4)

1.前言         上一篇讲到 socket  2.处理流程 static LRESULT CALLBACK CAsyncSocketExHelperWindow::WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) void CListenSocket::OnAccept(int nErrorCode)

systomnet的专栏 724

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

分享一款 ftp 软件 —— filezilla的源码

注意: 客户端使用 C++11 开发,而之所以服务器端的 filezillaserver 和 filezillaserver 选择使用较新的 VS2015,是因为服务器端的代码使用了 C++14 的一些特性,而 VS2013 是不支持 C++14 的所有的特性的。3. 编译filezillaserverinterface用VS2015打开filezillaserverinterface\filezillaserver\filezillaserverinterface.sln编译即可。

左雪菲的专栏 1588

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

FileZilla Server源码分析(1)

之所以有本系列的分析,是因为两点: FileZilla 是目前非常火爆的开源ftp项目,整个项目采用C++代码编写,代码紧凑可读性高,值得学习(缺陷是注释太少)。网络上已有的对该源码的分析基于的版本是0.9.18,分析比较粗略,无论是框架还是细节。 这里仅仅是我个人对FileZilla Server源码0.9.34版本的分析,能力有限,不足和错误之处还希望大家不吝斧正。 本片作为开篇

flymachine的专栏 9443

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

eMule中的 CAsyncSocketEx 类

 说说eMule中的大多数通信类的基类CAsyncSocketEx1、先看看静态链表m_spAsyncSocketExThreadDataList其数据成员定义如下: struct t_AsyncSocketExThreadData {  CAsyncSocketExHelperWindow *m_pHelperWindow; //线程中的socket事件接收窗口  int nInst

InsideKernel的专栏 1554

SSL通信流程函数封装

OpenSSL的API使用起来与我们常用的普通网络通信函数大同小异,主要分为初始化阶段、密钥载入及验证阶段、创建SSL(类似于文件描述符(套结字)创建函数socket)阶段、绑定套结字阶段(类似于bind函数)、请求建立连接阶段(客户端特有,类似于connect)、接受连接请求阶段(服务端特有,类似于accept函数)、数据收发阶段(类似于read和write)、结束连接阶段以及结束监听阶段(类似

mabo1988313的专栏 952

HTTPS简介以及SSL协议详解

这里写目录标题1.HTTPS简介2.SSL协议介绍2.1、SSL协议的功能2.2、SSL协议在协议栈的位置2.3、SSL协议组成2.3.1、纪录协议的功能2.3.2、SSL记录协议的示意图:2.3.3、SSL协议封装过程2.3.4、SSL协议解封装过程2.3.5、SSL握手协议2.3.5.1、全流程握手全流程握手详解Client helloServer helloServer CertificateClient Certificate RequstServer Hello doneClient Certif

一只青木呀 2258

SSL协议详解

SSL)是一个安全协议,为基于TCPTransmission Control Protocol)的应用层协议提供安全连接,SSL介于TCP/IP协议栈第四层和第七层之间。主要提供私密性、完整性和身份验证;我们常见的就是 SSL为HTTP)协议提供安全连接。SSL协议是一种在两个机器之间提供安全通道的协议,它具有保护数据传输以及识别通信机器的功能。内的愈来愈多的浏览器支持SSL,SSL协议成为应用最广泛的安全协议之一。

m0_67756456的博客 9267

高性能服务器代码,(八)高性能服务器架构设计总结3——以flamigo服务器代码为例...

(WM_SOCKETEX_NOTIFY+pWnd->m_nWindowDataSize))18 {19 //Lookup socket and verify if it's valid20 CAsyncSocketEx *pSocket=pWnd->m_pAsyncSocketExWindowData[m...

weixin_32784349的博客 244

eMule源码分享

这里分享一款资源分享与下载工具——电驴,其实严格来说,应该叫电骡,这是我维护的版本,eMuleVeryCD版本,VeryCD是一个不错的资源分享网站:http://www.verycd.com/。大概在08年之前,中国市场流行的下载工具大概有三款:网际快车(flashget)、电驴(eMule)和迅雷,后来前两者都没落了。电驴的源码也开源了,迅雷抓住这个机会分析了电驴的下载协议(Kad...

左雪菲的专栏 4895

使用 acme.sh + 阿里云 DNS API 实现 SSL 证书自动签发与自动续期

本文详述在Linux服务器上使用acme.sh结合阿里云DNS API(RAM子账号)为Nginx站点自动签发并续期Let's Encrypt证书的全流程。涵盖阿里云RAM子账号权限最小化配置、DNS-01验证原理、证书签发与安装、自动续期及热加载机制,实现安全、高效、免人工干预的HTTPS运维方案,适用于长期维护多域名站点的运维场景。

轻的博客 243
上一篇: SSL 例子
下一篇: 支持SSL的封装类--CAsyncSslSocketLayer
bukebushuo
博客等级 码龄26年 15粉丝 45原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值