您好,登錄后才能下訂單哦!
HP-Socket是一個免費開源的通信庫。示例代碼看起來比較紛繁,所以簡化了一下,寫了一個demo.
服務器端:
//調試模式請包含HPSocket4C_UD.lib //在工程目錄中,包含頭件HPSocket4C.h、HPTypeDef.h、SocketInterface.h #pragma once #include "HPSocket4C.h" const LPCTSTR DEF_ADDRESS = _T("0.0.0.0"); const USHORT PORT = 9700; class Server { public: Server(); ~Server(); HP_TcpPullServer m_pServer; //創建服務器 HP_TcpPullServerListener m_pListener;//監聽器 private: //回調函數 static En_HP_HandleResult __stdcall OnPrepareListen(HP_Server pSender, SOCKET soListen); static En_HP_HandleResult __stdcall OnAccept(HP_Server pSender, HP_CONNID dwConnID, SOCKET soClient); static En_HP_HandleResult __stdcall OnSend(HP_Server pSender, HP_CONNID dwConnID, const BYTE* pData, int iLength); static En_HP_HandleResult __stdcall OnReceive(HP_Server pSender, HP_CONNID dwConnID, int iLength); static En_HP_HandleResult __stdcall OnClose(HP_Server pSender, HP_CONNID dwConnID, En_HP_SocketOperation enOperation, int iErrorCode); static En_HP_HandleResult __stdcall OnShutdown(HP_Server pSender); public: VOID StartServer(); VOID StopServer(); private: static BOOL m_ServerStat; };
.CPP文件
#include "stdafx.h" #include "Server.h" BOOL Server::m_ServerStat = FALSE; Server::Server() { // 創建監聽器對象 m_pListener = ::Create_HP_TcpPullServerListener(); // 創建 Socket 對象 m_pServer = ::Create_HP_TcpPullServer(m_pListener); // 設置 Socket 監聽器回調函數 ::HP_Set_FN_Server_OnPrepareListen(m_pListener, OnPrepareListen); ::HP_Set_FN_Server_OnAccept(m_pListener, OnAccept); ::HP_Set_FN_Server_OnSend(m_pListener, OnSend); ::HP_Set_FN_Server_OnPullReceive(m_pListener, OnReceive); ::HP_Set_FN_Server_OnClose(m_pListener, OnClose); ::HP_Set_FN_Server_OnShutdown(m_pListener, OnShutdown); } Server::~Server() { // 銷毀 Socket 對象 ::Destroy_HP_TcpPullServer(m_pServer); // 銷毀監聽器對象 ::Destroy_HP_TcpPullServerListener(m_pListener); } En_HP_HandleResult Server::OnPrepareListen(HP_Server pSender, SOCKET soListen) { setlocale(LC_ALL, "chs"); TCHAR szAddress[50]; int iAddressLen = sizeof(szAddress) / sizeof(TCHAR); USHORT usPort; ::HP_Server_GetListenAddress(pSender, szAddress, &iAddressLen, &usPort); return HR_OK; } En_HP_HandleResult Server::OnAccept(HP_Server pSender, HP_CONNID dwConnID, SOCKET soClient) { BOOL bPass = TRUE; TCHAR szAddress[50]; int iAddressLen = sizeof(szAddress) / sizeof(TCHAR); USHORT usPort; ::HP_Server_GetRemoteAddress(pSender, dwConnID, szAddress, &iAddressLen, &usPort); USES_CONVERSION; char buff[255] = {}; sprintf_s(buff,255,"地址%s:%ld", W2A(szAddress), usPort); return HR_OK; } En_HP_HandleResult Server::OnSend(HP_Server pSender, HP_CONNID dwConnID, const BYTE* pData, int iLength) { return HR_OK; } En_HP_HandleResult Server::OnReceive(HP_Server pSender, HP_CONNID dwConnID, int iLength) { return HR_OK; } En_HP_HandleResult Server::OnClose(HP_Server pSender, HP_CONNID dwConnID, En_HP_SocketOperation enOperation, int iErrorCode) { return HR_OK; } En_HP_HandleResult Server::OnShutdown(HP_Server pSender) { return HR_OK; } VOID Server::StartServer() {//啟動服務器 USES_CONVERSION; if (!::HP_Server_Start(m_pServer, DEF_ADDRESS, PORT)) { m_ServerStat = FALSE; printf("服務器啟動出錯:%d,%s", ::HP_Server_GetLastError(m_pServer), W2A(::HP_Server_GetLastErrorDesc(m_pServer))); } else { m_ServerStat = TRUE; } } VOID Server::StopServer() {//停止服務器 ::HP_Server_Stop(m_pServer); m_ServerStat = FALSE; }
客戶端:
CHpClientDlg::CHpClientDlg(CWnd* pParent /*=NULL*/) : CDialogEx(IDD_HPCLIENT_DIALOG, pParent) {//初始化 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); // 創建監聽器對象 m_pListener = ::Create_HP_TcpPullClientListener(); // 創建 Socket 對象 m_pClient = ::Create_HP_TcpPullClient(m_pListener); // 設置 Socket 監聽器回調函數 ::HP_Set_FN_Client_OnConnect(m_pListener, OnConnect); ::HP_Set_FN_Client_OnSend(m_pListener, OnSend); ::HP_Set_FN_Client_OnPullReceive(m_pListener, OnReceive); ::HP_Set_FN_Client_OnClose(m_pListener, OnClose); }
連接服務器:
void CHpClientDlg::OnBnClickedButton2() { if (::HP_Client_Start(m_pClient, DEFAULT_ADDRESS, DEFAULT_PORT, true)) { m_edShow->SetSel(-1, -1); //插入光標放于最后 m_edShow->ReplaceSel(_T("連接成功!")); m_edShow->ScrollWindow(0, 0); //滾動到插入點 } else { CString str; str.Format(_T("%d,%s"), ::HP_Client_GetLastError(m_pClient), HP_Client_GetLastErrorDesc(m_pClient)); m_edShow->SetSel(-1, -1); //插入光標放于最后 m_edShow->ReplaceSel(str); m_edShow->ScrollWindow(0, 0); //滾動到插入點 } }
準備回調函數(固定寫法):
En_HP_HandleResult CHpClientDlg::OnConnect(HP_Client pSender, HP_CONNID dwConnID) { TCHAR szAddress[50]; int iAddressLen = sizeof(szAddress) / sizeof(TCHAR); USHORT usPort; ::HP_Client_GetLocalAddress(pSender, szAddress, &iAddressLen, &usPort); return HR_OK; } En_HP_HandleResult CHpClientDlg::OnSend(HP_Client pSender, HP_CONNID dwConnID, const BYTE* pData, int iLength) { return HR_OK; } En_HP_HandleResult CHpClientDlg::OnReceive(HP_Client pSender, HP_CONNID dwConnID, int iLength) { byte buffer[1000]{ 0 }; En_HP_FetchResult result = ::HP_TcpPullClient_Fetch(pSender, buffer, 1000); return HR_OK; } En_HP_HandleResult CHpClientDlg::OnClose(HP_Client pSender, HP_CONNID dwConnID, En_HP_SocketOperation enOperation, int iErrorCode) { return HR_OK; }
發送數據:
void CHpClientDlg::OnBnClickedButton1() {//發送數據 CString strSend; m_edSend->GetWindowText(strSend); USES_CONVERSION; char * p = W2A(strSend); if (::HP_Client_Send(m_pClient, (BYTE*)p, strlen(p)+1)) { CString strinfo; strinfo.Format(_T("%d-%s"), ::HP_Client_GetConnectionID(m_pClient), strSend); m_edShow->SetSel(-1, -1); //插入光標放于最后 m_edShow->ReplaceSel(_T("\r\n發送成功!")); m_edShow->ScrollWindow(0, 0); //滾動到插入點 } else ;//::LogSendFail(::HP_Client_GetConnectionID(m_pClient), ::SYS_GetLastError(), ::HP_GetSocketErrorDesc(SE_DATA_SEND)); }
關閉連接:
void CHpClientDlg::OnBnClickedButton3() { if (::HP_Client_Stop(m_pClient)) ;//記錄客戶端(::HP_Client_GetConnectionID(m_pClient)); else ASSERT(FALSE); }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。