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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。