使用Poco C++庫(kù)創(chuàng)建websocket安全訪問(wss)客戶端
來源:易賢網(wǎng) 閱讀:1674 次 日期:2014-12-03 13:27:45
溫馨提示:易賢網(wǎng)小編為您整理了“使用Poco C++庫(kù)創(chuàng)建websocket安全訪問(wss)客戶端”,方便廣大網(wǎng)友查閱!

Poco websocket庫(kù)特點(diǎn):

1,使用http/https ClientSession創(chuàng)建websocket client

2,是同步的,這對(duì)C++桌面編程來說應(yīng)該是夠用的.

3,依賴openssl.

代碼如下:

#include "stdafx.h"

#include <iostream>

#include <assert.h>

#include "Poco/Net/WebSocket.h"

#include "Poco/Net/HTTPClientSession.h"

#include "Poco/Net/HTTPRequest.h"

#include "Poco/Net/HTTPResponse.h"

#include "Poco/Net/ServerSocket.h"

#include "Poco/Net/NetException.h"

#include "Poco/Exception.h"

#include "Poco/Net/HTTPSClientSession.h"

#include "Poco/URIStreamOpener.h"

#include "Poco/StreamCopier.h"

#include "Poco/Path.h"

#include "Poco/URI.h"

#include "Poco/SharedPtr.h"

#include "Poco/Exception.h"

#include "Poco/Net/HTTPStreamFactory.h"

#include "Poco/Net/HTTPSStreamFactory.h"

#include "Poco/Net/FTPStreamFactory.h"

#include "Poco/Net/SSLManager.h"

#include "Poco/Net/KeyConsoleHandler.h"

#include "Poco/Net/ConsoleCertificateHandler.h"

#include "Poco/Net/AcceptCertificateHandler.h"

#include "Poco/Net/SecureStreamSocket.h"

#include "Poco/Net/X509Certificate.h"

#include <memory>

using Poco::Net::HTTPSClientSession;

using Poco::Net::HTTPClientSession;

using Poco::Net::HTTPRequest;

using Poco::Net::HTTPResponse;

using Poco::Net::HTTPServerRequest;

using Poco::Net::HTTPServerResponse;

using Poco::Net::WebSocket;

using Poco::Net::WebSocketException;

using Poco::Exception;

using Poco::URIStreamOpener;

using Poco::StreamCopier;

using Poco::Path;

using Poco::URI;

using Poco::SharedPtr;

using Poco::Exception;

using Poco::Net::HTTPStreamFactory;

using Poco::Net::HTTPSStreamFactory;

using Poco::Net::FTPStreamFactory;

using Poco::Net::SSLManager;

using Poco::Net::Context;

using Poco::Net::KeyConsoleHandler;

using Poco::Net::PrivateKeyPassphraseHandler;

using Poco::Net::InvalidCertificateHandler;

using Poco::Net::ConsoleCertificateHandler;

using Poco::Net::AcceptCertificateHandler;

using Poco::Net::X509Certificate;

int main(int argc, _TCHAR* argv[])

{

char buffer[1024];

int flags;

int n;

std::string payload;

try

{

Poco::Net::initializeSSL();

SharedPtr<InvalidCertificateHandler> pCert = new ConsoleCertificateHandler(false); // ask the user via console

Context::Ptr pContext = new Context(Context::TLSV1_CLIENT_USE, "", "", "rootcert.pem", Context::VERIFY_RELAXED, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");

SSLManager::instance().initializeClient(0, pCert, pContext);

HTTPSClientSession cs("echo.websocket.org", 443);

HTTPRequest request(HTTPRequest::HTTP_GET, "/","HTTP/1.1");

HTTPResponse response;

std::string cmd;

WebSocket * ws = new WebSocket(cs, request, response); // Causes the timeout

payload = "SHAKETHAND";

ws->sendFrame(payload.data(), payload.size(), WebSocket::FRAME_TEXT);

n = ws->receiveFrame(buffer, sizeof(buffer), flags);

std::cout<<"return:"<<std::string(buffer,n)<<"\n";

ws->shutdown();

}

catch (Poco::Exception& ex)

{

std::cout << "Error: " <<__FILE__<<":"<<__LINE__<<":"<< ex.displayText() << "\n";

return -1;

}

return 0;

}

WS客戶端創(chuàng)建不再贅述,官方已有范例.

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:使用Poco C++庫(kù)創(chuàng)建websocket安全訪問(wss)客戶端
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)