扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
Control ID Type Member IDC_EDITMSG CEdit m_MSG IDC_LISTMSG ClistBox m_MSGS |
#include "CSockClient.h" #include "CSockClientDlg.h" |
public: BOOL m_bConnected; UINT m_nLength; char m_szBuffer[4096]; |
MySock::MySock() { m_nLength=0; memset(m_szBuffer,0,sizeof(m_szBuffer)); m_bConnected=FALSE;} MySock::~MySock() { //关闭套接字 if(m_hSocket!=INVALID_SOCKET) Close(); } void MySock::OnReceive(int nErrorCode) { m_nLength=Receive(m_szBuffer,sizeof(m_szBuffer),0); //下面两行代码用来获取对话框指针 CCSockClientApp* pApp=(CCSockClientApp*)AfxGetApp(); CCSockClientDlg* pDlg=(CCSockClientDlg*)pApp- >m_pMainWnd; pDlg- >m_MSGS.InsertString(0,m_szBuffer); memset(m_szBuffer,0,sizeof(m_szBuffer)); CAsyncSocket::OnReceive(nErrorCode);} void MySock::OnSend(int nErrorCode) { Send(m_szBuffer,m_nLength,0); m_nLength=0; memset(m_szBuffer,0,sizeof(m_szBuffer)); //继续提请一个“读”的网络事件,接收Server消息 AsyncSelect(FD_READ); CAsyncSocket::OnSend(nErrorCode);} void MySock::OnConnect(int nErrorCode) { if (nErrorCode==0) { m_bConnected=TRUE; CCSockClientApp* pApp=(CCSockClientApp*)AfxGetApp(); CCSockClientDlg* pDlg=(CCSockClientDlg*)pApp- >m_pMainWnd; memcpy(m_szBuffer,"Connected to ",13); strncat(m_szBuffer,pDlg- >m_szServerAdr, sizeof(pDlg- >m_szServerAdr)); pDlg- >m_MSGS.InsertString(0,m_szBuffer); AsyncSelect(FD_READ);}//提请一个“读”的网络事件,准备接收 CAsyncSocket::OnConnect(nErrorCode); } |
Control ID Type Member IDC_Addr CString m_Addr IDC_Port Int m_Port |
#include "AddrDlg.h" protected: int TryCount; MySock m_clientSocket; UINT m_szPort; public: char m_szServerAdr[256]; |
m_clientSocket.ShutDown(2); m_clientSocket.m_hSocket=INVALID_SOCKET; m_clientSocket.m_bConnected=FALSE; CAddrDlg m_Dlg; m_Dlg.m_Port=1088;//默认端口1088 if (m_Dlg.DoModal()==IDOK && !m_Dlg.m_Addr.IsEmpty()) { memcpy(m_szServerAdr,m_Dlg.m_Addr,sizeof(m_szServerAdr)); m_szPort=m_Dlg.m_Port; SetTimer(1,1000,NULL);//建立计时器,每1秒尝试连接一次 TryCount=0;} |
void CCSockClientDlg::OnTimer(UINT nIDEvent) { if (m_clientSocket.m_hSocket==INVALID_SOCKET) { BOOL bFlag=m_clientSocket.Create(0,SOCK_STREAM,FD_CONNECT); if(!bFlag) { AfxMessageBox("Socket Error!"); m_clientSocket.Close(); PostQuitMessage(0); return;}} m_clientSocket.Connect(m_szServerAdr,m_szPort); TryCount++; if (TryCount >=10 || m_clientSocket.m_bConnected) { KillTimer(1); if (TryCount >=10) AfxMessageBox("Connect Failed!"); return;} CDialog::OnTimer(nIDEvent);} |
void CCSockClientDlg::OnSend() {if (m_clientSocket.m_bConnected) { m_clientSocket.m_nLength=m_MSG.GetWindowText (m_clientSocket.m_szBuffer, sizeof(m_clientSocket.m_szBuffer)); m_clientSocket.AsyncSelect(FD_WRITE); m_MSG.SetWindowText("");}} 双击IDD_CSOCKCLIENT_DIALOG对话框中的“关闭”按钮,添加以下代码: void CCSockClientDlg::OnExit() { m_clientSocket.ShutDown(2); //关闭Socket EndDialog(0);} |
CNewSocket::OnReceive(int nErrorCOde) { m_nLength=Receive(m_szBuffer,sizeof(m_szBuffer),0); AsyncSelect(FD_WRITE);}// 直接转发消息 CNewSocket::OnSend(int nErrorCode) { Send(m_szBuffer,m_nLength,0);} |
public:: CNewSocket* m_pSocket; void CMyServerSocket::OnAccept(int nErrorCode) { CNewSocket* pSocket = new CNewSocket(); if (Accept(*pSocket)) { pSocket- >AsyncSelect(FD_READ); m_pSocket=pSocket;}else delete pSocket;} |
public: CMyServerSocket m_srvrSocket; void CCSockServerDlg::OnListen() { if (m_srvrSocket.m_hSocket==INVALID_SOCKET) { BOOL bFlag=m_srvrSocket.Create (UserPort,SOCK_STREAM,FD_ACCEPT); if (!bFlag) { AfxMessageBox(“Socket Error!”); M_srvrSocket.Close(); PostQuitMessage(0); Return;}} if (!m_srvrSocket。Listen(1)) { int nErrorCode = m_srvrSocket.GetLastError(); if (nError!=WSAEWOULDBLOCK) { AfxMessageBox(“Socket Error!”); M_srvrSocket.Close(); PostQuitMessage(0); Return;}}} |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者